[R] Selecting variables from a list of data.frames
Ben Bolker
bolker at ufl.edu
Tue Jan 6 15:25:46 CET 2009
Magnus <account <at> zulutime.net> writes:
>
> 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?
>
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)
More information about the R-help
mailing list