组内的R dplyr计数观察

时间:2019-01-30 20:12:03

标签: r dplyr

我有一个数据框,其中包含不同日期和时间的是/否值。对于每一天,我想获得拥有数据的小时总数,以及具有Y值的小时总数。

df <- data.frame(day = c(1,1,1,2,2,3,3,3,3,4),
                 hour = c(1,2,3,1,2,1,2,3,4,1),
                 YN = c("Y","Y","Y","Y","Y","Y","N","N","N","N"))

df %>% 
  group_by(day) %>%
  summarise(tot.hour = n(),
            totY = WHAT DO I PUT HERE?)

1 个答案:

答案 0 :(得分:4)

使用布尔值,然后将其加起来

df %>% 
    group_by(day) %>%
    dplyr::summarise(tot.hour = n(),
              totY = sum(YN=='Y'))
# A tibble: 4 x 3
    day tot.hour  totY
  <dbl>    <int> <int>
1     1        3     3
2     2        2     2
3     3        4     1
4     4        1     0