我尝试通过每个 ID 获得唯一的组合,我一直收到错误,它不会扩展 ID。
ID <- c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,4,5,5,5,5,5,6,6,6,6)
var1 <- c("A","B","E","F","C","D","C","A","B","C","A","D","B","C",
"A","B","C","A","D","C","A","B","C","E","F","G")
df1 <- data.frame(ID,var1)
df1 <- df1[order(df1$ID, df1$var1),]
dd <- unique(df1)
dd <- data.table(dd)
dd[,new4 := t(combn(sort(var1), m = 3))[,1],by= "ID"]
dd[,new5:= t(combn(sort(var1), m = 3))[,2],by="ID"]
dd[,new6:= t(combn(sort(var1), m = 3))[,3],by="ID"]
Warning message:
In `[.data.table`(dd, , `:=`(new4, t(combn(sort(var1), m = 3))[, :
RHS 1 is length 10 (greater than the size (5) of group 1). The last 5 element(s) will be discarded.
ID var1 new4 new5 new6
1: 1 A A B C
2: 1 B A B E
3: 1 C A B F
4: 1 E A C E
5: 1 F A C F
6: 2 A A B C
7: 2 B A B D
8: 2 C A C D
9: 2 D B C D
10: 3 A A B C
11: 3 B A B D
12: 3 C A C D
13: 3 D B C D
14: 4 A A B C
15: 4 B A B C
16: 4 C A B C
17: 5 A A B C
18: 5 B A B D
19: 5 C A C D
20: 5 D B C D
21: 6 C C E F
22: 6 E C E G
23: 6 F C F G
24: 6 G E F G
每个 ID ID1 (A,B,C,E,F) 的输出没有给出足够的组合,它只给出了 5 个组合。反正有解决问题吗?输出我想要的ID1,有10个组合(ABC)(ACF)(ABF)(ABE)(BCE)(BCF)(CAB)(CAE)(CAF)(ECF)