从矢量生成R中所有长度的所有组合

时间:2016-10-14 17:56:06

标签: r combinations

假设我有向量["a","b","c"],那么我想创建所有唯一组合的向量列表,所有大小(顺序无关紧要):

["a"]
["b"]
["c"]
["a","b"]
["a","c"]
["b","c"]
["a","b","c"]

我怎样才能在R?

中这样做

1 个答案:

答案 0 :(得分:3)

我们可以尝试combn

do.call("c", lapply(seq_along(v1), function(i) combn(v1, i, FUN = list)))

数据

v1 <- letters[1:3]