【问题标题】:Add a new row by group based on a condition using datatable?根据使用数据表的条件按组添加新行?
【发布时间】:2021-03-11 21:13:28
【问题描述】:

这是我当前的数据表:

ID  Stage Month 
200 A    2020-11   
200 B    2020-11  
200 C    2020-11   
201 A    2020-11   
201 B   2020-11  
... 

仅当存在 A B 和 C 阶段时,我才尝试向每个 ID/月份组添加一行。

这将是我想要的输出:

ID  Stage Month 
200 A    2020-11   
200 B    2020-11  
200 C    2020-11 
200 All  2020-11  
201 A    2020-11   
201 B   2020-11 
...  

我是数据表和 R 的新手,因此非常感谢任何指导!

【问题讨论】:

    标签: r dataframe datatable row data-manipulation


    【解决方案1】:

    我们可以按'ID','Month'分组,检查ifall'A','B','C'找到%in%'Stage',然后将'Stage'与'All' (c(Stage, 'All)) 或 else 返回 'Stage'

    library(data.table)
    setDT(df1)[, .(Stage = if(all(c('A', 'B', 'C') %in% Stage)) c(Stage, 'All') 
                  else Stage),  by = .(ID, Month)][, names(df1), with = FALSE]
    

    -输出

    #    ID Stage   Month
    #1: 200     A 2020-11
    #2: 200     B 2020-11
    #3: 200     C 2020-11
    #4: 200   All 2020-11
    #5: 201     A 2020-11
    #6: 201     B 2020-11
    

    或者在tidyverse中使用类似的逻辑

    library(dplyr)
    df1 %>% 
        group_by(ID, Month) %>% 
        summarise(Stage = if(all(c('A', 'B', 'C') %in% Stage)) c(Stage, 'All') 
               else Stage, .groups = 'drop') %>% 
        select(names(df1))
    # A tibble: 6 x 3
    #     ID Stage Month  
    #  <int> <chr> <chr>  
    #1   200 A     2020-11
    #2   200 B     2020-11
    #3   200 C     2020-11
    #4   200 All   2020-11
    #5   201 A     2020-11
    #6   201 B     2020-11
    

    数据

    df1 <- structure(list(ID = c(200L, 200L, 200L, 201L, 201L), Stage = c("A", 
    "B", "C", "A", "B"), Month = c("2020-11", "2020-11", "2020-11", 
    "2020-11", "2020-11")), class = "data.frame", row.names = c(NA, 
    -5L))
    

    【讨论】:

      猜你喜欢
      • 2021-06-20
      • 2023-02-10
      • 1970-01-01
      • 1970-01-01
      • 2023-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-25
      相关资源
      最近更新 更多