Re: [R] Looping through a list of objects & do something...

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Tue, 19 Feb 2008 14:08:16 -0500

On 2/19/2008 1:51 PM, 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);
> }
>
> Can this be done in R?

Yes, easily:

dfarray <- list(df1, df2, df3)
lapply(dfarray, function(i) plot(i$x, i$y))

Duncan Murdoch



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 19 Feb 2008 - 19:14:57 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 19 Feb 2008 - 19:30:15 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