dplyr count number of one specific value of variable dplyr count number of one specific value of variable r r

dplyr count number of one specific value of variable


Try the following instead:

library(dplyr)dat %>% group_by(id) %>%  summarise(cip.completed= sum(code == "a"))Source: local data frame [3 x 2]    id cip.completed  (dbl)         (int)1     1             12     2             23     3             0

This works because the logical condition code == a is just a series of zeros and ones, and the sum of this series is the number of occurences.

Note that you would not necessarily use dplyr::count inside summarise anyway, as it is a wrapper for summarise calling either n() or sum() itself. See ?dplyr::count. If you really want to use count, I guess you could do that by first filtering the dataset to only retain all rows in which code==a, and using count would then give you all strictly positive (i.e. non-zero) counts. For instance,

dat %>% filter(code==a) %>% count(id)Source: local data frame [2 x 2]     id     n  (dbl) (int)1     1     12     2     2