I have a datatable as follows:
library(data.table)
dt <- fread(
"A B D E iso year
1 A 1 NA ECU 2009
2 B 2 0 ECU 2009
3 D 3 0 BRA 2011
4 E 4 0 BRA 2011
5 D 7 NA ECU 2008
6 E 1 0 ECU 2008
7 A 3 2 BRA 2012
8 A 4 NA BRA 2012",
header = TRUE
)
I want to create an aggregate of the dataset by doing something like: dt[, .(D = sum(D)), by = c("iso", "year")]
However, instead of only for D
, I want to be able to also add A
, E
(and 10 more columns in the actual data).
Desired output:
dt <- fread(
"A D E iso year
3 3 0 ECU 2009
7 7 0 BRA 2011
11 8 0 ECU 2008
15 7 2 BRA 2012",
header = TRUE
)
How can I specify this?