Dan Abner
2013-Oct-11 18:59 UTC
[R] Looping over names of multiple data frames in an R for() loop XXXX
Hi everybody, I thought I was using the get() fn correctly here to loop over multiple data frame names in an R for() loop. Can someone advise?> miss<-c("#NULL!","999") > d<-c("d1","d2","d3","d4") > > for(i in 1:4){+ + miss1<-ifelse(i<=2,miss[1],miss[2]) + miss1 + + get(d[i])<-read.csv(paste("C:\\DATA\\Data\\Original\\",dsn[i],sep=""), + na.strings=c(miss1,"999999999")) + + head(get(d[i])) + + write.csv(get(d[i]),paste("C:\\DATA\\Data\\",dsn[i],sep=""), + na=".") + + } Error in get(d[i]) <- read.csv(paste("C:\\DATA\\Data\\Original\\", dsn[i], : could not find function "get<-">Thanks! Dan [[alternative HTML version deleted]]
jim holtman
2013-Oct-11 19:11 UTC
[R] Looping over names of multiple data frames in an R for() loop XXXX
I think you want 'assign' at that point. Would suggest using a 'list' to store the input instead of unique named objects. 'list's are easier to manage. Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Fri, Oct 11, 2013 at 2:59 PM, Dan Abner <dan.abner99 at gmail.com> wrote:> Hi everybody, > > I thought I was using the get() fn correctly here to loop over multiple > data frame names in an R for() loop. Can someone advise? > > >> miss<-c("#NULL!","999") >> d<-c("d1","d2","d3","d4") >> >> for(i in 1:4){ > + > + miss1<-ifelse(i<=2,miss[1],miss[2]) > + miss1 > + > + get(d[i])<-read.csv(paste("C:\\DATA\\Data\\Original\\",dsn[i],sep=""), > + na.strings=c(miss1,"999999999")) > + > + head(get(d[i])) > + > + write.csv(get(d[i]),paste("C:\\DATA\\Data\\",dsn[i],sep=""), > + na=".") > + > + } > Error in get(d[i]) <- read.csv(paste("C:\\DATA\\Data\\Original\\", dsn[i], > : > could not find function "get<-" >> > > Thanks! > > Dan > > [[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.
Mikkel Grum
2013-Oct-13 22:27 UTC
[R] Looping over names of multiple data frames in an R for() loop XXXX
You might want to try: assign(d[1], read.csv("yourfile.csv")) ... write.csv(d1, "yourfile.csv", append = FALSE) Regards Mikkel On Friday, October 11, 2013 2:53 PM, Dan Abner <dan.abner99@gmail.com> wrote: Hi everybody, I thought I was using the get() fn correctly here to loop over multiple data frame names in an R for() loop. Can someone advise?> miss<-c("#NULL!","999") > d<-c("d1","d2","d3","d4") > > for(i in 1:4){+ + miss1<-ifelse(i<=2,miss[1],miss[2]) + miss1 + + get(d[i])<-read.csv(paste("C:\\DATA\\Data\\Original\\",dsn[i],sep=""), + na.strings=c(miss1,"999999999")) + + head(get(d[i])) + + write.csv(get(d[i]),paste("C:\\DATA\\Data\\",dsn[i],sep=""), + na=".") + + } Error in get(d[i]) <- read.csv(paste("C:\\DATA\\Data\\Original\\", dsn[i], : could not find function "get<-">Thanks! Dan [[alternative HTML version deleted]] ______________________________________________ R-help@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. [[alternative HTML version deleted]]