Here is the data.table solution
library(data.table) AgeAnalyisis.DT <- data.table(AgeAnalyisis) AgeAnalyisis.DT[, lapply(.SD[, !"Age", with=FALSE], function(x) mean(Age[x])) , by=Gender] Gender Gout Arthritis Vasculitis 1: F 54.58333 52.00000 55.81818 2: M 50.09091 52.69231 52.40000
If you want it to be transposed, you can use:
# Save the results res <- AgeAnalyisis.DT[, lapply(.SD[, !"Age", with=FALSE], function(x) mean(Age[x])) , by=Gender]
source share