Hello, I'd like to generate automatically all the possible combinations of a set of 8 variables (there are 535, too many to do it by hand). For example: input: varA, varB, varC output: varA+varB+varC varA+varB varA+varC varB+varC varA varB varC Is there any function that produces this option? Thank you [[alternative HTML version deleted]]
one option is the following:
varNames <- c("varA", "varB", "varC",
"varD")
f <- function (i) {
combn(length(varNames), i,
function (x) paste(varNames[x], collapse = " + "))
}
lapply(seq_along(varNames), f)
However, in case you're interested in performing a linear regression
with these subsets, then have also a look at package leaps
(http://CRAN.R-project.org/package=leaps).
Best,
Dimitris
On 9/12/2011 11:20 AM, Santiago Guallar wrote:> Hello,
>
> I'd like to generate automatically all the possible combinations of a
set of 8 variables (there are 535, too many to do it by hand). For example:
>
> input: varA, varB, varC
> output: varA+varB+varC
> varA+varB
> varA+varC
> varB+varC
> varA
> varB
> varC
> Is there any function that produces this option?
>
> Thank you
> [[alternative HTML version deleted]]
>
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center
Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/
Try this> ltr<-LETTERS[1:3] > unique(apply(expand.grid(ltr,ltr,ltr),1,function(x) paste("Var",unique(sort(x)),collapse="+",sep="")))[1] "VarA" "VarA+VarB" "VarA+VarC" "VarA+VarB+VarC" "VarB" "VarB+VarC" "VarC">Andrej -- Andrej Blejec National Institute of Biology Vecna pot 111 POB 141, SI-1000 Ljubljana, SLOVENIA e-mail: andrej.blejec at nib.si URL: http://ablejec.nib.si tel: + 386 (0)59 232 789 fax: + 386 1 241 29 80 -------------------------- Organizer: Applied Statistics 2011 conference http://conferences.nib.si/AS2011> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On > Behalf Of Santiago Guallar > Sent: Monday, September 12, 2011 2:45 PM > To: r-help at r-project.org > Subject: [R] Automated generation of combinations > > Hello, > > I'd like to generate automatically all the possible combinations of a set of > 8 variables (there are 535, too many to do it by hand). For example: > > input: varA, varB, varC > output: varA+varB+varC > varA+varB > varA+varC > varB+varC > varA > varB > varC > Is there any function that produces this option? > > Thank you > [[alternative HTML version deleted]]