将字符串与R中的combn列表组合

将字符串与R中的combn列表组合,r,R,我想循环使用combn()创建的组合 输入: "a" "b" "c" "d" 期望输出: [1] "a" "b" "c" "d" [1] "a and b" "a and c" "a and d" "b and c" "b and d" "c and d" [1] "a and b and c" "a and b and d" "a and c and d" "b and c and d" [1] "a and b and c and d" 我的尝试: classes <- lette

我想循环使用
combn()
创建的组合

输入:

"a" "b" "c" "d"
期望输出:

[1] "a" "b" "c" "d"
[1] "a and b" "a and c" "a and d" "b and c" "b and d" "c and d"
[1] "a and b and c" "a and b and d" "a and c and d" "b and c and d"
[1] "a and b and c and d"
我的尝试:

classes <- letters[1:4]
cl <- lapply(1:length(classes), combn, x = classes)
apply(cl[[1]], 2, paste, collapse = " and ")
apply(cl[[2]], 2, paste, collapse = " and ")
apply(cl[[3]], 2, paste, collapse = " and ")
apply(cl[[4]], 2, paste, collapse = " and ")

classes您可以迭代向量的长度,并使用
combn()
的函数参数使用
paste()
折叠输出:


vec Bingo,
combn()
有一个经常被忽略的
FUN=
参数,它适用于每个组合。这就是我想写的。从未注意到函数参数。谢谢!
vec <- letters[1:4]

lapply(seq_along(vec), function(x) combn(vec, x, FUN = paste, collapse = " and "))

[[1]]
[1] "a" "b" "c" "d"

[[2]]
[1] "a and b" "a and c" "a and d" "b and c" "b and d" "c and d"

[[3]]
[1] "a and b and c" "a and b and d" "a and c and d" "b and c and d"

[[4]]
[1] "a and b and c and d"