Is it possible to average a variable only when another variable is true?
up vote
0
down vote
favorite
mean(data$NumKilled if data$ForAffil = TRUE, na.rm = TRUE)
This command doesn't work - is there a way to change it to command that R run the average of data$NumKilled when data$ForAffil = TRUE or when data$ForAffil = Yes?
r
add a comment |
up vote
0
down vote
favorite
mean(data$NumKilled if data$ForAffil = TRUE, na.rm = TRUE)
This command doesn't work - is there a way to change it to command that R run the average of data$NumKilled when data$ForAffil = TRUE or when data$ForAffil = Yes?
r
2
there is almost certainly a way this can be done; please provide a minimal, complete, and verifiable example
– landru27
Nov 12 at 3:38
mean(data$NumKilled[data$ForAffil == TRUE], na.rm = TRUE)or simplermean(data$NumKilled[data$ForAffil], na.rm = TRUE)
– G5W
Nov 12 at 12:37
Or convert to data.table and compute. data <- as.data.table(data). Then val <- data[ForAffil == TRUE | ForAffil == “Yes”, mean(NumKilled)]
– Prevost
Nov 12 at 13:07
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
mean(data$NumKilled if data$ForAffil = TRUE, na.rm = TRUE)
This command doesn't work - is there a way to change it to command that R run the average of data$NumKilled when data$ForAffil = TRUE or when data$ForAffil = Yes?
r
mean(data$NumKilled if data$ForAffil = TRUE, na.rm = TRUE)
This command doesn't work - is there a way to change it to command that R run the average of data$NumKilled when data$ForAffil = TRUE or when data$ForAffil = Yes?
r
r
edited Nov 12 at 11:30
markus
9,770929
9,770929
asked Nov 12 at 3:26
Megan Drown
1
1
2
there is almost certainly a way this can be done; please provide a minimal, complete, and verifiable example
– landru27
Nov 12 at 3:38
mean(data$NumKilled[data$ForAffil == TRUE], na.rm = TRUE)or simplermean(data$NumKilled[data$ForAffil], na.rm = TRUE)
– G5W
Nov 12 at 12:37
Or convert to data.table and compute. data <- as.data.table(data). Then val <- data[ForAffil == TRUE | ForAffil == “Yes”, mean(NumKilled)]
– Prevost
Nov 12 at 13:07
add a comment |
2
there is almost certainly a way this can be done; please provide a minimal, complete, and verifiable example
– landru27
Nov 12 at 3:38
mean(data$NumKilled[data$ForAffil == TRUE], na.rm = TRUE)or simplermean(data$NumKilled[data$ForAffil], na.rm = TRUE)
– G5W
Nov 12 at 12:37
Or convert to data.table and compute. data <- as.data.table(data). Then val <- data[ForAffil == TRUE | ForAffil == “Yes”, mean(NumKilled)]
– Prevost
Nov 12 at 13:07
2
2
there is almost certainly a way this can be done; please provide a minimal, complete, and verifiable example
– landru27
Nov 12 at 3:38
there is almost certainly a way this can be done; please provide a minimal, complete, and verifiable example
– landru27
Nov 12 at 3:38
mean(data$NumKilled[data$ForAffil == TRUE], na.rm = TRUE) or simpler mean(data$NumKilled[data$ForAffil], na.rm = TRUE)– G5W
Nov 12 at 12:37
mean(data$NumKilled[data$ForAffil == TRUE], na.rm = TRUE) or simpler mean(data$NumKilled[data$ForAffil], na.rm = TRUE)– G5W
Nov 12 at 12:37
Or convert to data.table and compute. data <- as.data.table(data). Then val <- data[ForAffil == TRUE | ForAffil == “Yes”, mean(NumKilled)]
– Prevost
Nov 12 at 13:07
Or convert to data.table and compute. data <- as.data.table(data). Then val <- data[ForAffil == TRUE | ForAffil == “Yes”, mean(NumKilled)]
– Prevost
Nov 12 at 13:07
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255581%2fis-it-possible-to-average-a-variable-only-when-another-variable-is-true%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
there is almost certainly a way this can be done; please provide a minimal, complete, and verifiable example
– landru27
Nov 12 at 3:38
mean(data$NumKilled[data$ForAffil == TRUE], na.rm = TRUE)or simplermean(data$NumKilled[data$ForAffil], na.rm = TRUE)– G5W
Nov 12 at 12:37
Or convert to data.table and compute. data <- as.data.table(data). Then val <- data[ForAffil == TRUE | ForAffil == “Yes”, mean(NumKilled)]
– Prevost
Nov 12 at 13:07