【问题标题】:Aggregate all possible combinations of a column values in R based on common values in another column根据另一列中的常见值聚合 R 中列值的所有可能组合
【发布时间】:2018-07-20 09:27:24
【问题描述】:

我有一个数据框:

# 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 中获得此输出。

【问题讨论】:

  • 您好!请将此行添加到示例数据中:C\t5.

标签: r dataframe aggregate


【解决方案1】:

您可以使用dcastcombn 和少量处理来获得这样的最终列表

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

【讨论】:

  • 哇,太棒了!非常感谢你。这很好用!
  • 当然,我刚刚做了 :) 再次感谢您!
猜你喜欢
  • 2015-08-11
  • 1970-01-01
  • 2021-07-24
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 2022-11-08
相关资源
最近更新 更多