0

Suppose that we have data.table like that:

    TYPE KEY  VALUE
1:  1    A    10
2:  1    B    10
3:  1    A    40
4:  2    B    20
5:  2    B    40

I need to generate the following aggregated data.table (numbers are sums of values for given TYPE and KEY):

    TYPE A    B
1:  1    50   10
2:  2    0    60

In a real life problem there are a lot of different values for KEY so it's impossible to hardcode them.

How can I achieve that?

levanovd
  • 4,095
  • 6
  • 37
  • 56

1 Answers1

1

One way I could think of is:

# to ensure all levels are present when using `tapply`
DT[, KEY := factor(KEY, levels=unique(KEY))]
DT[, as.list(tapply(VALUE, KEY, sum)), by = TYPE]
#    TYPE  A  B
# 1:    1 50 10
# 2:    2 NA 60
Arun
  • 116,683
  • 26
  • 284
  • 387