Ok, here is a problem that I would like to solve using an efficient, elegant solution like data.table or dplyr.
Define:
DT = data.table(group=c(rep("A",3),rep("B",5)),value=c(2,9,2,3,4,1,0,3)) time group value 1: 1 A 2 2: 2 A 9 3: 3 A 2 4: 1 B 3 5: 2 B 4 6: 3 B 1 7: 4 B 0 8: 5 B 3
What I'm trying to get is the sum total of groups of values ββdivisible by their reverse ordering at the point in time when they were observed.
time group value RESULT 1: 1 A 2 2.000000 2: 2 A 9 10.000000 3: 3 A 2 7.166667 4: 1 B 3 3.000000 5: 2 B 4 5.500000 6: 3 B 1 4.000000 7: 4 B 0 2.583333 8: 5 B 3 4.933333
In line 5, the result: 4/1 + 3/2 = 5.5 Since group 2 had 2 observations during time 2, the latter is divided by 1 and the previous one by 1. Next, in line 6, the result: 1/1 + 4/2+ 3/3 = 4 Since at time 3, group B had 3 observations, the last is divided by 1, the previous by 2 and still the previous by 3. In line 7, 0/1 + 1/2 + 4/3 + 3/4 = 2.583333 and so Further...
The data is big, so avoiding loops is very important!
r data.table dplyr data-manipulation
Edm
source share