首页 文章

如果每个观察可以属于多个组,则进行聚合

提问于
浏览
3

我想按组聚合日期 . 然而,每个观察可以属于几个组(例如,观察1属于组A和B) . 我找不到用 data.table 实现这个目标的好方法 . 目前,我为每个可能的组创建了一个逻辑变量,如果观察属于该组,则该变量的值为 TRUE . 我正在寻找一种比下面提供的更好的方法 . 我也想知道如何用 tidyverse 实现这一目标 .

library(data.table)
# Data
set.seed(1)
TF <- c(TRUE, FALSE)
time <- rep(1:4, each = 5)
df <- data.table(time = time, x = rnorm(20), groupA = sample(TF, size = 20, replace = TRUE),
                                             groupB = sample(TF, size = 20, replace = TRUE),
                                             groupC = sample(TF, size = 20, replace = TRUE))

# This should be nicer and less repetitive
df[groupA == TRUE, .(A = sum(x)), by = time][
  df[groupB == TRUE, .(B = sum(x)), by = time], on = "time"][
    df[groupC == TRUE, .(C = sum(x)), by = time], on = "time"]

# desired output
time          A          B         C
1:    1         NA  0.9432955 0.1331984
2:    2  1.2257538  0.2427420 0.1882493
3:    3 -0.1992284 -0.1992284 1.9016244
4:    4  0.5327774  0.9438362 0.9276459

3 回答

  • 2

    这是一个 data.table 的解决方案:

    df[, lapply(.SD[, .(groupA, groupB, groupC)]*x, sum), time]
    # > df[, lapply(.SD[, .(groupA, groupB, groupC)]*x, sum), time]
    #    time     groupA     groupB    groupC
    # 1:    1  0.0000000  0.9432955 0.1331984
    # 2:    2  1.2257538  0.2427420 0.1882493
    # 3:    3 -0.1992284 -0.1992284 1.9016244
    # 4:    4  0.5327774  0.9438362 0.9276459
    

    或者(以thx到@ chinsoon12为评论)更多编程:

    df[, lapply(.SD*x, sum), by=.(time), .SDcols=paste0("group", c("A","B","C"))]
    

    如果您想要长格式的结果,您可以:

    df[, colSums(.SD*x), by=.(time), .SDcols=paste0("group", c("A","B","C"))]
    ### with indicator for the group:
    df[, .(colSums(.SD*x), c("A","B","C")), by=.(time), .SDcols=paste0("group", c("A","B","C"))]
    
  • 3

    我认为这里以长格式工作更容易 . 首先,我将观察结果收集到长格式,然后仅保留观察属于相应组的值 . 然后我删除逻辑列,并将组重命名为单个字母 . 然后我在组和时间之间进行汇总(总结在 dplyr 中) . 最后,我又回到了广泛的格式 .

    library(dplyr)
    library(tidyr)
    
    set.seed(1)
    TF <- c(TRUE, FALSE)
    time <- rep(1:4, each = 5)
    
    
    df <- data.frame(time = time, x = rnorm(20), groupA = sample(TF, size = 20, replace = TRUE),
                     groupB = sample(TF, size = 20, replace = TRUE),
                     groupC = sample(TF, size = 20, replace = TRUE))
    
    
    df %>% 
      gather(group, belongs, groupA:groupC) %>% 
      filter(belongs) %>% 
      select(-belongs) %>% 
      mutate(group = gsub("group", "", group)) %>% 
      group_by(time, group) %>% 
      summarise(x = sum(x)) %>% 
      spread(group, x)
    

    产量

    # A tibble: 4 x 4
    # Groups:   time [4]
       time       A      B     C
      <int>   <dbl>  <dbl> <dbl>
    1     1  NA      0.943 0.133
    2     2   1.23   0.243 0.188
    3     3  -0.199 -0.199 1.90 
    4     4   0.533  0.944 0.928
    
  • 1

    一个选项可以将 tidyrdplyr 包与 data.table 结合使用 . 尝试以长格式处理数据,然后将其更改为宽格式 .

    library(dplyr)
    library(tidyr)
    
    melt(df, id.vars = c("time", "x")) %>%
      filter(value) %>%
      group_by(time, variable) %>%
      summarise(sum = sum(x)) %>%
      spread(variable, sum)
    
    # # A tibble: 4 x 4
    # # Groups: time [4]
    # time  groupA groupB groupC
    # * <int>   <dbl>  <dbl>  <dbl>
    # 1     1  NA      0.943  0.133
    # 2     2   1.23   0.243  0.188
    # 3     3 - 0.199 -0.199  1.90 
    # 4     4   0.533  0.944  0.928
    

相关问题