like the question , I want to assign the column names of which were processed by lapply and tapply in R. A simple example:
df<-data.frame('X1'=rnorm(100),
'X2'=rnorm(100),
'X3'=c(c(rep('A',50)),c(rep('B',50))))
var<-c('X1','X2')
plyr::ldply(lapply(var, function(v) {
tapply(df[,v],df$X3,mean)
}),rbind)
which will result as :
A B
1 -0.06856352 0.08608197
2 -0.23585510 0.01551267
from which I could not tell whether row 1 is from 'X1' or 'X2'. What I want is :
A B
X1 -0.06856352 0.08608197
X2 -0.23585510 0.01551267
although we could do a simply manual check in this example and a bold guess that row 1 is from 'X1', however this will become tedious and risky when there are lots more variables and function much more complex than mean.
Anyone know how to achieve this? your time and knowledge would be deeply appreciated. Thanks in advance.