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

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


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


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)))


If I understand the question correctly, heres something to do with plyr:

dats <- list( df1 = data.frame(a=sample(1:3), b = sample(11:13)),              df2 = data.frame(a=sample(1:3), b = sample(11:13)))library(plyr)xlim <- adply(do.call("rbind",dats),2,function(x)c(min(x),max(x)))    names(xlim)=c("xlab","min","max")xlim  xlab min max1    a   1   32    b  11  13

Gives per variable the minimum and maximum collapsed over all data frames in the list.

EDIT: shortened the code abit. I do assume that each dataframe contains the same number of columns in the same order.