Select all possible tuples from a vector in R

岁酱吖の 提交于 2019-12-10 19:34:03

问题


I'm trying to write a program in R that when, given a vector, will return all possible tuples of elements from that vector.

For example: tuples(c('a','b','c')) = c('a','b','c'); c('a','b'); c('a','c'), c('b','c'); c('a'); c('b'); c('c')

I think it should return a list of vectors.

For reference, here is a program that does a similar function in Stata.


回答1:


You can use combn:

x <- 1:3
unlist(lapply(x, function(n) combn(x, n, simplify=FALSE)), recursive=FALSE)


来源:https://stackoverflow.com/questions/5240466/select-all-possible-tuples-from-a-vector-in-r

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