[R] looping through data frames in a workspace

From: lucy b <lucy.lists_at_gmail.com>
Date: Tue, 04 Mar 2008 15:01:33 -0500


Thanks for helpful replies. Unfortunately, I didn't mention that I want to overwrite the existing data frame with the corrected one using the same name. Here's what I have:

## get names of data frames
frames <- names(Filter(function(x) x=="data.frame",

                   sapply(objects(),function(x)
                          class(eval(parse(text=x))))))
## loop through list
for( i in seq(along=frames) ) {

#don't need next line, but good to remember if need to exclude a df
#if (frames[i] != "junk") {

        df <- eval(parse(text=frames[i]))

	for (c in 1:ncol(df)){
		if (is.factor(df[,c])) {
			df[,c] <- gsub("\r\n","",as.vector(df[,c]))
			}
		}

	names(df) <- frames[i]

	}#end first if

}#end first for
#END Unfortunately, the "names(df)<-frames[i]" line just changes the name of the first column in df. How can I overwrite the existing data frame using the right name?



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. Received on Tue 04 Mar 2008 - 20:04:26 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 04 Mar 2008 - 21:30:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive