I have a data.table of the following form:-
library(data.table)
b <- data.table(Code_Nm = c(rep("a", 3), rep("b", 3), rep("d", 3)),
Year = rep(2011:2013, 3), total = rep(10, 9),
a = c(rep(0, 3), 1:6), b = c(7:9, 0, 0, 0, 10:12), d = c(13:18, 0, 0, 0))
b
# Code_Nm Year total a b d
#1: a 2011 10 0 7 13
#2: a 2012 10 0 8 14
#3: a 2013 10 0 9 15
#4: b 2011 10 1 0 16
#5: b 2012 10 2 0 17
#6: b 2013 10 3 0 18
#7: d 2011 10 4 10 0
#8: d 2012 10 5 11 0
#9: d 2013 10 6 12 0
I want to get a data.table such that the last column named actual has the value equal to the total minus the sum of yearly groups of each Code_Nm. I.e. for a in 2011 it is 10 minus sum of all the 2011 values but from column a. This is equal to 10 - (1+4).
Similarly for a in 2012, it is 10 minus sum of all 2012 values but from column a. This is equal to 10 - (2+5).
Similary for b in 2011 it is 10 minus sum of all the 2011 values but from column b. This is equal to 10 - (7+10). For b in 2012, it is 10 minus sum of all 2012 values but from column b. This is equal to 10 - (8+11).
And similary for d column as well.
The final result is the following data.table:-
b <- data.table(Code_Nm = c(rep("a", 3), rep("b", 3), rep("d", 3)),
Year = rep(2011:2013, 3), total = rep(10, 9),
a = c(rep(0, 3), 1:6), b = c(7:9, 0, 0, 0, 10:12), d = c(13:18, 0, 0, 0),
actual = c(5, 3, 1, -7, -9, -11, -19, -21, -23))
b
# Code_Nm Year total a b d actual
#1: a 2011 10 0 7 13 5
#2: a 2012 10 0 8 14 3
#3: a 2013 10 0 9 15 1
#4: b 2011 10 1 0 16 -7
#5: b 2012 10 2 0 17 -9
#6: b 2013 10 3 0 18 -11
#7: d 2011 10 4 10 0 -19
#8: d 2012 10 5 11 0 -21
#9: d 2013 10 6 12 0 -23
Please give a solution using a data.table.
Thanks in advance.