2

I failed at searching for how to do this specific transformation. Does anyone have a smart way to achieve this? sorry if I missed an obvious answer somewhere. thanks

# 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 )
)
4

1 回答 1

2

循环遍历值序列,通过'col'获取指定为序列值combn的向量,将嵌套列表展平为a of s with and itmsplitlistvectordo.call(cunname

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)))
于 2018-12-07T14:33:03.380 回答