首页 > 解决方案 > 根据另一列中的常见值聚合 R 中列值的所有可能组合

问题描述

我有一个数据框:

# A data Sample
df <- read.table(textConnection("id\ttype
                            A\t1
                            A\t2
                            A\t4
                            B\t1
                            B\t2
                            B\t3
                            B\t5
                            C\t1
                            C\t3
                            C\t4
                            C\t5"), header = TRUE)

# print the data sample
df

看起来像这样:

     id type
 1   A    1
 2   A    2
 3   A    4
 4   B    1
 5   B    2
 6   B    3
 7   B    5
 8   C    1
 9   C    3
 10  C    4
 11  C    5

你知道我怎样才能拥有一个数据框,包括所有可能的“id”值组合,这些组合具有共同的“类型”吗?像这样的东西:

     id-Combination     type- common
 1   A,B                1,2
 2   A,C                1,4
 3   B,C                1,3,5
 4   A,B,C              1

例如,它显示 'id' A 和 B 有共同的 'type' 1 和 2。

我想,“聚合”函数会有所帮助,但我不知道如何准确地使用它在 R 中获得这个输出。

标签: rdataframeaggregate

解决方案


您可以使用dcast,combn并且很少处理来获得这样的最终列表

library(data.table)
library(reshape2)

df <- read.table(textConnection("id\ttype
                            A\t1
                                A\t2
                                A\t4
                                B\t1
                                B\t2
                                B\t3
                                B\t5
                                C\t1
                                C\t3
                                C\t4
                                C\t5"), header = TRUE, stringsAsFactors = F)

# print the data sample
df

### dcast the table
df.cast <- dcast(df, id~type, value.var = "type", fun = length)

df.cast

   id 1 2 3 4 5
1  A  1 1 0 1 0
2  B  1 1 1 0 1
3  C  1 0 1 1 1

### Final dataframe
final.df <- data.table(NULL)

### Run the loop for each column, to check matching rows
for(i in 1:5){

### get matching rows values
  values <- df.cast[1 == df.cast[,grep(i, colnames(df.cast))], "id" ]

### Create Combination of these values (group of 2 or more)
  for(v in 2:length(values)){
    combn.mat <- combn(values, v)
    dx <- data.frame(t(combn.mat))
    combination <- apply(dx, 1, paste, collapse=",")

    temp <- data.frame(combination = combination)
    temp$Common <- i

    final.df <- rbind(final.df, temp)

  }
}

### Combine the same groups using toString
final.df <- final.df[, .(Common = toString(Common)), by = .(combination)] 

final.df

   combination   Common
1:         A,B     1, 2
2:         A,C     1, 4
3:         B,C  1, 3, 5
4:       A,B,C        1

推荐阅读