[R] looping through data frames in a workspace

From: lucy b <lucy.lists_at_gmail.com>
Date: Mon, 03 Mar 2008 17:28:05 -0500


I have a workspace containing only data frame objects. I would like to loop though each one and clean-up text columns in them. How can I have R loop through the list? I have tried to find an answer in R help but the closest solution I can find is to make a static list of data frames, as illustrated in this recent post:

---------begin post
On Tuesday 19 February 2008 (19:51:15), TLowe wrote:
> Hey Folks,
> Could somebody show me how to loop through a list of dataframes? I want to
> be able to generically access their elements and do something with them.
> For instance, instead of this:
> df1<- data.frame(x=(1:5),y=(1:5));
> df2<- data.frame(x=(1:5),y=(1:5));
> df3<- data.frame(x=(1:5),y=(1:5));
> plot(df1$x,df1$y);
> plot(df2$x,df2$y);
> plot(df3$x,df3$y);
> I would like to do something like:
> (pseudocode)
> dfarray[1] = df1
> dfarray[2] = df2
> dfarray[3] = df3
> for (each i in dfarray) {
> plot(i$x, i$y);
> }

It's surprisingly simple:

for(df in list(df1,df2,df3)) plot(df$x,df$y)



---------------------end post

I would like to avoid having to type-out a very long list over and over again. I have tried every variation I could think of similar to:

for(df in list(noquote(ls()))) {

     do stuff with df


I know this has to be possible. What am I missing?

Many Thanks-

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 Mon 03 Mar 2008 - 22:46:08 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 - 10:30:18 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