[R] dplyr - counting a number of specific values in each column - for all columns at once

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Tue Jun 16 21:52:00 CEST 2015


Thank you guys - it's a great learning: 'summarise_each' and 'funs'

On Tue, Jun 16, 2015 at 3:47 PM, Hadley Wickham <h.wickham at gmail.com> wrote:
> On Tue, Jun 16, 2015 at 12:24 PM, Dimitri Liakhovitski
> <dimitri.liakhovitski at gmail.com> wrote:
>> Hello!
>>
>> I have a data frame:
>>
>> md <- data.frame(a = c(3,5,4,5,3,5), b = c(5,5,5,4,4,1), c = c(1,3,4,3,5,5),
>>       device = c(1,1,2,2,3,3))
>> myvars = c("a", "b", "c")
>> md[2,3] <- NA
>> md[4,1] <- NA
>> md
>>
>> I want to count number of 5s in each column - by device. I can do it like this:
>>
>> library(dplyr)
>> group_by(md, device) %>%
>> summarise(counts.a = sum(a==5, na.rm = T),
>>           counts.b = sum(b==5, na.rm = T),
>>           counts.c = sum(c==5, na.rm = T))
>>
>> However, in real life I'll have tons of variables (the length of
>> 'myvars' can be very large) - so that I can't specify those counts.a,
>> counts.b, etc. manually - dozens of times.
>>
>> Does dplyr allow to run the count of 5s on all 'myvars' columns at once?
>
> md %>%
>   group_by(device) %>%
>   summarise_each(funs(sum(. == 5, na.rm = TRUE)))
>
> Hadley
>
> --
> http://had.co.nz/



-- 
Dimitri Liakhovitski



More information about the R-help mailing list