# [R] cumulative sum by group and under some criteria

arun smartpink111 at yahoo.com
Wed Feb 27 14:43:41 CET 2013

```dat1<- read.table(text="
m1 n1 m n    A      B    C   D
2    2    4  5  0.1    0.2  0.2 0.3
2   2     4  4  0.2   0.1   0.3  0.4
2   3     4  5  0.5   0.6   0.2  0.2
2  4      3  4  0.2   0.8   0.5  0.3

dat1[which.max(dat1\$D),]
#  m1 n1 m n   A   B   C   D
#2  2  2 4 4 0.2 0.1 0.3 0.4
A.K.

________________________________
From: Joanna Zhang <zjoanna2013 at gmail.com>
To: arun <smartpink111 at yahoo.com>
Sent: Wednesday, February 27, 2013 1:25 AM
Subject: Re: [R] cumulative sum by group and under some criteria

Thanks very much! it works!

suppose that I have a data:
m1 n1 m n    A      B    C   D
2    2    4  5  0.1    0.2  0.2 0.3
2   2     4  4  0.2   0.1   0.3  0.4
I want to identify the max value of D and extract the row , in this example, it is the 2nd row.

On Tue, Feb 26, 2013 at 11:13 PM, arun <smartpink111 at yahoo.com> wrote:

res2<- join(res1,d3,by=c("m1","n1"),type="inner")
>
>p0L<-0.05
>p0H<-0.05
>p1L<-0.20
>p1H<-0.20
>
>res2<- within(res2,{p1<- x/m; p2<- y/n;term2_p0<-dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, log=FALSE);term2_p1<- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, log=FALSE)})
>
>res4<-do.call(rbind,lapply(seq_len(nrow(res2)),function(i) {Pm2<-rbeta(1000,0.2+res2[i,"x"],0.8+res2[i,"m"]-res2[i,"x"]);Pn2<- rbeta(1000,0.2+res2[i,"y"],0.8+res2[i,"n"]-res2[i,"y"]); Fm2<- ecdf(Pm2); Fn2<- ecdf(Pn2); Fmm2<- Fm2(res2[i,"p1"]); Fnn2<- Fn2(res2[i,"p2"]);R2<- (Fmm2+Fnn2)/2; Fmm_f2<- min(R2, Fmm2); Fnn_f2<- max(R2, Fnn2); Qm2<- 1-Fmm_f2; Qn2<- 1-Fnn_f2;data.frame(Fmm2,Fnn2,R2,Fmm_f2,Fnn_f2,Qm2,Qn2)}))
>
>res5<-cbind(res2,res4)
>#  m1 n1 x1 y1 m n x y cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H   term2_p1
>#1  2  2  0  0 4 4 0 0     0.9025       0.64     0.9025       0.64 0.16777216
>#2  2  2  0  0 4 4 0 1     0.9025       0.64     0.9025       0.64 0.08388608
>#3  2  2  0  0 4 4 0 2     0.9025       0.64     0.9025       0.64 0.01048576
>#4  2  2  0  0 4 4 1 0     0.9025       0.64     0.9025       0.64 0.08388608
>#5  2  2  0  0 4 4 1 1     0.9025       0.64     0.9025       0.64 0.04194304
>#     term2_p0   p2   p1 Fmm2  Fnn2     R2 Fmm_f2 Fnn_f2   Qm2   Qn2
>#1 0.663420431 0.00 0.00 0.00 0.000 0.0000  0.000  0.000 1.000 1.000
>#2 0.069833730 0.25 0.00 0.00 0.601 0.3005  0.000  0.601 1.000 0.399
>#3 0.001837730 0.50 0.00 0.00 0.612 0.3060  0.000  0.612 1.000 0.388
>#4 0.069833730 0.00 0.25 0.59 0.000 0.2950  0.295  0.295 0.705 0.705
>#5 0.007350919 0.25 0.25 0.60 0.566 0.5830  0.583  0.583 0.417 0.417
>
>
>A.K.
>
>
>
>
>>R-help at r-project.org mailing list
>>
>>https://stat.ethz.ch/mailman/listinfo/r-help