I have data
set.seed(42)
dat <- data.table(id=1:8, group=c(1,1,2,2,2,3,3,3), val=rnorm(8))
> dat
id group val
1: 1 1 1.37095845
2: 2 1 -0.56469817
3: 3 2 0.36312841
4: 4 2 0.63286260
5: 5 2 0.40426832
6: 6 3 -0.10612452
7: 7 3 1.51152200
8: 8 3 -0.09465904
and I would like to obtain the cumulative values of val
within each level of group
.
> res
id group cum
1: 1 1 1.37095845
2: 2 1 0.80626037
3: 3 2 0.36312841
4: 4 2 0.995991
5: 5 2 1.400259
6: 6 3 -0.10612452
7: 7 3 1.405397
8: 8 3 1.310738
I am always astonished by the efficiency of data.table
, so I 'm wondering about a way to get this done in data.table
but of course any other efficient solution is just as welcome.