Skip to content
Prev 56362 / 398500 Next

An index of all possible combinations of variables in a datafram e

Alternatively you can use the combinations() function in gregmisc.

library(gregmisc)
n <- 3
sapply(1:n, function(x) apply(combinations(n, x, LETTERS[1:n]), 1, 
paste, collapse="") )

Here is the same code written with a for loop
n <- 3
out <- NULL
for(i in 1:n){
  tmp <- combinations(n, i, LETTERS[1:n])
  out <- c(out, apply(tmp, 1, paste, collapse=""))
}
out
[1] "A"   "B"   "C"   "AB"  "AC"  "BC"  "ABC"
On Tue, 2004-09-28 at 08:38, Dimitris Rizopoulos wrote: