Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over a list of data-frames in R

I have a list in R that contains several data frames. I want to iterate over the data frames and calculate the min/max of a value from the data frame. Here's the code that I have right now:

firstname = names(dats)[1]
xlim = c( min( dats[[firstname]][,xlab] ), max( dats[[firstname]][,xlab] ) )
for ( name in names(dats) ) {
   xlim = c( min(xlim[1],dats[[name]][,xlab]), max(xlim[2],dats[[name]][,xlab]) )
} 

This seems ugly to me, as it requires a lot of code to do something very simple. Is there a more canonical way to do this in R?

like image 721
James Thompson Avatar asked Feb 19 '11 20:02

James Thompson


2 Answers

You can use lapply to extract the xlab column out of all the data-frames, and unlist to combine into one vector, then take the min or max:

xlab <- 'a'
dats <- list( df1 = data.frame(a=sample(1:3), b = sample(11:13)),
              df2 = data.frame(a=sample(1:3), b = sample(11:13)))

>  min( unlist( lapply( dats, '[', xlab ) ) )
[1] 1
>  max( unlist( lapply( dats, '[', xlab ) ) )
[1] 3
like image 191
Prasad Chalasani Avatar answered Sep 30 '22 05:09

Prasad Chalasani


Can you combine the data frames from the list of data frames into one data frame? I would use the plyr package and rbind.fill, which would allow the data frames to have mismatched columns as long as the column of interest is named the same in all data frames.

library(plyr)
df.orig <- data.frame(one = rep(1:4, each = 4), two = 1:16)
df.list <- dlply(df.orig, "one")
df.new <- rbind.fill(df.list)
xlim <- with(df.new, c(min(two), max(two)))
like image 44
Richard Herron Avatar answered Sep 30 '22 06:09

Richard Herron