If I have a list of data frames in R, such as:
x<-c(1:10)
y<-2*x
z<-3*x
df.list <- list(data.frame(x),data.frame(y),data.frame(z))
And I'd like to average over a specific column (this is a simplified example) of all these data frames, is there any easy way to do it?
The length of the list is known but dynamic (i.e. it can change depending on run conditions).
For example:
dfone<-data.frame(c(1:10))
dftwo<-data.frame(c(11:20))
dfthree<-data.frame(c(21:30))
(Assume all the column names are val
)
row, output
1, (1+11+21)/3 = 11
2, (2+12+22)/3 = 12
3, (3+13+23)/3 = 13
etc
So output[i,1] = (dfone[i,1]+dftwo[i,1]+dfthree[i,1])/3
To do this in a for loop would be trivial:
for (i in 1:length(dfone))
{
dfoutput[i,'value']=(dfone[i,'value']+dftwo[i,'value']+dfthree[i,'value'])/3
}
But I'm sure there must be a more elegant way?