我未能找到如何进行此特定转换。有谁有一个聪明的方法来实现这一目标?抱歉,如果我在某个地方错过了明显的答案。谢谢
# start with a single vector
1:3
# achieve this desired structure, containing every combination
# from combn( x , 1 ) thru combn( x , length( x ) )
list(
1 ,
2 ,
3 ,
c( 1 , 2 ) ,
c( 1 , 3 ) ,
c( 2 , 3 ) ,
c( 1 , 2 , 3 )
)
最佳答案
循环遍历值序列,得到combn
向量的 m
指定为序列值 split
通过 'col',将嵌套列表展平为 list
的vector
与do.call(c
和unname
它
unname(do.call(c, lapply(1:3, function(x) {
x1 <- combn(1:3, x)
split(x1, col(x1))})))
#[[1]]
#[1] 1
#[[2]]
#[1] 2
#[[3]]
#[1] 3
#[[4]]
#[1] 1 2
#[[5]]
#[1] 1 3
#[[6]]
#[1] 2 3
#[[7]]
#[1] 1 2 3
或者正如评论中提到的@IceCreamToucan
do.call(c, lapply(1:3, function(x) combn(1:3, x, simplify = FALSE)))
关于r - 如何将 `combn` 向量的每个元素的每个组合放入单个列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53671565/