7

I would like, when summarizing after grouping, to count the number of a specific level of another factor.

In the working example below, I would like to count the number of "male" levels in each group. I've tried many things with count, tally and so on but cannot find a straightforward and neat way to do it.

df <- data.frame(Group=replicate(20, sample(c("A","B"), 1)),
                 Value=rnorm(20),
                 Factor=replicate(20, sample(c("male","female"), 1)))
df %>% 
  group_by(Group) %>% 
  summarize(Value = mean(Value),
            n_male = ???)

Thanks for your help!

Dominique Makowski
  • 1,511
  • 1
  • 13
  • 30
  • 2
    Maybe `with(df, table(Factor))` or `with(df, table(Factor, Group))` would also be of interest. – lmo Mar 22 '17 at 14:53

1 Answers1

9

We can use sum on a logical vector i.e. Factor == "male". The TRUE/FALSE will be coerced to 1/0 to get the frequency of 'male' elements when we do the sum

df %>%
   group_by(Group) %>% 
   summarise(Value = mean(Value), 
             n_male = sum(Factor=="male"))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 2
    To add to that, you could also apply a filter to the variable `Factor` : `n_male = length(Factor[Factor=="male"]))` – count Mar 22 '17 at 15:08