I have a huge list with data.frames (same number of columns, different number of rows).
I succeeded to use apply – instead of the for loops I learned to avoid – to create a mean value over specific columns in each list element with
t2<-lapply(t1, function(x) cbind(x,rowMeans(x[,c("ColumnX","ColumnY","ColumnZ")])))
The problem I am stuck with now is the new columns name. It is “rowMeans(x[,c(“ColumnX”,”ColumnY”,”ColumnZ”)])”.
How can I change this for all list elements? My poor “lapply”-knowledge was not sufficient for this task.
There’s two ways to do this, and it actually has to do with the
cbindfunction and not thelapplyfunction:Or after you’ve cbind’ed:
That’s obviously the long way, but it useful for if you forget to name something during the
applyorcbindprocess.