Kaiyin Zhong
2011-Nov-23 15:34 UTC
[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 + }> df1w 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]]
Jeff Newmiller
2011-Nov-23 16:06 UTC
[R] Is there an easier way to iterate over multiple data frames in R?
Whenever similar objects are to be handled with similar code, having the data frames stored in lists or even as one big data frame is preferred. If you can load them as such, half the complexity is addressed right there. The for loop processing is usually wrapped up using base apply functions or "plyr" package functions. Those idioms are not necessarily faster than for loops, but they can wrap up some common split and assemble steps cleanly. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Kaiyin Zhong <kindlychung at gmail.com> wrote:>> 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 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.
Mark Lamias
2011-Nov-23 16:17 UTC
[R] Is there an easier way to iterate over multiple data frames in R?
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 <kindlychung@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 + }> df1w 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]]