gpt4 book ai didi

r - 根据另一列中的常见值聚合 R 中列值的所有可能组合

转载 作者:行者123 更新时间:2023-12-04 10:33:09 24 4
gpt4 key购买 nike

我有一个数据框:

# 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 具有共同的“类型”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

关于r - 根据另一列中的常见值聚合 R 中列值的所有可能组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51439386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com