在dplyr给予(我期望的)数量的总和

时间:2017-09-28 15:09:47

标签: r dplyr tidyverse

我试图在我的tibble中获得两个不同列的条件和。

请参阅下面一个可重现的问题示例。

Library(tidyverse)  

# Data for quick visualisation 
#        wave type calls detections
# 1 2017-03-31   B1    92         29
# 2 2017-03-31   N2    51         49
# 3 2017-03-31   N3    29         15
# 4 2017-03-31   N4   156        142
# 5 2017-03-31    P     3          3  

rst <- structure(list(wave = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("2017-03-31", 
                                                                       "2017-04-01", "2017-06-08", "2017-06-16", "2017-06-19", "2017-06-22", 
                                                                       "2017-06-26", "2017-06-29", "2017-07-03", "2017-07-07", "2017-07-11", 
                                                                       "2017-07-18", "2017-07-21", "2017-07-25", "2017-07-28", "2017-08-01", 
                                                                       "2017-08-04", "2017-08-08", "2017-08-11", "2017-08-15", "2017-08-18", 
                                                                       "2017-08-22", "2017-08-25", "2017-08-29"), class = "factor"), 
                    type = structure(c(1L, 5L, 6L, 7L, 8L), .Label = c("B1", 
                                                                       "B2", "F1", "N1", "N2", "N3", "N4", "P", "X"), class = "factor"), 
                    calls = c(92L, 51L, 29L, 156L, 3L), detections = c(29L, 49L, 
                                                                       15L, 142L, 3L)), class = "data.frame", row.names = c(NA, 
                                                                                                                            -5L), .Names = c("wave", "type", "calls", "detections"))

accompanyment <- rst %>%
  group_by(wave) %>%
  summarise(calls = n(),
            infection = sum(calls[type == 'B1']),
            detections = sum(detections[type == 'B1']))

实际输出是:

# A tibble: 1 × 4
    wave      calls infection detections
  <fctr>      <int>     <int>      <int>
1 2017-03-31     5         5         29

这对我来说很奇怪,因为infection = sum(calls[type == 'B1'])给出了一些行的计数(和calls = n()一样),而detections = sum(detections[type == 'B1'])给出了一个值的总和(尽管只有一个值)从中选择)。

基本上我预料到了这一点:

# A tibble: 1 × 4
    wave      calls infection detections
  <fctr>      <int>     <int>      <int>
1 2017-03-31     5         92         29

我做错了什么?

如果这是重复的道歉,我无法在SO上找到另一个这样的实例。

0 个答案:

没有答案