简体   繁体   English

如何在汇总 dplyr 中进行子集化

[英]How to subset inside summarise dplyr

I would like to subset inside summarise() .我想在summarise()内进行子集化。 Is the following subset() -ing somehow possible?以下subset() -ing 是否有可能?

df <- structure(list(category = structure(c(1L, 1L, 1L, 2L, 2L, 1L, 
                                        1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L), .Label = c("category MB", "category LR"
                                        ), class = "factor"), start = c(111, 222, 333, 444, 555, 111, 
                                                                        222, 333, 444, 111, 111, 222, 333, 444), stop = c(666, 777, 888, 
                                                                                                                          999, 1000, 666, 777, 888, 999, 666, 666, 777, 888, 999), ID = c(101, 
                                                                                                                                                                                          101, 101, 101, 101, 102, 102, 102, 102, 102, 102, 102, 102, 102
                                                                                                                          )), row.names = c(NA, -14L), class = "data.frame")

library(dplyr)

df %>% 
group_by(ID) %>% 
summarise(
    countAll = n(),
    durationAll = sum(stop - start),
    countCategoryMB = sum(category == "category MB"),
    durationCategoryMB = sum( subset(., category == "category MB", select = stop) -  subset(., category == "category MB", select = start) ), # line in question, currently wrong
    countCategoryLR = sum(category == "category LR"),
    durationCategoryLR = sum( subset(., category == "category LR", select = stop) -  subset(., category == "category LR", select = start) ) # line in question, currently wrong
)

The expected result (pic at the end of the post), I am able to achieve with left_join() .预期的结果(帖子末尾的图片),我可以使用left_join()实现。 But I hope that it is possible to achieve the desired output in one-call with something like the code above.但我希望有可能通过类似上面的代码在一次调用中实现所需的输出。

# expected result achieved with left_join()
 df %>%
  group_by(ID) %>%
  summarise(countAll = n(),
            durationALL = sum(stop - start)) %>%
  left_join(
            .,
            df %>%
            filter(category == "category MB") %>%
            group_by(ID) %>%
            summarise(
            countCategoryMB = n(),
            durationCategoryMB = sum(stop - start)
           ),
           by = "ID"
 ) %>%
 left_join(
           .,
           df %>%
           filter(category == "category LR") %>%
           group_by(ID) %>%
           summarise(
           countCategoryLR = n(),
           durationCategoryLR = sum(stop - start)
           ) ,
          by = "ID"
          )

在此处输入图片说明

Thank you for your time!感谢您的时间!

In the below solution, (category == "category MB") equals 1 if it is True, otherwise it is 0. Therefore this effectively only sums the values of start and stop for those rows where category equals "category MB" or "category LR", as requested.在下面的解决方案中, (category == "category MB")如果为 True 则等于 1,否则为 0。因此,这有效地仅对类别等于“category MB”或“category”的那些行的 start 和 stop 的值求和LR”,根据要求。

df %>% 
  group_by(ID) %>% 
  summarise(
    countAll = n(),
    durationAll = sum(stop - start),
    countCategoryMB = sum(category == "category MB"),
    durationCategoryMB = sum( ((category == "category MB")*stop) - ((category == "category MB")*start) ),
    countCategoryLR = sum(category == "category LR"),
    durationCategoryLR = sum( ((category == "category LR")*stop) - ((category == "category LR")*start) )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM