I'd first set a key using setkey
and then use lapply
in the j
expression and self-join the result. You can use .SD
in lapply
and the associated .SDcols
to specify columns by numeric position. Like this:
setkey( data , id )
data[ data[ , lapply( .SD , mean ) , keyby = id , .SDcols = 2:4 ] ]
# id x1 x2 x3 x1.1 x2.1 x3.1
#1: a 1 6 11 2.0 7.0 12.0
#2: a 2 7 12 2.0 7.0 12.0
#3: a 3 8 13 2.0 7.0 12.0
#4: b 4 9 14 4.5 9.5 14.5
#5: b 5 10 15 4.5 9.5 14.5
# If you just want the group means use this:
data[ , lapply( .SD , mean ), by = id , .SDcols = 2:4 ]
Alternatively, you can use :=
along with by
as follows, which'll avoid the join as well:
sd_cols = c("x1", "x2", "x3")
data[, c(paste0("v", 1:3)) := lapply(.SD, mean), by=id, .SDcols=sd_cols]
# id x1 x2 x3 v1 v2 v3
# 1: a 1 6 11 2.0 7.0 12.0
# 2: a 2 7 12 2.0 7.0 12.0
# 3: a 3 8 13 2.0 7.0 12.0
# 4: b 4 9 14 4.5 9.5 14.5
# 5: b 5 10 15 4.5 9.5 14.5