简体   繁体   English

R:data.table组和求和两列

[英]R: data.table group and sum two columns

Ok, I am stuck with trying to use data.table package to group and sum two separate columns. 好的,我坚持尝试使用data.table包对两个单独的列进行分组和求和。

My DT: 我的DT:

PARK   WTG    T_stop    T_AF
PC    81970   4.743     4.742
PC    81970   48.850    47.462
PC    81970   16.714    0.000
PC    81970   0.121     0.115
PC    81970   0.004     0.004
PC    81970   0.015     0.000
PC    81970   0.049     0.046
PC    81970   0.090     0.090
PC    81970   0.060     0.060
PC    81970   0.163     0.152

Now using the data.table function I have tried: 现在,我尝试使用data.table函数:

DT[, lapply(.SD, sum), by = c("PARK", "WTG")]

but my results look like this: 但是我的结果看起来像这样:

PARK   WTG    T_stop    T_AF
PC    81970   70.808    5.267

What I am looking for is: 我正在寻找的是:

PARK   WTG    T_stop    T_AF
PC    81970   70.808    52.671

Now this is just a snippet of my data set with around 700 rows 现在这只是我的数据集的大约700行的摘要

From your comments it seems like you are getting the desired result but in scientific notation. 从您的评论看来,您似乎正在获得期望的结果,但使用的是科学计数法。 Try rounding to 3 decimals if you want to see it as you say: 如果您想看到的话,尝试四舍五入到小数点后3位:

DT[, lapply(.SD, function(x) round(sum(x), 3)), by = c("PARK", "WTG")]

使用plyr软件包,您可以尝试以下操作:

DT_sum <- ddply(DT, .("PARK","WTG"), colSums)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM