Suppose I have a data.table as below (where you can think of w as a grouping variable):
set.seed(1)
prQ = CJ(Q1 = 1:10, Q2=1:10,w=1:2)
prQ[,pQ:=runif(100,0,1)]
prQ[,pQ:=pQ/sum(pQ),by=w]
> prQ
Q1 Q2 w pQ
1: 1 1 1 0.004889560
2: 1 1 2 0.007553012
3: 1 2 1 0.010549565
4: 1 2 2 0.018433927
5: 1 3 1 0.003714138
---
196: 10 8 2 0.016183006
197: 10 9 1 0.008384253
198: 10 9 2 0.008323492
199: 10 10 1 0.014932841
200: 10 10 2 0.012278353
How do I calculate a new column, for a given w, called CDF, that does the following:
For example suppose Q1
= 4 and Q2
= 6. Define a new column,
CDF
= sum(pQ) for all Q1
<=4 and Q2
<=6, holding w fixed.
For example, a single row:
CDF0 = sum(prQ[Q1<=4 & Q2<=6 & w==1,pQ])
prQ[Q1==4 & Q2==6,CDF:=CDF0]
I want to do this with all rows for a given w.
Desired output done using brute force:
for(w0 in 1:2){
for(j in 1:10){
for(p in 1:10){
CDF0 = sum(prQ[Q1<=j & Q2<=p & w==w0,pQ])
prQ[Q1==j & Q2==p & w==w0,CDF:=CDF0]
}
}
}
> head(prQ)
Q1 Q2 w pQ CDF
1: 1 1 1 0.004889560 0.004889560
2: 1 1 2 0.007553012 0.007553012
3: 1 2 1 0.010549565 0.015439125
4: 1 2 2 0.018433927 0.025986939
5: 1 3 1 0.003714138 0.019153263
6: 1 3 2 0.018234648 0.044221587