0

There is a table with 3 columns

Id Food Sex

1       23       0
2       46       1
3       99       1
4       37       0
5       12       1

Now how do I get mean of food whose sex is marked as 1?

I have applied if(data$sex == 1) To check the conditions but how do I select food column to get mean of food with the help of sex column

phiver
  • 23,048
  • 14
  • 44
  • 56

1 Answers1

-1

Try :

library(dplyr)
data %>% filter(Sex ==1) %>% select(Food) %>% unlist() %>% mean(.)
S.Gradit
  • 164
  • 1
  • 9
  • Why don't you just use `summarise`, instead of `unlist` and `mean`? – phiver Sep 15 '19 at 12:23
  • I used to handle such issues with this syntaxe avoiding NA troubles. Sometimes I prefer the use of mutate to store the mean. Here it is not mentioned if the value has to be stored of just displayed – S.Gradit Sep 15 '19 at 12:28