在data.table中进行多次转换时,如何避免使用相同的列名? [英] How to avoid same column names when multiple transformations in data.table?

查看:68
本文介绍了在data.table中进行多次转换时,如何避免使用相同的列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图对 data.table 中的同一列进行多次转换,并发现此答案。但是,如果按照这些步骤操作,则会得到相同的列名(而不是 mean.Obs_1 等)。



< pre $ = lang-r prettyprint-override> 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(平均值= lapply(.SD,平均值),sd = lapply (.SD,sd)),by = 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

是有没有一种方法可以避免这种行为,并为不同的转换获取不同的列名?
我使用 data.table 的最新(1.9.4)稳定版本。

解决方案

您可以尝试

  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

或@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


I tried to make multiple transformations for the same columns in a data.table and found this answer. However, if I follow the steps there I get 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)), by = 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 a way to avoid this behavior and get different column names for different transformations? I use the latest (1.9.4) stable version of data.table.

解决方案

You could 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 a variation as suggested by @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

这篇关于在data.table中进行多次转换时,如何避免使用相同的列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆