Generate all combinations, of all lengths, in R, from a vector [duplicate]

醉酒当歌 提交于 2019-12-25 04:16:05

问题


Suppose I have the vector ["a","b","c"], then I'd like to create a list of vectors of all the unique combinations, of all sizes (order does not matter):

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

How can I do this in R?


回答1:


We can try with combn

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

data

v1 <- letters[1:3]


来源:https://stackoverflow.com/questions/40049313/generate-all-combinations-of-all-lengths-in-r-from-a-vector

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!