My data.frame
df <- data.frame(ID=rep(1:3, 3), Obs_1=rnorm(9), Obs_2=rnorm(9), Obs_3=rnorm(9))
I want to calculate the mean of each colum by the ID. I have tried two approaches: 1)
ag <- aggregate(. ~ ID, df, function(x) c(mean = mean(x)))
returns
ID Obs_1 Obs_2 Obs_3
1 1 0.41220831 -0.9999704 -0.7234958
2 2 0.03564336 0.5014259 0.4847635
3 3 0.05647885 0.2067311 -0.0542368
Why does it does not indicate "mean" in the column name and how can make this reported? something like this:
Obs_1.mean Obs_2.mean Obs_3.mean
2) the second approach
df[, c(mean = lapply(.SD, mean), sd = lapply(.SD, sd)), by = ID]
it gives
unused argument (by = ID)
Thank you