Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all possible tuples from a vector in R

Tags:

r

tuples

set

stata

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.

like image 974
Zach Avatar asked Sep 01 '25 23:09

Zach


1 Answers

You can use combn:

x <- 1:3
unlist(lapply(x, function(n) combn(x, n, simplify=FALSE)), recursive=FALSE)
like image 172
Hong Ooi Avatar answered Sep 03 '25 14:09

Hong Ooi