[R] An index of all possible combinations of variables in a data fram e

Chris Jackson chris.jackson at imperial.ac.uk
Tue Sep 28 15:18:39 CEST 2004


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 <chris.jackson at imperial.ac.uk>, Research Associate,
Department of Epidemiology and Public Health, Imperial College
School of Medicine, Norfolk Place, London W2 1PG, tel. 020 759 43371




More information about the R-help mailing list