Yet another solution might be to abuse expand.grid:
dat <- expand.grid(rep(list(c(FALSE,TRUE)), 3)) apply(dat, 1, function(x)which(x))
This gives you a list of the numeric column indices, including an empty component at the beginning corresponding to none of the variables present.
Alan Simpson wrote:
Hello list
Does anybody know of any way to create an index of all the possible
combinations of variables (factors) in a data frame? ie for 3 factors A, B &
C we have A
B
C
AB
AC
BC
ABC
which equates to columns 1, 2, 3, 1:2, (1,3), 2:3 and 1:3.
I realise that a function like model.matrix does this, but how to get the
seqence of the index?
-- Christopher Jackson <[EMAIL PROTECTED]>, Research Associate, Department of Epidemiology and Public Health, Imperial College School of Medicine, Norfolk Place, London W2 1PG, tel. 020 759 43371
______________________________________________ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
