Skip to content

Selecting variables from a list of data.frames

2 messages · Magnus, Ben Bolker

#
I have a simulation program that generates a data frame for each run. I 
aggregate the data.frames into a list (df.list).  The structure of all data 
frames is the same, only the values are different.

I then want to aggregate the various runs. Currently I use the following 
method (for three runs):

means = (df.list[[1]]$variable + df.list[[2]]$variable + 
df.list[[3]]$variable)/3

I would like to do this in a more parsimonious way, for example using lapply 
or related commands, but I can't seem to figure out the magic touch. Any 
thoughts on the best way to accomplish this?

Thanks and regards,
Magnus
#
Magnus <account <at> zulutime.net> writes:
x <- sapply(df.list,"[[","variable")  

will extract the variables as a matrix.
If the variables are all vectors of the same
length then you might follow this with
rowSums(x)/ncol(x) or apply(x,1,mean)