I'm using a function smean.cl.normal
from Hmisc package that returns a vector with 3 values: the mean and the lower and upper CI. When I use it on a data.table
with 2 groups, I obtain 2 columns and 6 rows. Is there a way to obtain the result with two rows corresponding to 2 groups and separate columns for each of function's outputs, i.e. the mean and CIs?
require(Hmisc)
require(data.table)
dt = data.table(x = rnorm(100),
gr = rep(c('A', 'B'), each = 50))
dt[, lapply(.SD, smean.cl.normal), by = gr, .SDcols = "x"]
The output:
gr x
1: A -0.07916335
2: A -0.33656667
3: A 0.17823998
4: B -0.02745333
5: B -0.32950607
6: B 0.27459941
The desired output:
gr Mean Lower Upper
1: A -0.07916335 -0.33656667 0.17823998
2: B -0.02745333 -0.32950607 0.27459941