[R] Accomplishing a loop on multiple columns

Nerak nerak.t at hotmail.com
Wed Jan 11 11:22:16 CET 2012


Hello,
I have a question concerning ‘for loops’ on multiple columns. 
I made 91 columns with results (all made together with a for loop) and I
want to us lm to fit the model.
I want to compare the results of all these calculated columns (91) with one
column with observed values. I use the function lm to fit the model and
calculate r.squared. I manage to do this for each column separately:

For example: my calculated results are in the dataframe ‘results6”, my
observed results in data, (data$observed). 
#To calculate R2 for column 1:
lm.modelobs1 <- lm(results6[,c(1)] ~ data$observed)
R2.1 <- summary(lm.modelobs1)["r.squared"]
#To calculate R2 for column 91:
lm.modelobs91 <- lm(results6[,c(91)] ~ data$observed)
R2.91 <- summary(lm.modelobs91)["r.squared"]

But I think there has to be a method to do this automatically and not 91
times.
I tried to use a for loop:
###(length(C) = 91)
results7<-data.frame(lm.modelobs=rep(NA,length(C)))
for (i in (1:91))
{
results7$lm.modelobs[i] <- lm(results6[i] ~ data$observed)
R2.[i] <- summary(lm.modelobs[i])["r.squared"]
}

I also tried just to calculate results7$lm.modelobs[i] without directly
calculating r.squared but I also didn’t manage. It seems like it’s not
possible to use the referral to a column in a for loop or a function. (if I
just ask R the data in column 5 with ‘ results6[5]  ’, that works. ‘
results6[,c(5)]’ gives the same but replacing results6[i] by
results6[,c([i])] in the for loop is apparently also no a solution).  I’m
looking for a manner to repeat a calculation/function on several columns. I
kind of need this as well further in my script, not only in this part…

I would greatly appreciate any suggestions!
Thanks!
Nerak


--
View this message in context: http://r.789695.n4.nabble.com/Accomplishing-a-loop-on-multiple-columns-tp4284974p4284974.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list