dplyr count number of one specific value of variable

前端 未结 1 1605
不思量自难忘°
不思量自难忘° 2021-02-05 15:37

Say I have a dataset like this:

id <- c(1, 1, 2, 2, 3, 3)
code <- c(\"a\", \"b\", \"a\", \"a\", \"b\", \"b\")
dat <- data.frame(id, code)
相关标签:
1条回答
  • 2021-02-05 16:09

    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             1
    2     2             2
    3     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     1
    2     2     2
    
    0 讨论(0)
提交回复
热议问题