问题
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