I am trying to use the R by command to get the column value for subsets of a data frame. For example, consider this data frame:
> z = data.frame(labels=c("a","a","b","c","c"),data=matrix(1:20,nrow=5)) > z labels data.1 data.2 data.3 data.4 1 a 1 6 11 16 2 a 2 7 12 17 3 b 3 8 13 18 4 c 4 9 14 19 5 c 5 10 15 20
I can use the R by command to get the column value according to the labels column:
> by(z[,2:5],z$labels,colMeans) z[, 1]: a data.1 data.2 data.3 data.4 1.5 6.5 11.5 16.5 ------------------------------------------------------------ z[, 1]: b data.1 data.2 data.3 data.4 3 8 13 18 ------------------------------------------------------------ z[, 1]: c data.1 data.2 data.3 data.4 4.5 9.5 14.5 19.5
But how can I force the output back to the data frame? as.data.frame does not work ...
> as.data.frame(by(z[,2:5],z$labels,colMeans)) Error in as.data.frame.default(by(z[, 2:5], z$labels, colMeans)) : cannot coerce class '"by"' into a data.frame
Andrew
source share