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

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Tue Sep 28 12:54:01 CEST 2004


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:
> Hi Alan,
> 
> you could also try the following function which has been submitted to 
> s-news some time ago:
> 
> powerSet <- function(x){
>  if(length(x)==0) return(vector(mode(x), 0))
>  x <- sort(unique(x))
>  K <- NULL
>  for(m in x) K <- rbind(cbind(K, FALSE), cbind(K, TRUE))
>  out <- apply(K, 1, function(x, s) s[x], s=x)[-1]
>  names(out) <- NULL
>  return(out)
> }
> 
> powerSet(1:5)
> 
> I hope it helps.
> 
> Best,
> Dimitris
> 
> ----
> Dimitris Rizopoulos
> Ph.D. Student
> Biostatistical Centre
> School of Public Health
> Catholic University of Leuven
> 
> Address: Kapucijnenvoer 35, Leuven, Belgium
> Tel: +32/16/396887
> Fax: +32/16/337015
> Web: http://www.med.kuleuven.ac.be/biostat/
>      http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
> 
> 
> ----- Original Message ----- 
> From: "Alan Simpson" <alan.simpson at robertsresearch.com.au>
> To: <r-help at stat.math.ethz.ch>
> Sent: Tuesday, September 28, 2004 6:51 AM
> Subject: [R] An index of all possible combinations of variables in a 
> datafram e
> 
> 
> > 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?
> >
> > Any help would be greatly appreciated.
> >
> > Regards
> >
> > Alan Simpson
> > Roberts Research Group
> >
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! 
> > http://www.R-project.org/posting-guide.html
> >
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list