I am trying to create a summary data table from an existing data.table in R. For this purpose rather than looping across all variables, i am trying to leverage data.table ":=" function in R
raw_df <- cbind.data.frame(1:4,7:10,15:18)
names(raw_df) <- c('A','B','C')
raw_df$sample <- 1
What i want is the mean and quantile of each columns in a new data.table. below is the code i am using but this only updates the existing table. How can i create a new data.table without writing each of the variable names in the code.
setDT(raw_df)[, c("Unique_Count","Mean",paste("Quantile_",seq(0,1,0.05),sep = ""))
:= lapply(.SD, function(x) c(length(unique(x)),mean(x,na.rm = T),quantile(x,probs = seq(0,1,0.05),na.rm = T))),
by = sample]
Thanks in anticipation!