Liaw, Andy
2004-Sep-28 12:25 UTC
[R] An index of all possible combinations of variables in a d ata frame
This might be a bit of an abuse of the functions, but it seems to work:> dat <- data.frame(A=1, B=2, C=3) > f <- ~ .^3 > colnames(attr(terms(f, data=dat), "factors"))[1] "A" "B" "C" "A:B" "A:C" "B:C" "A:B:C" Cheers, Andy> From: Alan Simpson > > 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 > >
Gabor Grothendieck
2004-Sep-28 12:44 UTC
[R] An index of all possible combinations of variables in a d ata frame
And here is a variation on your idea of using the factors attribute of terms with some minor reductions, this one returning a list of numeric vectors: fac <- attributes(terms(~ a * b * c))$factors apply(fac == 1, 2, which) Liaw, Andy <andy_liaw <at> merck.com> writes: : : This might be a bit of an abuse of the functions, but it seems to work: : : > dat <- data.frame(A=1, B=2, C=3) : > f <- ~ .^3 : > colnames(attr(terms(f, data=dat), "factors")) : [1] "A" "B" "C" "A:B" "A:C" "B:C" "A:B:C" : : Cheers, : Andy : : > From: Alan Simpson : > : > 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 : :