【问题标题】:How to subset inside summarise dplyr如何在汇总 dplyr 中进行子集化
【发布时间】:2020-06-28 11:02:30
【问题描述】:

我想在summarise() 中进行子集化。以下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
)

我可以通过left_join() 实现预期的结果(图片在帖子末尾)。但我希望可以通过类似上述代码的方式一次性实现所需的输出。

# 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"
          )

感谢您的宝贵时间!

【问题讨论】:

  • sum( ((category == "category MB")*stop) - ((category == "category MB")*start) ) 怎么样?如果为真,(category == "category MB") 部分为 1,否则为 0。因此,这实际上仅对 category 等于“类别 MB”的行的 startstop 的值求和。
  • 有效!谢谢!不错的方法!请您将其写为答案,以便我可以检查它是否已回答!万事如意,-
  • 作为一个解决方案,我的意思是,所以我可以这样标记它:)

标签: r dplyr subset summarize


【解决方案1】:

在下面的解决方案中,(category == "category MB") 如果为 True,则等于 1,否则为 0。因此,这仅有效地将类别等于“类别 MB”或“类别 LR”的那些行的 start 和 stop 的值相加,根据要求。

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) )
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 2016-06-03
    • 2021-07-27
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    相关资源
    最近更新 更多