[R] Odp: Loop counter used in variable

Petr PIKAL petr.pikal at precheza.cz
Tue Dec 15 09:03:11 CET 2009


Hi

r-help-bounces at r-project.org napsal dne 15.12.2009 00:25:10:

> 
> Hi All,
> 
> I need to run muliple lm functions.  My independent variables are called
> dataset$x1, x2, x3, x4 etc.
> 
> How can I use a loop counter variable to replace the numbers?
> 
> 
> 
> fit1=lm(dataset$y~dataset$x1)
> fit2=lm(dataset$y~dataset$x2)
> fit3=lm(dataset$y~dataset$x3)
> fit4=lm(dataset$y~dataset$x4)

I would use list for results and brackets for lm fit. Something like 
(untested)

fit<- vector("list", length=4)

for (i in 1:4) {
fit[[i]] <- lm(y~dataset[,i], data=dataset)
}

Regards
Petr




> 
> Thanks,
> Tom
> -- 
> View this message in context: http://n4.nabble.com/Loop-counter-used-in-
> variable-tp963984p963984.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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.




More information about the R-help mailing list