r - How to avoid same column names when multiple transformations in data.table? -
i tried make multiple transformations same columns in data.table , found this answer. however, if follow steps there identical column names (instead of mean.obs_1, etc.).
library(data.table) set.seed(1) dt = data.table(id=c(1:3), obs_1=rnorm(9), obs_2=rnorm(9), obs_3=rnorm(9)) dt[, c(mean = lapply(.sd, mean), sd = lapply(.sd, sd)), = id] # id obs_1 obs_2 obs_3 obs_1 obs_2 obs_3 #1: 1 0.4854187 -0.3238542 0.7410611 1.1108687 0.2885969 0.1067961 #2: 2 0.4171586 -0.2397030 0.2041125 0.2875411 1.8732682 0.3438338 #3: 3 -0.3601052 0.8195368 -0.4087233 0.8105370 0.3829833 1.4705692 is there way avoid behavior , different column names different transformations? use latest (1.9.4) stable version of data.table.
you try
library(data.table) dt[, unlist(lapply(.sd, function(x) list(mean=mean(x), sd=sd(x))),recursive=false), by=id] # id obs_1.mean obs_1.sd obs_2.mean obs_2.sd obs_3.mean obs_3.sd #1: 1 0.4854187 1.1108687 -0.3238542 0.2885969 0.7410611 0.1067961 #2: 2 0.4171586 0.2875411 -0.2397030 1.8732682 0.2041125 0.3438338 #3: 3 -0.3601052 0.8105370 0.8195368 0.3829833 -0.4087233 1.4705692 or variation suggested @david arenburg
dt[, as.list(unlist(lapply(.sd, function(x) list(mean=mean(x), sd=sd(x))))), by=id] # id obs_1.mean obs_1.sd obs_2.mean obs_2.sd obs_3.mean obs_3.sd #1: 1 0.4854187 1.1108687 -0.3238542 0.2885969 0.7410611 0.1067961 #2: 2 0.4171586 0.2875411 -0.2397030 1.8732682 0.2041125 0.3438338 #3: 3 -0.3601052 0.8105370 0.8195368 0.3829833 -0.4087233 1.4705692
Comments
Post a Comment