Depending upon what you are trying to do with them, how about storing the 
dataframes as a multidimensional array of size 4x3x3 in the case below.  After 
that  you can use the sapply function with the 
appropriate MARGIN argument specified:

For example


mydfs <- as.array(list(df1, df2, df3))

sapply(X=mydfs, MARGIN=c(1,2), FUN=mean)


         [,1]        [,2]       [,3]
V1  0.8206239 -0.51637848 -0.9025252
V2 -0.2828919  0.41837384 -0.4042646
V3 -0.8996891 -0.06284681 -0.2459261
--Mark Lamias


________________________________
 From: Kaiyin Zhong <kindlych...@gmail.com>
To: r-help <r-help@r-project.org> 
Sent: Wednesday, November 23, 2011 10:34 AM
Subject: [R] Is there an easier way to iterate over multiple data frames in R?

> for (d in paste('df', 1:3, sep='')) {
+  assign(d, as.data.frame(replicate(3, rnorm(4))))
+ }
> dats = list(df1,df2,df3)
> for (i in 1:length(dats)) {
+  names(dats[[i]]) = c('w', 'l', 'h')
+ }
> dats
[[1]]
            w           l           h
1  1.24319239 -0.05543649  0.05409178
2  0.05124331 -1.89346950  0.33896273
3 -1.69686777 -0.35963008 -0.91720034
4  1.30786112 -0.23953238  0.94139356

[[2]]
          w           l          h
1 -1.238519 -0.12352187 -1.2577607
2  1.180469  2.38836107  2.9139199
3  1.494369 -0.07088712  0.2372746
4  1.942535  1.47911615  1.1431675

[[3]]
           w          l          h
1  1.0198692 -1.4222194  1.9486072
2  0.3057461  1.7630326 -0.6501801
3 -0.5576854 -1.1637263 -0.1782680
4  0.6625268  0.6913202  0.9588915
> i = 1
> for (n in paste('df', 1:3, sep='')) {
+  assign(n, dats[[i]])
+  i = i+1
+ }
> df1
            w           l           h
1  1.24319239 -0.05543649  0.05409178
2  0.05124331 -1.89346950  0.33896273
3 -1.69686777 -0.35963008 -0.91720034
4  1.30786112 -0.23953238  0.94139356

    [[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]]

______________________________________________
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.

Reply via email to