[R] Accomplishing a loop on multiple columns

iliketurtles isaacm200 at gmail.com
Wed Jan 11 12:39:38 CET 2012


Lists are the answer.


LIST<-list()
 for(i in 1:ncol(results6))
 {
  LIST[[i]]<-lm(results6[,i]~data$observed)
 }

You'll now have a 91 entry list of lm(). You can then do something like
this:

LIST2<-list()
 for(i in 1:length(LIST))
 {
  LIST2[[i]]<-LIST[[i]]$r.squared
 }

This should now be a list of 91 R-squared, which you can unlist() and save
in matrix form if you want. 

-----
----

Isaac
Research Assistant
Quantitative Finance Faculty, UTS
--
View this message in context: http://r.789695.n4.nabble.com/Accomplishing-a-loop-on-multiple-columns-tp4284974p4285136.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list