0

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

BB.squared
  • 113
  • 13

2 Answers2

1

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")]
ebeneditos
  • 2,542
  • 1
  • 15
  • 35
  • 1
    I figured out my issue. so my original code worked. I changed my options to not show scientific notation. [How to prevent scientific notation in R?](https://stackoverflow.com/questions/25946047/how-to-prevent-scientific-notation-in-r) – BB.squared Dec 19 '18 at 18:52
0

With package plyr you can try this:

DT_sum <- ddply(DT, .("PARK","WTG"), colSums)
T. Ciffréo
  • 126
  • 10