[R] Loops (run the same function per different columns)

arun smartpink111 at yahoo.com
Mon Apr 21 15:19:49 CEST 2014


Hi,

Using the example data from library(gvlma)

library(gvlma)
data(CarMileageData)
CarMileageNew <- CarMileageData[,c(5,6,3)]
 lst1 <- list()
 y <- c("NumGallons", "NumDaysBetw")
 for(i in seq_along(y)){
 lst1[[i]] <- gvlma(lm(get(y[i])~MilesLastFill,data=CarMileageNew))
 lst1}
pdf("gvlmaplot.pdf")
 lapply(lst1,plot)
dev.off()


You could also use ?lapply().


A.K.



Hi
I have a spread sheet with a column Samples (column1) and then 34 more columns with different concentrations of fatty acids per sample. Im trying to run the same function 34 times. In this case (the first of 34), I have a fatty acid called C14.0 (column 2). I'm a newbie with R so I spent the last 4 days looking for a way of doing it (without running the same function 34 times with a different fatty acid each time). I saw that people do similar things with loops but I cannot get them to work.
I have tried the script below but it does not work.

y<-c("C14.0","C15.0","C16.0")
for (i in y) {
FA.ml=lm(i~Sample,data=FA)
gvlmaFA<-gvlma(FA.ml)
gvlmaFA
}


I really appreciate if someone can give me a hand with that. I know would have been finished if I had typed the 34 fatty acids but I want to learn how to do it with loops.
Cheers 





More information about the R-help mailing list