I have a list of vectors and I would like to get a list of all possible combinations between the elements of every vector, i.e., combinations of n elements (from a vector) taken two and more than two at a time.
我有一个向量的列表,我想要一个列表,列出每个向量的元素之间所有可能的组合,例如。,n个元素(来自一个向量)的组合一次取2次,一次超过2次。
For instance, I have the following list:
例如,我有以下清单:
> DF
$`1`
A B C
1 11 2 432
$`2`
A B C
2 11 3 432
$`3`
A B C
3 13 4 241
Here's my code:
这是我的代码:
> d=list()
> for (j in 1:length(DF)){
+ for (i in 2:length(DF)){
+ d[[j]]=combn(DF[[j]],i,simplify=F)
+ }
+ }
> d
[[1]]
[[1]][[1]]
A B C
1 11 2 432
[[2]]
[[2]][[1]]
A B C
2 11 3 432
[[3]]
[[3]][[1]]
A B C
3 13 4 241
It is wrong, because I just get combinations of three elements taken three at a time. I would have to add combinations of three elements taken two at a time. I just get the last loop value. It is a problem of dimensions inside the loop.
这是错误的,因为我每次只取三个元素的组合。我需要添加三个元素的组合一次取两个。得到最后一个循环值。这是循环内部的维数问题。
If I run the loop just for i=2, then I get:
如果我只对I =2进行循环,就得到:
> d
[[1]]
[[1]][[1]]
A B
1 11 2
[[1]][[2]]
A C
1 11 432
[[1]][[3]]
B C
1 2 432
[[2]]
[[2]][[1]]
A B
2 11 3
[[2]][[2]]
A C
2 11 432
[[2]][[3]]
B C
2 3 432
....
1 个解决方案
#1
2
you could try
你可以试试
lapply(2:3, function(k) { lapply(1:length(DF),function(x){ combn(DF[[x]],k,
simplify = FALSE)})})
#1
2
you could try
你可以试试
lapply(2:3, function(k) { lapply(1:length(DF),function(x){ combn(DF[[x]],k,
simplify = FALSE)})})