r - 根据另一列中的常见值聚合 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 中获得这个输出。
解决方案
您可以使用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
推荐阅读
- python - 错误:找不到满足要求的版本
- java - 在 Firebase Android 中将带有子项的值转换为 Java 对象
- django - 如何在 vue 模板中显示序列化程序验证错误消息?
- ruby - 如何修复 NoMethodError:nil:NilClass 的未定义方法 `<<'
- xpath - XPATH - 定位特定的子元素
- java - 无法从 Lotus Notes 中的邮件中获取 HTML 内容,即使在 setConvertMIME(false) 之后 getMIMEEntity() 也会返回 null
- html - 子只在 Firefox 中脱离内联网格父级
- git - Git 命令仅重置索引和工作树而不是 HEAD
- c# - 我怎样才能在 LINQ 中做到这一点
- r - 如何向导入的 csv 添加其他行?