Alan Simpson
2004-Sep-28 04:51 UTC
[R] An index of all possible combinations of variables in a data fram 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]]
Gabor Grothendieck
2004-Sep-28 05:13 UTC
[R] An index of all possible combinations of variables in a data fram e
Alan Simpson <alan.simpson <at> robertsresearch.com.au> writes: : 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? : Is this what you are looking for? R> attributes(terms(~ a * b * c))$term.labels [1] "a" "b" "c" "a:b" "a:c" "b:c" "a:b:c"
Dimitris Rizopoulos
2004-Sep-28 07:38 UTC
[R] An index of all possible combinations of variables in a datafram e
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
>
Adaikalavan Ramasamy
2004-Sep-28 10:54 UTC
[R] 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:> 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
>
Chris Jackson
2004-Sep-28 13:18 UTC
[R] An index of all possible combinations of variables in a data fram e
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