0
votes

Ok, I am stuck with trying to use data.table package to group and sum two separate columns.

My 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:

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

2
Is data.table compulsory for you?T. Ciffréo
No, I just like using that package, but maybe a different approach might be suited.BB.squared
If I try your code on the example data, I get your expected result.phiver
Like this? DT[, lapply(.SD,sum), by = .(PARK, WTG)]Mako212
@phiver yes, but when run it over my complete DT with 700 rows, the T_AF is always 5.267 not 52.671BB.squared

2 Answers

1
votes

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:

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

With package plyr you can try this:

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