I would just use meltand dcastfrom "reshape2":
library(reshape2)
dfL <- melt(table2, id.vars = c("ID", "occ"))
dcast(dfL, variable ~ value, value.var = "occ", fun.aggregate = mean)
# variable 0 1
# 1 c_Al 2057.100 1032.778
# 2 c_D 1596.667 1529.429
# 3 c_Hy 1509.500 1641.222
Of course, the R base can handle this just fine.
Here I used tapplyand vapply:
vapply(table2[2:4], function(x) tapply(table2$occ, x, mean), numeric(2L))
t(vapply(table2[2:4], function(x) tapply(table2$occ, x, mean), numeric(2L)))
source
share