library(data.table)
library(dplyr)
N <- 100
DT <- data.table(
id = sample(5, N, TRUE),
v1 = sample(5, N, TRUE),
v2 = sample(1e6, N, TRUE)
)
setmutate_each(DT, funs(mean, mean_na = mean(., na.rm = TRUE)), v2, i = id ==1, by = v1)
setmutate_each(DT, funs(as.character), replace = TRUE)
Run the code above in your browser using DataLab