cumulative sum by group and under some criteria

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
58 messages Options
123
Reply | Threaded
Open this post in threaded view
|

cumulative sum by group and under some criteria

Zjoanna
This post has NOT been accepted by the mailing list yet.
 Within the group with the same m1 and n1, I need to calculate cumlative sum of x1  if x1 is not 0 and cumulative sum of y1 if y1 is not 0. Any help would be appreciated.


d <- data.frame ()
for ( m1 in 2: 3) {
     for (n1 in 2: 3){
          for (x1 in 0: m1) {
               for (y1 in 0: n1) {                                                                                                        
                                               
              d<-rbind(d, c(m1, n1, x1,y1))
}}}}
     
d

so for m1=2, n1=2, the cumlative sum of x1 should be 9 and sum of y1 is 9,
for  m1=2, n1=3, the cumlative sum of x1 should be 12 and sum of y1 is 18.



Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

arun kirshna
This post has NOT been accepted by the mailing list yet.
Hi,

I am not sure I understand it correctly.
set.seed(1243)
d<- data.frame(m1=rep(rep(1:4,each=4),3),n1=rep(rep(1:4,times=4),3), x1=sample(0:9,48,replace=TRUE),y1=sample(0:20,48,replace=TRUE))
lapply(unique(d$m1),function(i) d[d$m1==i & d$n1==i,])
#[[1]]
 #  m1 n1 x1 y1
#1   1  1  3 20
#17  1  1  9  5
#33  1  1  3  2

#[[2]]
  # m1 n1 x1 y1
#6   2  2  8 19
#22  2  2  0  1
#38  2  2  1 10

#[[3]]
  # m1 n1 x1 y1
#11  3  3  2  7
#27  3  3  8  0
#43  3  3  8 13

#[[4]]
  # m1 n1 x1 y1
#16  4  4  6  1
#32  4  4  7  9
#48  4  4  7  8

#if there is any 0 in either x1 or y1 columns in each of the same groups of m1 and n1, that column shoul d be left as such and do cumsum on rest of the columns (as I understand)
res<-do.call(rbind,lapply(lapply(unique(d$m1),function(i) d[d$m1==i & d$n1==i,]),function(x) {x[3:4][apply(x[,3:4]!=0,2,all)]<- cumsum(x[3:4][apply(x[,3:4]!=0,2,all)]);x}))
 res
#   m1 n1 x1 y1
#1   1  1  3 20
#17  1  1 12 25
#33  1  1 15 27
#6   2  2  8 19
#22  2  2  0 20
#38  2  2  1 30
#11  3  3  2  7
#27  3  3 10  0
#43  3  3 18 13
#16  4  4  6  1
#32  4  4 13 10
#48  4  4 20 18
A.K.
Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

Zjoanna
This post has NOT been accepted by the mailing list yet.
thank you for your reply, but let me restate and clarify my question:

maxN<-9
d <- data.frame ()
for ( m1 in 2: (maxN-6)) {
     for (n1 in 2: (maxN-m1-4)){
          for (x1 in 0: m1) {
               for (y1 in 0: n1) {
                       p11<- round(x1/m1, digits=2)
                       p12<- round(y1/n1, digits=2)                                                                                  
                       
                       

                       d<-rbind(d, c(m1,n1,x1,y1,p11,p12))
}}
 
}}
d

#within the group of m1,n1, for example, for all rows that m1 =2 and n1=3,
 I need to calculate cumulative sum of p11 if x1 >1 and
 to calculate cumulative sum of p12 if y1>1. I only need the final cumulative value in the last row.

the results should be like this:
  m1   n1 x1   y1 p11  p12  cp11 cp12
  2    3  0    0 0.00 0.00   0    0
  2    3  0    1 0.00 0.33   0    0
  2    3  0    2 0.00 0.67   0    0.67
  2    3  0    3 0.00 1.00   0    1.67
  2    3  1    0 0.50 0.00   0    1.67
  2    3  1    1 0.50 0.33   0    1.67
  2    3  1    2 0.50 0.67   0    2.34
  2    3  1    3 0.50 1.00   0    3.34
  2    3  2    0 1.00 0.00   1.00 3.34
  2    3  2    1 1.00 0.33   2.00 3.34
  2    3  2    2 1.00 0.67   3.00 4.01
  2    3  2    3 1.00 1.00   4.00 5.01
Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

arun kirshna
This post has NOT been accepted by the mailing list yet.
Hi,
Try this:
colnames(d)<-c("m1","n1","x1","y1","p11","p12")
library(zoo)
res1<- do.call(rbind,lapply(lapply(split(d,list(d$m1,d$n1)),function(x) {x$cp11[x$x1>1]<- cumsum(x$p11[x$x1>1]);x$cp12[x$y1>1]<- cumsum(x$p12[x$y1>1]);x}),function(x) {x$cp11<-na.locf(x$cp11,na.rm=F);x$cp12<- na.locf(x$cp12,na.rm=F);x})) #there would be a warning here as one of the list element is NULL.  The, warning is okay
row.names(res1)<- 1:nrow(res1)
res1[,7:8][is.na(res1[,7:8])]<- 0
res1
 #  m1 n1 x1 y1  p11  p12 cp11 cp12
#1   2  2  0  0 0.00 0.00 0.00 0.00
#2   2  2  0  1 0.00 0.50 0.00 0.00
#3   2  2  0  2 0.00 1.00 0.00 1.00
#4   2  2  1  0 0.50 0.00 0.00 1.00
#5   2  2  1  1 0.50 0.50 0.00 1.00
#6   2  2  1  2 0.50 1.00 0.00 2.00
#7   2  2  2  0 1.00 0.00 1.00 2.00
#8   2  2  2  1 1.00 0.50 2.00 2.00
#9   2  2  2  2 1.00 1.00 3.00 3.00
#10  3  2  0  0 0.00 0.00 0.00 0.00
#11  3  2  0  1 0.00 0.50 0.00 0.00
#12  3  2  0  2 0.00 1.00 0.00 1.00
#13  3  2  1  0 0.33 0.00 0.00 1.00
#14  3  2  1  1 0.33 0.50 0.00 1.00
#15  3  2  1  2 0.33 1.00 0.00 2.00
#16  3  2  2  0 0.67 0.00 0.67 2.00
#17  3  2  2  1 0.67 0.50 1.34 2.00
#18  3  2  2  2 0.67 1.00 2.01 3.00
#19  3  2  3  0 1.00 0.00 3.01 3.00
#20  3  2  3  1 1.00 0.50 4.01 3.00
#21  3  2  3  2 1.00 1.00 5.01 4.00
#22  2  3  0  0 0.00 0.00 0.00 0.00
#23  2  3  0  1 0.00 0.33 0.00 0.00
#24  2  3  0  2 0.00 0.67 0.00 0.67
#25  2  3  0  3 0.00 1.00 0.00 1.67
#26  2  3  1  0 0.50 0.00 0.00 1.67
#27  2  3  1  1 0.50 0.33 0.00 1.67
#28  2  3  1  2 0.50 0.67 0.00 2.34
#29  2  3  1  3 0.50 1.00 0.00 3.34
#30  2  3  2  0 1.00 0.00 1.00 3.34
#31  2  3  2  1 1.00 0.33 2.00 3.34
#32  2  3  2  2 1.00 0.67 3.00 4.01
#33  2  3  2  3 1.00 1.00 4.00 5.01
A.K.
Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

Zjoanna
Thank you very much for your reply. Your code work well with this example. I modified a little to fit my real data, I got an error massage.
 
Error in split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...) :
  Group length is 0 but data length > 0
 
 
On Thu, Jan 31, 2013 at 12:21 PM, arun kirshna [via R] <[hidden email]> wrote:
Hi,
Try this:
colnames(d)<-c("m1","n1","x1","y1","p11","p12")
library(zoo)
res1<- do.call(rbind,lapply(lapply(split(d,list(d$m1,d$n1)),function(x) {x$cp11[x$x1>1]<- cumsum(x$p11[x$x1>1]);x$cp12[x$y1>1]<- cumsum(x$p12[x$y1>1]);x}),function(x) {x$cp11<-na.locf(x$cp11,na.rm=F);x$cp12<- na.locf(x$cp12,na.rm=F);x})) #there would be a warning here as one of the list element is NULL.  The, warning is okay
row.names(res1)<- 1:nrow(res1)
res1[,7:8][is.na(res1[,7:8])]<- 0
res1
 #  m1 n1 x1 y1  p11  p12 cp11 cp12
#1   2  2  0  0 0.00 0.00 0.00 0.00
#2   2  2  0  1 0.00 0.50 0.00 0.00
#3   2  2  0  2 0.00 1.00 0.00 1.00
#4   2  2  1  0 0.50 0.00 0.00 1.00
#5   2  2  1  1 0.50 0.50 0.00 1.00
#6   2  2  1  2 0.50 1.00 0.00 2.00
#7   2  2  2  0 1.00 0.00 1.00 2.00
#8   2  2  2  1 1.00 0.50 2.00 2.00
#9   2  2  2  2 1.00 1.00 3.00 3.00
#10  3  2  0  0 0.00 0.00 0.00 0.00
#11  3  2  0  1 0.00 0.50 0.00 0.00
#12  3  2  0  2 0.00 1.00 0.00 1.00
#13  3  2  1  0 0.33 0.00 0.00 1.00
#14  3  2  1  1 0.33 0.50 0.00 1.00
#15  3  2  1  2 0.33 1.00 0.00 2.00
#16  3  2  2  0 0.67 0.00 0.67 2.00
#17  3  2  2  1 0.67 0.50 1.34 2.00
#18  3  2  2  2 0.67 1.00 2.01 3.00
#19  3  2  3  0 1.00 0.00 3.01 3.00
#20  3  2  3  1 1.00 0.50 4.01 3.00
#21  3  2  3  2 1.00 1.00 5.01 4.00
#22  2  3  0  0 0.00 0.00 0.00 0.00
#23  2  3  0  1 0.00 0.33 0.00 0.00
#24  2  3  0  2 0.00 0.67 0.00 0.67
#25  2  3  0  3 0.00 1.00 0.00 1.67
#26  2  3  1  0 0.50 0.00 0.00 1.67
#27  2  3  1  1 0.50 0.33 0.00 1.67
#28  2  3  1  2 0.50 0.67 0.00 2.34
#29  2  3  1  3 0.50 1.00 0.00 3.34
#30  2  3  2  0 1.00 0.00 1.00 3.34
#31  2  3  2  1 1.00 0.33 2.00 3.34
#32  2  3  2  2 1.00 0.67 3.00 4.01
#33  2  3  2  3 1.00 1.00 4.00 5.01
A.K.


If you reply to this email, your message will be added to the discussion below:
http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657196.html
To unsubscribe from cumulative sum by group and under some criteria, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

arun kirshna
This post has NOT been accepted by the mailing list yet.
HI,
Could you show the modified code and also str(dataset)?
A.K.
Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

Zjoanna
I am new to this mailing list. Is everything I posted seen for everyone in the mailing list? or just you can see the post?

On Fri, Feb 1, 2013 at 11:33 AM, arun kirshna [via R] <[hidden email]> wrote:
HI,
Could you show the modified code and also str(dataset)?
A.K.


If you reply to this email, your message will be added to the discussion below:
http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657316.html
To unsubscribe from cumulative sum by group and under some criteria, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

Zjoanna
This post has NOT been accepted by the mailing list yet.
In reply to this post by arun kirshna
maxN<-9
c11<-0.2
c12<-0.2
p0L<-0.05
p0H<-0.05
p1L<-0.20
p1H<-0.20

d <- data.frame ()
for ( m1 in 2: (maxN-6)) {
     for (n1 in 2: (maxN-m1-4)){
          for (x1 in 0: m1) {
               for (y1 in 0: n1) {
                       p11<- x1/m1
                       p12<- y1/n1

                       Pm<-rbeta(100, 0.2+x1, 0.8+m1-x1)
                       Fm<-ecdf(Pm)
                       Fmm<-Fm(x1/m1)
                                             
                       Pn<-rbeta(100, 0.2+y1, 0.8+n1-y1)
                       Fn<-ecdf(Pn)
                       Fnn<-Fn(y1/n1)      
                   
                       R = (Fm(x1/m1)+Fn(y1/n1))/2
                       Fm_f=min(R, Fm(x1/m1))
                       Fn_f=max(R, Fn(y1/n1))
                     
                       Qm = 1-Fm_f
                       Qn = 1-Fn_f
 
                       term1_p0 = dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)
                       term1_p1 = dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, log=FALSE)                                                              
                       
             
                       d<-rbind(d, c(m1,n1,x1,y1,Fmm, Fnn, Qm, Qn, term1_p0, term1_p1))
}}
}}
d

colnames(d)<-c("m1","n1",,"x1","y1","Fmm", "Fnn", "Qm", "Qn", "term1_p0", "term1_p1")
d

library(zoo)
res1<- do.call(rbind,lapply(lapply(split(d,list(d$m1,d$n1)),function(x)
 {x$cterm1_p0L[x$Qm<=c11]<- cumsum(x$term1_p0[x$Qm<=c11]);
  x$cterm1_p0H[x$Qn<=c12]<- cumsum(x$term1_p0[x$Qn<=c12]);
  x$cterm1_p1L[x$Qm<=c11]<- cumsum(x$term1_p1[x$Qm<=c11]);
  x$cterm1_p1H[x$Qm<=c12]<- cumsum(x$term1_p1[x$Qn<=c12]);
  x}),function(x) {x$cterm1_p0L<-na.locf(x$cterm1_p0L,na.rm=F);
                   x$cterm1_p0H<-na.locf(x$cterm1_p0H,na.rm=F);
                   x$cterm1_p1L<-na.locf(x$cterm1_p1L,na.rm=F);
                   x$cterm1_p1H<-na.locf(x$cterm1_p1H,na.rm=F);x}))

res1
Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

Zjoanna
This post has NOT been accepted by the mailing list yet.
In reply to this post by arun kirshna
Hi ,

can you see the modified code? I replied to myself in the last massage. I am attaching again here just in case.

maxN<-9
c11<-0.2
c12<-0.2
p0L<-0.05
p0H<-0.05
p1L<-0.20
p1H<-0.20

d <- data.frame ()
for ( m1 in 2: (maxN-6)) {
     for (n1 in 2: (maxN-m1-4)){
          for (x1 in 0: m1) {
               for (y1 in 0: n1) {
                       p11<- x1/m1
                       p12<- y1/n1

                       Pm<-rbeta(100, 0.2+x1, 0.8+m1-x1)
                       Fm<-ecdf(Pm)
                       Fmm<-Fm(x1/m1)
                                             
                       Pn<-rbeta(100, 0.2+y1, 0.8+n1-y1)
                       Fn<-ecdf(Pn)
                       Fnn<-Fn(y1/n1)      
                   
                       R = (Fm(x1/m1)+Fn(y1/n1))/2
                       Fm_f=min(R, Fm(x1/m1))
                       Fn_f=max(R, Fn(y1/n1))
                     
                       Qm = 1-Fm_f
                       Qn = 1-Fn_f
 
                       term1_p0 = dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)
                       term1_p1 = dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, log=FALSE)                                                              
                       
             
                       d<-rbind(d, c(m1,n1,x1,y1,Fmm, Fnn, Qm, Qn, term1_p0, term1_p1))
}}
}}
d

colnames(d)<-c("m1","n1",,"x1","y1","Fmm", "Fnn", "Qm", "Qn", "term1_p0", "term1_p1")
d

library(zoo)
res1<- do.call(rbind,lapply(lapply(split(d,list(d$m1,d$n1)),function(x)
 {x$cterm1_p0L[x$Qm<=c11]<- cumsum(x$term1_p0[x$Qm<=c11]);
  x$cterm1_p0H[x$Qn<=c12]<- cumsum(x$term1_p0[x$Qn<=c12]);
  x$cterm1_p1L[x$Qm<=c11]<- cumsum(x$term1_p1[x$Qm<=c11]);
  x$cterm1_p1H[x$Qm<=c12]<- cumsum(x$term1_p1[x$Qn<=c12]);
  x}),function(x) {x$cterm1_p0L<-na.locf(x$cterm1_p0L,na.rm=F);
                   x$cterm1_p0H<-na.locf(x$cterm1_p0H,na.rm=F);
                   x$cterm1_p1L<-na.locf(x$cterm1_p1L,na.rm=F);
                   x$cterm1_p1H<-na.locf(x$cterm1_p1H,na.rm=F);x}))

res1
 
Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

arun kirshna
This post has NOT been accepted by the mailing list yet.
Hi,
Just by a glance:
colnames(d)<-c("m1","n1",,"x1","y1","Fmm", "Fnn", "Qm", "Qn", "term1_p0", "term1_p1")
                                                    #between "n1" and "x1", two commas..  Could you clarify it?
#Error in c("m1", "n1", , "x1", "y1", "Fmm", "Fnn", "Qm", "Qn", "term1_p0",  :
 # argument 3 is empty
I hope this is a typo.  But, still you have 11 columns and only 10 colnames.

Can you look into it?
A.K.

 
Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

arun kirshna
In reply to this post by Zjoanna
Hi,

Saw your reply on Nabble:
#Your code:

library(zoo)
res1<- do.call(rbind,lapply(lapply(split(d,list(d$m1,d$n1)),function(x) 
 {x$cterm1_p0L[x$Qm<=c11]<- cumsum(x$term1_p0[x$Qm<=c11]); 
  x$cterm1_p0H[x$Qn<=c12]<- cumsum(x$term1_p0[x$Qn<=c12]); 
  x$cterm1_p1L[x$Qm<=c11]<- cumsum(x$term1_p1[x$Qm<=c11]); 
  x$cterm1_p1H[x$Qm<=c12]<- cumsum(x$term1_p1[x$Qn<=c12]); #Check this line Qm and Qn  
  x}),function(x) {x$cterm1_p0L<-na.locf(x$cterm1_p0L,na.rm=F); 
                   x$cterm1_p0H<-na.locf(x$cterm1_p0H,na.rm=F); 
                   x$cterm1_p1L<-na.locf(x$cterm1_p1L,na.rm=F); 
                   x$cterm1_p1H<-na.locf(x$cterm1_p1H,na.rm=F);x})) 



#should be:
 colnames(d)<-c("m1","n1","x1","y1","Fmm", "Fnn", "Qm", "Qn", "term1_p0", "term1_p1") 
res1<- do.call(rbind,lapply(lapply(split(d,list(d$m1,d$n1)),function(x) {x$cterm1_p0L[x$Qm<=c11]<- cumsum(x$term1_p0[x$Qm<=c11]);
  x$cterm1_p0H[x$Qn<=c12]<- cumsum(x$term1_p0[x$Qn<=c12]); 
 x$cterm1_p1L[x$Qm<=c11]<- cumsum(x$term1_p1[x$Qm<= c11]);
 x$cterm1_p1H[x$Qn<=c12]<- cumsum(x$term1_p1[x$Qn<= c12]);
 x}),function(x) {x$cterm1_p0L<-na.locf(x$cterm1_p0L,na.rm=F); 
                   x$cterm1_p0H<-na.locf(x$cterm1_p0H,na.rm=F); 
                   x$cterm1_p1L<-na.locf(x$cterm1_p1L,na.rm=F); 
                   x$cterm1_p1H<-na.locf(x$cterm1_p1H,na.rm=F);x})) 
  
row.names(res1) <- 1:nrow(res1)
res1[,11:14][is.na(res1[,11:14])]<- 0 


 res1[,11:14][is.na(res1[,11:14])]<- 0 
 res1
#   m1 n1 x1 y1  Fmm  Fnn    Qm    Qn     term1_p0 term1_p1  cterm1_p0L   cterm1_p0H cterm1_p1L cterm1_p1H
#1   2  2  0  0 0.00 0.00 1.000 1.000 0.8145062500  0.40960 0.00000e+00 0.0000000000    0.00000    0.00000
#2   2  2  0  1 0.00 0.60 1.000 0.400 0.0857375000  0.20480 0.00000e+00 0.0000000000    0.00000    0.00000
#3   2  2  0  2 0.00 1.00 1.000 0.000 0.0022562500  0.02560 0.00000e+00 0.0022562500    0.00000    0.02560
#4   2  2  1  0 0.61 0.00 0.695 0.695 0.0857375000  0.20480 0.00000e+00 0.0022562500    0.00000    0.02560
#5   2  2  1  1 0.61 0.62 0.390 0.380 0.0090250000  0.10240 0.00000e+00 0.0022562500    0.00000    0.02560
#6   2  2  1  2 0.63 1.00 0.370 0.000 0.0002375000  0.01280 0.00000e+00 0.0024937500    0.00000    0.03840
#7   2  2  2  0 1.00 0.00 0.500 0.500 0.0022562500  0.02560 0.00000e+00 0.0024937500    0.00000    0.03840
#8   2  2  2  1 1.00 0.67 0.165 0.165 0.0002375000  0.01280 2.37500e-04 0.0027312500    0.01280    0.05120
#9   2  2  2  2 1.00 1.00 0.000 0.000 0.0000062500  0.00160 2.43750e-04 0.0027375000    0.01440    0.05280
#10  3  2  0  0 0.00 0.00 1.000 1.000 0.7737809375  0.32768 0.00000e+00 0.0000000000    0.00000    0.00000
#11  3  2  0  1 0.00 0.65 1.000 0.350 0.0814506250  0.16384 0.00000e+00 0.0000000000    0.00000    0.00000
#12  3  2  0  2 0.00 1.00 1.000 0.000 0.0021434375  0.02048 0.00000e+00 0.0021434375    0.00000    0.02048
#13  3  2  1  0 0.67 0.00 0.665 0.665 0.1221759375  0.24576 0.00000e+00 0.0021434375    0.00000    0.02048
#14  3  2  1  1 0.60 0.64 0.400 0.360 0.0128606250  0.12288 0.00000e+00 0.0021434375    0.00000    0.02048
#15  3  2  1  2 0.66 1.00 0.340 0.000 0.0003384375  0.01536 0.00000e+00 0.0024818750    0.00000    0.03584
#16  3  2  2  0 0.71 0.00 0.645 0.645 0.0064303125  0.06144 0.00000e+00 0.0024818750    0.00000    0.03584
#17  3  2  2  1 0.69 0.66 0.325 0.325 0.0006768750  0.03072 0.00000e+00 0.0024818750    0.00000    0.03584
#18  3  2  2  2 0.64 1.00 0.360 0.000 0.0000178125  0.00384 0.00000e+00 0.0024996875    0.00000    0.03968
#19  3  2  3  0 1.00 0.00 0.500 0.500 0.0001128125  0.00512 0.00000e+00 0.0024996875    0.00000    0.03968
#20  3  2  3  1 1.00 0.74 0.130 0.130 0.0000118750  0.00256 1.18750e-05 0.0025115625    0.00256    0.04224
#21  3  2  3  2 1.00 1.00 0.000 0.000 0.0000003125  0.00032 1.21875e-05 0.0025118750    0.00288    0.04256
#22  2  3  0  0 0.00 0.00 1.000 1.000 0.7737809375  0.32768 0.00000e+00 0.0000000000    0.00000    0.00000
#23  2  3  0  1 0.00 0.60 1.000 0.400 0.1221759375  0.24576 0.00000e+00 0.0000000000    0.00000    0.00000
#24  2  3  0  2 0.00 0.65 1.000 0.350 0.0064303125  0.06144 0.00000e+00 0.0000000000    0.00000    0.00000
#25  2  3  0  3 0.00 1.00 1.000 0.000 0.0001128125  0.00512 0.00000e+00 0.0001128125    0.00000    0.00512
#26  2  3  1  0 0.77 0.00 0.615 0.615 0.0814506250  0.16384 0.00000e+00 0.0001128125    0.00000    0.00512
#27  2  3  1  1 0.60 0.62 0.400 0.380 0.0128606250  0.12288 0.00000e+00 0.0001128125    0.00000    0.00512
#28  2  3  1  2 0.61 0.72 0.390 0.280 0.0006768750  0.03072 0.00000e+00 0.0001128125    0.00000    0.00512
#29  2  3  1  3 0.65 1.00 0.350 0.000 0.0000118750  0.00256 0.00000e+00 0.0001246875    0.00000    0.00768
#30  2  3  2  0 1.00 0.00 0.500 0.500 0.0021434375  0.02048 0.00000e+00 0.0001246875    0.00000    0.00768
#31  2  3  2  1 1.00 0.58 0.210 0.210 0.0003384375  0.01536 0.00000e+00 0.0001246875    0.00000    0.00768
#32  2  3  2  2 1.00 0.60 0.200 0.200 0.0000178125  0.00384 1.78125e-05 0.0001425000    0.00384    0.01152
#33  2  3  2  3 1.00 1.00 0.000 0.000 0.0000003125  0.00032 1.81250e-05 0.0001428125    0.00416    0.01184

A.K.



----- Original Message -----
From: Zjoanna <[hidden email]>
To: [hidden email]
Cc:
Sent: Friday, February 1, 2013 12:19 PM
Subject: Re: [R] cumulative sum by group and under some criteria

Thank you very much for your reply. Your code work well with this example.
I modified a little to fit my real data, I got an error massage.

Error in split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...) :
  Group length is 0 but data length > 0


On Thu, Jan 31, 2013 at 12:21 PM, arun kirshna [via R] <
[hidden email]> wrote:

> Hi,
> Try this:
> colnames(d)<-c("m1","n1","x1","y1","p11","p12")
> library(zoo)
> res1<- do.call(rbind,lapply(lapply(split(d,list(d$m1,d$n1)),function(x)
> {x$cp11[x$x1>1]<- cumsum(x$p11[x$x1>1]);x$cp12[x$y1>1]<-
> cumsum(x$p12[x$y1>1]);x}),function(x)
> {x$cp11<-na.locf(x$cp11,na.rm=F);x$cp12<- na.locf(x$cp12,na.rm=F);x}))
> #there would be a warning here as one of the list element is NULL.  The,
> warning is okay
> row.names(res1)<- 1:nrow(res1)
> res1[,7:8][is.na(res1[,7:8])]<- 0
> res1
>  #  m1 n1 x1 y1  p11  p12 cp11 cp12
> #1   2  2  0  0 0.00 0.00 0.00 0.00
> #2   2  2  0  1 0.00 0.50 0.00 0.00
> #3   2  2  0  2 0.00 1.00 0.00 1.00
> #4   2  2  1  0 0.50 0.00 0.00 1.00
> #5   2  2  1  1 0.50 0.50 0.00 1.00
> #6   2  2  1  2 0.50 1.00 0.00 2.00
> #7   2  2  2  0 1.00 0.00 1.00 2.00
> #8   2  2  2  1 1.00 0.50 2.00 2.00
> #9   2  2  2  2 1.00 1.00 3.00 3.00
> #10  3  2  0  0 0.00 0.00 0.00 0.00
> #11  3  2  0  1 0.00 0.50 0.00 0.00
> #12  3  2  0  2 0.00 1.00 0.00 1.00
> #13  3  2  1  0 0.33 0.00 0.00 1.00
> #14  3  2  1  1 0.33 0.50 0.00 1.00
> #15  3  2  1  2 0.33 1.00 0.00 2.00
> #16  3  2  2  0 0.67 0.00 0.67 2.00
> #17  3  2  2  1 0.67 0.50 1.34 2.00
> #18  3  2  2  2 0.67 1.00 2.01 3.00
> #19  3  2  3  0 1.00 0.00 3.01 3.00
> #20  3  2  3  1 1.00 0.50 4.01 3.00
> #21  3  2  3  2 1.00 1.00 5.01 4.00
> #22  2  3  0  0 0.00 0.00 0.00 0.00
> #23  2  3  0  1 0.00 0.33 0.00 0.00
> #24  2  3  0  2 0.00 0.67 0.00 0.67
> #25  2  3  0  3 0.00 1.00 0.00 1.67
> #26  2  3  1  0 0.50 0.00 0.00 1.67
> #27  2  3  1  1 0.50 0.33 0.00 1.67
> #28  2  3  1  2 0.50 0.67 0.00 2.34
> #29  2  3  1  3 0.50 1.00 0.00 3.34
> #30  2  3  2  0 1.00 0.00 1.00 3.34
> #31  2  3  2  1 1.00 0.33 2.00 3.34
> #32  2  3  2  2 1.00 0.67 3.00 4.01
> #33  2  3  2  3 1.00 1.00 4.00 5.01
> A.K.
>
> ------------------------------
>  If you reply to this email, your message will be added to the discussion
> below:
>
> http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657196.html
> To unsubscribe from cumulative sum by group and under some criteria, click
> here<
> .
> NAML<
http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




--
View this message in context: http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657315.html
Sent from the R help mailing list archive at Nabble.com.
    [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

arun kirshna
In reply to this post by Zjoanna
HI,

I think this should be more correct:
maxN<-9 
c11<-0.2 
c12<-0.2 
p0L<-0.05 
p0H<-0.05 
p1L<-0.20 
p1H<-0.20 

d <- structure(list(m1 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), 
    n1 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 
    3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), x1 = c(0, 
    0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 
    2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3), y1 = c(0, 1, 2, 0, 
    1, 2, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 
    2, 0, 1, 2, 0, 1, 2, 0, 1, 2), Fmm = c(0, 0, 0, 0.7, 0.59, 
    0.64, 1, 1, 1, 0, 0, 0, 0, 0.63, 0.7, 0.74, 0.68, 1, 1, 1, 
    1, 0, 0, 0, 0.62, 0.63, 0.6, 0.63, 0.6, 0.68, 1, 1, 1), Fnn = c(0, 
    0.64, 1, 0, 0.51, 1, 0, 0.67, 1, 0, 0.62, 0.69, 1, 0, 0.54, 
    0.62, 1, 0, 0.63, 0.73, 1, 0, 0.63, 1, 0, 0.7, 1, 0, 0.7, 
    1, 0, 0.58, 1), Qm = c(1, 1, 1, 0.65, 0.45, 0.36, 0.5, 0.165, 
    0, 1, 1, 1, 1, 0.685, 0.38, 0.32, 0.32, 0.5, 0.185, 0.135, 
    0, 1, 1, 1, 0.69, 0.37, 0.4, 0.685, 0.4, 0.32, 0.5, 0.21, 
    0), Qn = c(1, 0.36, 0, 0.65, 0.45, 0, 0.5, 0.165, 0, 1, 0.38, 
    0.31, 0, 0.685, 0.38, 0.32, 0, 0.5, 0.185, 0.135, 0, 1, 0.37, 
    0, 0.69, 0.3, 0, 0.685, 0.3, 0, 0.5, 0.21, 0), term1_p0 = c(0.81450625, 
    0.0857375, 0.00225625, 0.0857375, 0.009025, 0.0002375, 0.00225625, 
    0.0002375, 6.25e-06, 0.7737809375, 0.1221759375, 0.00643031249999999, 
    0.0001128125, 0.081450625, 0.012860625, 0.000676875, 1.1875e-05, 
    0.0021434375, 0.0003384375, 1.78125e-05, 3.125e-07, 0.7737809375, 
    0.081450625, 0.0021434375, 0.1221759375, 0.012860625, 0.0003384375, 
    0.00643031249999999, 0.000676875, 1.78125e-05, 0.0001128125, 
    1.1875e-05, 3.125e-07), term1_p1 = c(0.4096, 0.2048, 0.0256, 
    0.2048, 0.1024, 0.0128, 0.0256, 0.0128, 0.0016, 0.32768, 
    0.24576, 0.06144, 0.00512, 0.16384, 0.12288, 0.03072, 0.00256, 
    0.02048, 0.01536, 0.00384, 0.00032, 0.32768, 0.16384, 0.02048, 
    0.24576, 0.12288, 0.01536, 0.06144, 0.03072, 0.00384, 0.00512, 
    0.00256, 0.00032)), .Names = c("m1", "n1", "x1", "y1", "Fmm", 
"Fnn", "Qm", "Qn", "term1_p0", "term1_p1"), row.names = c(NA, 
33L), class = "data.frame")

library(zoo)
lst1<- split(d,list(d$m1,d$n1))
res2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){
x[,11:14]<-NA;
x[,11:12][x$Qm<=c11,]<-cumsum(x[,9:10][x$Qm<=c11,]);
x[,13:14][x$Qn<=c12,]<-cumsum(x[,9:10][x$Qn<=c12,]);
colnames(x)[11:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H");
x1<-na.locf(x);
x1[,11:14][is.na(x1[,11:14])]<-0;
x1}))
row.names(res2)<- 1:nrow(res2)

 res2
 #  m1 n1 x1 y1  Fmm  Fnn    Qm    Qn     term1_p0 term1_p1   cterm1_P0L cterm1_P1L   cterm1_P0H cterm1_P1H

#1   2  2  0  0 0.00 0.00 1.000 1.000 0.8145062500  0.40960 0.0000000000    0.00000 0.0000000000    0.00000
#2   2  2  0  1 0.00 0.64 1.000 0.360 0.0857375000  0.20480 0.0000000000    0.00000 0.0000000000    0.00000
#3   2  2  0  2 0.00 1.00 1.000 0.000 0.0022562500  0.02560 0.0000000000    0.00000 0.0022562500    0.02560
#4   2  2  1  0 0.70 0.00 0.650 0.650 0.0857375000  0.20480 0.0000000000    0.00000 0.0022562500    0.02560
#5   2  2  1  1 0.59 0.51 0.450 0.450 0.0090250000  0.10240 0.0000000000    0.00000 0.0022562500    0.02560
#6   2  2  1  2 0.64 1.00 0.360 0.000 0.0002375000  0.01280 0.0000000000    0.00000 0.0024937500    0.03840
#7   2  2  2  0 1.00 0.00 0.500 0.500 0.0022562500  0.02560 0.0000000000    0.00000 0.0024937500    0.03840
#8   2  2  2  1 1.00 0.67 0.165 0.165 0.0002375000  0.01280 0.0002375000    0.01280 0.0027312500    0.05120
#9   2  2  2  2 1.00 1.00 0.000 0.000 0.0000062500  0.00160 0.0002437500    0.01440 0.0027375000    0.05280
#10  3  2  0  0 0.00 0.00 1.000 1.000 0.7737809375  0.32768 0.0000000000    0.00000 0.0000000000    0.00000
#11  3  2  0  1 0.00 0.63 1.000 0.370 0.0814506250  0.16384 0.0000000000    0.00000 0.0000000000    0.00000
#12  3  2  0  2 0.00 1.00 1.000 0.000 0.0021434375  0.02048 0.0000000000    0.00000 0.0021434375    0.02048
#13  3  2  1  0 0.62 0.00 0.690 0.690 0.1221759375  0.24576 0.0000000000    0.00000 0.0021434375    0.02048
#14  3  2  1  1 0.63 0.70 0.370 0.300 0.0128606250  0.12288 0.0000000000    0.00000 0.0021434375    0.02048
#15  3  2  1  2 0.60 1.00 0.400 0.000 0.0003384375  0.01536 0.0000000000    0.00000 0.0024818750    0.03584
#16  3  2  2  0 0.63 0.00 0.685 0.685 0.0064303125  0.06144 0.0000000000    0.00000 0.0024818750    0.03584
#17  3  2  2  1 0.60 0.70 0.400 0.300 0.0006768750  0.03072 0.0000000000    0.00000 0.0024818750    0.03584
#18  3  2  2  2 0.68 1.00 0.320 0.000 0.0000178125  0.00384 0.0000000000    0.00000 0.0024996875    0.03968
#19  3  2  3  0 1.00 0.00 0.500 0.500 0.0001128125  0.00512 0.0000000000    0.00000 0.0024996875    0.03968
#20  3  2  3  1 1.00 0.58 0.210 0.210 0.0000118750  0.00256 0.0000000000    0.00000 0.0024996875    0.03968
#21  3  2  3  2 1.00 1.00 0.000 0.000 0.0000003125  0.00032 0.0000003125    0.00032 0.0025000000    0.04000
#22  2  3  0  0 0.00 0.00 1.000 1.000 0.7737809375  0.32768 0.0000000000    0.00000 0.0000000000    0.00000
#23  2  3  0  1 0.00 0.62 1.000 0.380 0.1221759375  0.24576 0.0000000000    0.00000 0.0000000000    0.00000
#24  2  3  0  2 0.00 0.69 1.000 0.310 0.0064303125  0.06144 0.0000000000    0.00000 0.0000000000    0.00000
#25  2  3  0  3 0.00 1.00 1.000 0.000 0.0001128125  0.00512 0.0000000000    0.00000 0.0001128125    0.00512
#26  2  3  1  0 0.63 0.00 0.685 0.685 0.0814506250  0.16384 0.0000000000    0.00000 0.0001128125    0.00512
#27  2  3  1  1 0.70 0.54 0.380 0.380 0.0128606250  0.12288 0.0000000000    0.00000 0.0001128125    0.00512
#28  2  3  1  2 0.74 0.62 0.320 0.320 0.0006768750  0.03072 0.0000000000    0.00000 0.0001128125    0.00512
#29  2  3  1  3 0.68 1.00 0.320 0.000 0.0000118750  0.00256 0.0000000000    0.00000 0.0001246875    0.00768
#30  2  3  2  0 1.00 0.00 0.500 0.500 0.0021434375  0.02048 0.0000000000    0.00000 0.0001246875    0.00768
#31  2  3  2  1 1.00 0.63 0.185 0.185 0.0003384375  0.01536 0.0003384375    0.01536 0.0004631250    0.02304
#32  2  3  2  2 1.00 0.73 0.135 0.135 0.0000178125  0.00384 0.0003562500    0.01920 0.0004809375    0.02688  
#33  2  3  2  3 1.00 1.00 0.000 0.000 0.0000003125  0.00032 0.0003565625    0.01952 0.0004812500    0.02720

#Sorry, some values in my previous solution didn't look right. I didn't 
A.K.





----- Original Message -----
From: Zjoanna <[hidden email]>
To: [hidden email]
Cc:
Sent: Friday, February 1, 2013 12:19 PM
Subject: Re: [R] cumulative sum by group and under some criteria

Thank you very much for your reply. Your code work well with this example.
I modified a little to fit my real data, I got an error massage.

Error in split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...) :
  Group length is 0 but data length > 0


On Thu, Jan 31, 2013 at 12:21 PM, arun kirshna [via R] <
[hidden email]> wrote:

> Hi,
> Try this:
> colnames(d)<-c("m1","n1","x1","y1","p11","p12")
> library(zoo)
> res1<- do.call(rbind,lapply(lapply(split(d,list(d$m1,d$n1)),function(x)
> {x$cp11[x$x1>1]<- cumsum(x$p11[x$x1>1]);x$cp12[x$y1>1]<-
> cumsum(x$p12[x$y1>1]);x}),function(x)
> {x$cp11<-na.locf(x$cp11,na.rm=F);x$cp12<- na.locf(x$cp12,na.rm=F);x}))
> #there would be a warning here as one of the list element is NULL.  The,
> warning is okay
> row.names(res1)<- 1:nrow(res1)
> res1[,7:8][is.na(res1[,7:8])]<- 0
> res1
>  #  m1 n1 x1 y1  p11  p12 cp11 cp12
> #1   2  2  0  0 0.00 0.00 0.00 0.00
> #2   2  2  0  1 0.00 0.50 0.00 0.00
> #3   2  2  0  2 0.00 1.00 0.00 1.00
> #4   2  2  1  0 0.50 0.00 0.00 1.00
> #5   2  2  1  1 0.50 0.50 0.00 1.00
> #6   2  2  1  2 0.50 1.00 0.00 2.00
> #7   2  2  2  0 1.00 0.00 1.00 2.00
> #8   2  2  2  1 1.00 0.50 2.00 2.00
> #9   2  2  2  2 1.00 1.00 3.00 3.00
> #10  3  2  0  0 0.00 0.00 0.00 0.00
> #11  3  2  0  1 0.00 0.50 0.00 0.00
> #12  3  2  0  2 0.00 1.00 0.00 1.00
> #13  3  2  1  0 0.33 0.00 0.00 1.00
> #14  3  2  1  1 0.33 0.50 0.00 1.00
> #15  3  2  1  2 0.33 1.00 0.00 2.00
> #16  3  2  2  0 0.67 0.00 0.67 2.00
> #17  3  2  2  1 0.67 0.50 1.34 2.00
> #18  3  2  2  2 0.67 1.00 2.01 3.00
> #19  3  2  3  0 1.00 0.00 3.01 3.00
> #20  3  2  3  1 1.00 0.50 4.01 3.00
> #21  3  2  3  2 1.00 1.00 5.01 4.00
> #22  2  3  0  0 0.00 0.00 0.00 0.00
> #23  2  3  0  1 0.00 0.33 0.00 0.00
> #24  2  3  0  2 0.00 0.67 0.00 0.67
> #25  2  3  0  3 0.00 1.00 0.00 1.67
> #26  2  3  1  0 0.50 0.00 0.00 1.67
> #27  2  3  1  1 0.50 0.33 0.00 1.67
> #28  2  3  1  2 0.50 0.67 0.00 2.34
> #29  2  3  1  3 0.50 1.00 0.00 3.34
> #30  2  3  2  0 1.00 0.00 1.00 3.34
> #31  2  3  2  1 1.00 0.33 2.00 3.34
> #32  2  3  2  2 1.00 0.67 3.00 4.01
> #33  2  3  2  3 1.00 1.00 4.00 5.01
> A.K.
>
> ------------------------------
>  If you reply to this email, your message will be added to the discussion
> below:
>
> http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657196.html
> To unsubscribe from cumulative sum by group and under some criteria, click
> here<
> .
> NAML<
http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




--
View this message in context: http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657315.html
Sent from the R help mailing list archive at Nabble.com.
    [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

arun kirshna
In reply to this post by Zjoanna
Hi,

If you need to extract only the columns `m1` and `n1` which satisfy the condition.

 res2[,1:2][res2$cterm1_P1L<0.01 & res2$cterm1_P1L!=0,] 
#   m1 n1
#20  3  2
#21  3  2

#  If you wanted structure(....) as shown below for `d`, use dput(res2)
A.K.

----- Original Message -----
From: "[hidden email]" <[hidden email]>
To: [hidden email]
Cc:
Sent: Sunday, February 3, 2013 3:58 PM
Subject: Re: cumulative sum by group and under some criteria

Hi,
Let me restate my questions. I need to get the m1 and n1 that satisfy some criteria, for example in this case, within each group, the maximum cterm1_p1L ( the last row in this group) <0.01. I need to extract m1=3, n1=2, I only need m1, n1 in the row.

Also, how to create the structure from the data.frame, I am new to R, I need to change the maxN and run the loop to different data.
Thanks very much for your help!

<quote author='arun kirshna'>
HI,

I think this should be more correct:
maxN<-9 
c11<-0.2 
c12<-0.2 
p0L<-0.05 
p0H<-0.05 
p1L<-0.20 
p1H<-0.20 

d <- structure(list(m1 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), 
    n1 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 
    3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), x1 = c(0, 
    0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 
    2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3), y1 = c(0, 1, 2, 0, 
    1, 2, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 
    2, 0, 1, 2, 0, 1, 2, 0, 1, 2), Fmm = c(0, 0, 0, 0.7, 0.59, 
    0.64, 1, 1, 1, 0, 0, 0, 0, 0.63, 0.7, 0.74, 0.68, 1, 1, 1, 
    1, 0, 0, 0, 0.62, 0.63, 0.6, 0.63, 0.6, 0.68, 1, 1, 1), Fnn = c(0, 
    0.64, 1, 0, 0.51, 1, 0, 0.67, 1, 0, 0.62, 0.69, 1, 0, 0.54, 
    0.62, 1, 0, 0.63, 0.73, 1, 0, 0.63, 1, 0, 0.7, 1, 0, 0.7, 
    1, 0, 0.58, 1), Qm = c(1, 1, 1, 0.65, 0.45, 0.36, 0.5, 0.165, 
    0, 1, 1, 1, 1, 0.685, 0.38, 0.32, 0.32, 0.5, 0.185, 0.135, 
    0, 1, 1, 1, 0.69, 0.37, 0.4, 0.685, 0.4, 0.32, 0.5, 0.21, 
    0), Qn = c(1, 0.36, 0, 0.65, 0.45, 0, 0.5, 0.165, 0, 1, 0.38, 
    0.31, 0, 0.685, 0.38, 0.32, 0, 0.5, 0.185, 0.135, 0, 1, 0.37, 
    0, 0.69, 0.3, 0, 0.685, 0.3, 0, 0.5, 0.21, 0), term1_p0 = c(0.81450625, 
    0.0857375, 0.00225625, 0.0857375, 0.009025, 0.0002375, 0.00225625, 
    0.0002375, 6.25e-06, 0.7737809375, 0.1221759375, 0.00643031249999999, 
    0.0001128125, 0.081450625, 0.012860625, 0.000676875, 1.1875e-05, 
    0.0021434375, 0.0003384375, 1.78125e-05, 3.125e-07, 0.7737809375, 
    0.081450625, 0.0021434375, 0.1221759375, 0.012860625, 0.0003384375, 
    0.00643031249999999, 0.000676875, 1.78125e-05, 0.0001128125, 
    1.1875e-05, 3.125e-07), term1_p1 = c(0.4096, 0.2048, 0.0256, 
    0.2048, 0.1024, 0.0128, 0.0256, 0.0128, 0.0016, 0.32768, 
    0.24576, 0.06144, 0.00512, 0.16384, 0.12288, 0.03072, 0.00256, 
    0.02048, 0.01536, 0.00384, 0.00032, 0.32768, 0.16384, 0.02048, 
    0.24576, 0.12288, 0.01536, 0.06144, 0.03072, 0.00384, 0.00512, 
    0.00256, 0.00032)), .Names = c("m1", "n1", "x1", "y1", "Fmm", 
"Fnn", "Qm", "Qn", "term1_p0", "term1_p1"), row.names = c(NA, 
33L), class = "data.frame")

library(zoo)
lst1<- split(d,list(d$m1,d$n1))
res2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){
x[,11:14]<-NA;
x[,11:12][x$Qm<=c11,]<-cumsum(x[,9:10][x$Qm<=c11,]);
x[,13:14][x$Qn<=c12,]<-cumsum(x[,9:10][x$Qn<=c12,]);
colnames(x)[11:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H");
x1<-na.locf(x);
x1[,11:14][is.na(x1[,11:14])]<-0;
x1}))
row.names(res2)<- 1:nrow(res2)

 res2
 #  m1 n1 x1 y1  Fmm  Fnn    Qm    Qn     term1_p0 term1_p1   cterm1_P0L
cterm1_P1L   cterm1_P0H cterm1_P1H

#1   2  2  0  0 0.00 0.00 1.000 1.000 0.8145062500  0.40960 0.0000000000  
 0.00000 0.0000000000    0.00000
#2   2  2  0  1 0.00 0.64 1.000 0.360 0.0857375000  0.20480 0.0000000000  
 0.00000 0.0000000000    0.00000
#3   2  2  0  2 0.00 1.00 1.000 0.000 0.0022562500  0.02560 0.0000000000  
 0.00000 0.0022562500    0.02560
#4   2  2  1  0 0.70 0.00 0.650 0.650 0.0857375000  0.20480 0.0000000000  
 0.00000 0.0022562500    0.02560
#5   2  2  1  1 0.59 0.51 0.450 0.450 0.0090250000  0.10240 0.0000000000  
 0.00000 0.0022562500    0.02560
#6   2  2  1  2 0.64 1.00 0.360 0.000 0.0002375000  0.01280 0.0000000000  
 0.00000 0.0024937500    0.03840
#7   2  2  2  0 1.00 0.00 0.500 0.500 0.0022562500  0.02560 0.0000000000  
 0.00000 0.0024937500    0.03840
#8   2  2  2  1 1.00 0.67 0.165 0.165 0.0002375000  0.01280 0.0002375000  
 0.01280 0.0027312500    0.05120
#9   2  2  2  2 1.00 1.00 0.000 0.000 0.0000062500  0.00160 0.0002437500  
 0.01440 0.0027375000    0.05280
#10  3  2  0  0 0.00 0.00 1.000 1.000 0.7737809375  0.32768 0.0000000000  
 0.00000 0.0000000000    0.00000
#11  3  2  0  1 0.00 0.63 1.000 0.370 0.0814506250  0.16384 0.0000000000  
 0.00000 0.0000000000    0.00000
#12  3  2  0  2 0.00 1.00 1.000 0.000 0.0021434375  0.02048 0.0000000000  
 0.00000 0.0021434375    0.02048
#13  3  2  1  0 0.62 0.00 0.690 0.690 0.1221759375  0.24576 0.0000000000  
 0.00000 0.0021434375    0.02048
#14  3  2  1  1 0.63 0.70 0.370 0.300 0.0128606250  0.12288 0.0000000000  
 0.00000 0.0021434375    0.02048
#15  3  2  1  2 0.60 1.00 0.400 0.000 0.0003384375  0.01536 0.0000000000  
 0.00000 0.0024818750    0.03584
#16  3  2  2  0 0.63 0.00 0.685 0.685 0.0064303125  0.06144 0.0000000000  
 0.00000 0.0024818750    0.03584
#17  3  2  2  1 0.60 0.70 0.400 0.300 0.0006768750  0.03072 0.0000000000  
 0.00000 0.0024818750    0.03584
#18  3  2  2  2 0.68 1.00 0.320 0.000 0.0000178125  0.00384 0.0000000000  
 0.00000 0.0024996875    0.03968
#19  3  2  3  0 1.00 0.00 0.500 0.500 0.0001128125  0.00512 0.0000000000  
 0.00000 0.0024996875    0.03968
#20  3  2  3  1 1.00 0.58 0.210 0.210 0.0000118750  0.00256 0.0000000000  
 0.00000 0.0024996875    0.03968
#21  3  2  3  2 1.00 1.00 0.000 0.000 0.0000003125  0.00032 0.0000003125  
 0.00032 0.0025000000    0.04000
#22  2  3  0  0 0.00 0.00 1.000 1.000 0.7737809375  0.32768 0.0000000000  
 0.00000 0.0000000000    0.00000
#23  2  3  0  1 0.00 0.62 1.000 0.380 0.1221759375  0.24576 0.0000000000  
 0.00000 0.0000000000    0.00000
#24  2  3  0  2 0.00 0.69 1.000 0.310 0.0064303125  0.06144 0.0000000000  
 0.00000 0.0000000000    0.00000
#25  2  3  0  3 0.00 1.00 1.000 0.000 0.0001128125  0.00512 0.0000000000  
 0.00000 0.0001128125    0.00512
#26  2  3  1  0 0.63 0.00 0.685 0.685 0.0814506250  0.16384 0.0000000000  
 0.00000 0.0001128125    0.00512
#27  2  3  1  1 0.70 0.54 0.380 0.380 0.0128606250  0.12288 0.0000000000  
 0.00000 0.0001128125    0.00512
#28  2  3  1  2 0.74 0.62 0.320 0.320 0.0006768750  0.03072 0.0000000000  
 0.00000 0.0001128125    0.00512
#29  2  3  1  3 0.68 1.00 0.320 0.000 0.0000118750  0.00256 0.0000000000  
 0.00000 0.0001246875    0.00768
#30  2  3  2  0 1.00 0.00 0.500 0.500 0.0021434375  0.02048 0.0000000000  
 0.00000 0.0001246875    0.00768
#31  2  3  2  1 1.00 0.63 0.185 0.185 0.0003384375  0.01536 0.0003384375  
 0.01536 0.0004631250    0.02304
#32  2  3  2  2 1.00 0.73 0.135 0.135 0.0000178125  0.00384 0.0003562500  
 0.01920 0.0004809375    0.02688  
#33  2  3  2  3 1.00 1.00 0.000 0.000 0.0000003125  0.00032 0.0003565625  
 0.01952 0.0004812500    0.02720

#Sorry, some values in my previous solution didn't look right. I didn't 
A.K.





----- Original Message -----
From: Zjoanna <[hidden email]>
To: [hidden email]
Cc:
Sent: Friday, February 1, 2013 12:19 PM
Subject: Re: [R] cumulative sum by group and under some criteria

Thank you very much for your reply. Your code work well with this example.
I modified a little to fit my real data, I got an error massage.

Error in split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...) :
  Group length is 0 but data length > 0


On Thu, Jan 31, 2013 at 12:21 PM, arun kirshna [via R] <
[hidden email]> wrote:

> Hi,
> Try this:
> colnames(d)<-c("m1","n1","x1","y1","p11","p12")
> library(zoo)
> res1<- do.call(rbind,lapply(lapply(split(d,list(d$m1,d$n1)),function(x)
> {x$cp11[x$x1>1]<- cumsum(x$p11[x$x1>1]);x$cp12[x$y1>1]<-
> cumsum(x$p12[x$y1>1]);x}),function(x)
> {x$cp11<-na.locf(x$cp11,na.rm=F);x$cp12<- na.locf(x$cp12,na.rm=F);x}))
> #there would be a warning here as one of the list element is NULL.  The,
> warning is okay
> row.names(res1)<- 1:nrow(res1)
> res1[,7:8][is.na(res1[,7:8])]<- 0
> res1
>  #  m1 n1 x1 y1  p11  p12 cp11 cp12
> #1   2  2  0  0 0.00 0.00 0.00 0.00
> #2   2  2  0  1 0.00 0.50 0.00 0.00
> #3   2  2  0  2 0.00 1.00 0.00 1.00
> #4   2  2  1  0 0.50 0.00 0.00 1.00
> #5   2  2  1  1 0.50 0.50 0.00 1.00
> #6   2  2  1  2 0.50 1.00 0.00 2.00
> #7   2  2  2  0 1.00 0.00 1.00 2.00
> #8   2  2  2  1 1.00 0.50 2.00 2.00
> #9   2  2  2  2 1.00 1.00 3.00 3.00
> #10  3  2  0  0 0.00 0.00 0.00 0.00
> #11  3  2  0  1 0.00 0.50 0.00 0.00
> #12  3  2  0  2 0.00 1.00 0.00 1.00
> #13  3  2  1  0 0.33 0.00 0.00 1.00
> #14  3  2  1  1 0.33 0.50 0.00 1.00
> #15  3  2  1  2 0.33 1.00 0.00 2.00
> #16  3  2  2  0 0.67 0.00 0.67 2.00
> #17  3  2  2  1 0.67 0.50 1.34 2.00
> #18  3  2  2  2 0.67 1.00 2.01 3.00
> #19  3  2  3  0 1.00 0.00 3.01 3.00
> #20  3  2  3  1 1.00 0.50 4.01 3.00
> #21  3  2  3  2 1.00 1.00 5.01 4.00
> #22  2  3  0  0 0.00 0.00 0.00 0.00
> #23  2  3  0  1 0.00 0.33 0.00 0.00
> #24  2  3  0  2 0.00 0.67 0.00 0.67
> #25  2  3  0  3 0.00 1.00 0.00 1.67
> #26  2  3  1  0 0.50 0.00 0.00 1.67
> #27  2  3  1  1 0.50 0.33 0.00 1.67
> #28  2  3  1  2 0.50 0.67 0.00 2.34
> #29  2  3  1  3 0.50 1.00 0.00 3.34
> #30  2  3  2  0 1.00 0.00 1.00 3.34
> #31  2  3  2  1 1.00 0.33 2.00 3.34
> #32  2  3  2  2 1.00 0.67 3.00 4.01
> #33  2  3  2  3 1.00 1.00 4.00 5.01
> A.K.
>
> ------------------------------
>  If you reply to this email, your message will be added to the discussion
> below:
>
> http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657196.html
> To unsubscribe from cumulative sum by group and under some criteria, click
> here<
> .
> NAML<
http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




--
View this message in context:
http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657315.html
Sent from the R help mailing list archive at Nabble.com.
    [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

</quote>
Quoted from:
http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657360.html


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

Zjoanna
This post has NOT been accepted by the mailing list yet.
Hi,
Thanks. This extract every row that satisfy the condition, but I need look at the last row (the maximum of cumulative sum) for each block (m1,n1). for example, if I set the criteria
res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95, this should extract m1= 3, n1 = 2.
Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

arun kirshna
In reply to this post by Zjoanna


Hi,
Thanks. This extract every row that satisfy the condition, but I need look at the last row (the maximum of cumulative sum) for each block (m1,n1). for example, if I set the criteria 

res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95, this should extract m1= 3, n1 = 2. 


Hi,
I am not sure I understand your question.
res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95
 #[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#[16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#[31] TRUE TRUE TRUE

This will extract all the rows.


res2[,1:2][res2$cterm1_P1L<0.01 & res2$cterm1_P1L!=0,]  
#   m1 n1
#21  3  2
This extract only the row you wanted.  

For the different groups:

aggregate(cterm1_P1L~m1+n1,data=res2,max)
#  m1 n1 cterm1_P1L
#1  2  2    0.01440
#2  3  2    0.00032
#3  2  3    0.01952
 
 aggregate(cterm1_P1L~m1+n1,data=res2,function(x) max(x)<0.01)
 # m1 n1 cterm1_P1L
#1  2  2      FALSE
#2  3  2       TRUE
#3  2  3      FALSE

res4<-aggregate(cterm1_P1L~m1+n1,data=res2,function(x) max(x)<0.01)
res4[,1:2][res4[,3],]
#  m1 n1
#2  3  2

A.K.




----- Original Message -----
From: "[hidden email]" <[hidden email]>
To: [hidden email]
Cc:
Sent: Sunday, February 3, 2013 3:58 PM
Subject: Re: cumulative sum by group and under some criteria

Hi,
Let me restate my questions. I need to get the m1 and n1 that satisfy some criteria, for example in this case, within each group, the maximum cterm1_p1L ( the last row in this group) <0.01. I need to extract m1=3, n1=2, I only need m1, n1 in the row.

Also, how to create the structure from the data.frame, I am new to R, I need to change the maxN and run the loop to different data.
Thanks very much for your help!

<quote author='arun kirshna'>
HI,

I think this should be more correct:
maxN<-9 
c11<-0.2 
c12<-0.2 
p0L<-0.05 
p0H<-0.05 
p1L<-0.20 
p1H<-0.20 

d <- structure(list(m1 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), 
    n1 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 
    3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), x1 = c(0, 
    0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 
    2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3), y1 = c(0, 1, 2, 0, 
    1, 2, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 
    2, 0, 1, 2, 0, 1, 2, 0, 1, 2), Fmm = c(0, 0, 0, 0.7, 0.59, 
    0.64, 1, 1, 1, 0, 0, 0, 0, 0.63, 0.7, 0.74, 0.68, 1, 1, 1, 
    1, 0, 0, 0, 0.62, 0.63, 0.6, 0.63, 0.6, 0.68, 1, 1, 1), Fnn = c(0, 
    0.64, 1, 0, 0.51, 1, 0, 0.67, 1, 0, 0.62, 0.69, 1, 0, 0.54, 
    0.62, 1, 0, 0.63, 0.73, 1, 0, 0.63, 1, 0, 0.7, 1, 0, 0.7, 
    1, 0, 0.58, 1), Qm = c(1, 1, 1, 0.65, 0.45, 0.36, 0.5, 0.165, 
    0, 1, 1, 1, 1, 0.685, 0.38, 0.32, 0.32, 0.5, 0.185, 0.135, 
    0, 1, 1, 1, 0.69, 0.37, 0.4, 0.685, 0.4, 0.32, 0.5, 0.21, 
    0), Qn = c(1, 0.36, 0, 0.65, 0.45, 0, 0.5, 0.165, 0, 1, 0.38, 
    0.31, 0, 0.685, 0.38, 0.32, 0, 0.5, 0.185, 0.135, 0, 1, 0.37, 
    0, 0.69, 0.3, 0, 0.685, 0.3, 0, 0.5, 0.21, 0), term1_p0 = c(0.81450625, 
    0.0857375, 0.00225625, 0.0857375, 0.009025, 0.0002375, 0.00225625, 
    0.0002375, 6.25e-06, 0.7737809375, 0.1221759375, 0.00643031249999999, 
    0.0001128125, 0.081450625, 0.012860625, 0.000676875, 1.1875e-05, 
    0.0021434375, 0.0003384375, 1.78125e-05, 3.125e-07, 0.7737809375, 
    0.081450625, 0.0021434375, 0.1221759375, 0.012860625, 0.0003384375, 
    0.00643031249999999, 0.000676875, 1.78125e-05, 0.0001128125, 
    1.1875e-05, 3.125e-07), term1_p1 = c(0.4096, 0.2048, 0.0256, 
    0.2048, 0.1024, 0.0128, 0.0256, 0.0128, 0.0016, 0.32768, 
    0.24576, 0.06144, 0.00512, 0.16384, 0.12288, 0.03072, 0.00256, 
    0.02048, 0.01536, 0.00384, 0.00032, 0.32768, 0.16384, 0.02048, 
    0.24576, 0.12288, 0.01536, 0.06144, 0.03072, 0.00384, 0.00512, 
    0.00256, 0.00032)), .Names = c("m1", "n1", "x1", "y1", "Fmm", 
"Fnn", "Qm", "Qn", "term1_p0", "term1_p1"), row.names = c(NA, 
33L), class = "data.frame")

library(zoo)
lst1<- split(d,list(d$m1,d$n1))
res2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){
x[,11:14]<-NA;
x[,11:12][x$Qm<=c11,]<-cumsum(x[,9:10][x$Qm<=c11,]);
x[,13:14][x$Qn<=c12,]<-cumsum(x[,9:10][x$Qn<=c12,]);
colnames(x)[11:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H");
x1<-na.locf(x);
x1[,11:14][is.na(x1[,11:14])]<-0;
x1}))
row.names(res2)<- 1:nrow(res2)

 res2
 #  m1 n1 x1 y1  Fmm  Fnn    Qm    Qn     term1_p0 term1_p1   cterm1_P0L
cterm1_P1L   cterm1_P0H cterm1_P1H

#1   2  2  0  0 0.00 0.00 1.000 1.000 0.8145062500  0.40960 0.0000000000  
 0.00000 0.0000000000    0.00000
#2   2  2  0  1 0.00 0.64 1.000 0.360 0.0857375000  0.20480 0.0000000000  
 0.00000 0.0000000000    0.00000
#3   2  2  0  2 0.00 1.00 1.000 0.000 0.0022562500  0.02560 0.0000000000  
 0.00000 0.0022562500    0.02560
#4   2  2  1  0 0.70 0.00 0.650 0.650 0.0857375000  0.20480 0.0000000000  
 0.00000 0.0022562500    0.02560
#5   2  2  1  1 0.59 0.51 0.450 0.450 0.0090250000  0.10240 0.0000000000  
 0.00000 0.0022562500    0.02560
#6   2  2  1  2 0.64 1.00 0.360 0.000 0.0002375000  0.01280 0.0000000000  
 0.00000 0.0024937500    0.03840
#7   2  2  2  0 1.00 0.00 0.500 0.500 0.0022562500  0.02560 0.0000000000  
 0.00000 0.0024937500    0.03840
#8   2  2  2  1 1.00 0.67 0.165 0.165 0.0002375000  0.01280 0.0002375000  
 0.01280 0.0027312500    0.05120
#9   2  2  2  2 1.00 1.00 0.000 0.000 0.0000062500  0.00160 0.0002437500  
 0.01440 0.0027375000    0.05280
#10  3  2  0  0 0.00 0.00 1.000 1.000 0.7737809375  0.32768 0.0000000000  
 0.00000 0.0000000000    0.00000
#11  3  2  0  1 0.00 0.63 1.000 0.370 0.0814506250  0.16384 0.0000000000  
 0.00000 0.0000000000    0.00000
#12  3  2  0  2 0.00 1.00 1.000 0.000 0.0021434375  0.02048 0.0000000000  
 0.00000 0.0021434375    0.02048
#13  3  2  1  0 0.62 0.00 0.690 0.690 0.1221759375  0.24576 0.0000000000  
 0.00000 0.0021434375    0.02048
#14  3  2  1  1 0.63 0.70 0.370 0.300 0.0128606250  0.12288 0.0000000000  
 0.00000 0.0021434375    0.02048
#15  3  2  1  2 0.60 1.00 0.400 0.000 0.0003384375  0.01536 0.0000000000  
 0.00000 0.0024818750    0.03584
#16  3  2  2  0 0.63 0.00 0.685 0.685 0.0064303125  0.06144 0.0000000000  
 0.00000 0.0024818750    0.03584
#17  3  2  2  1 0.60 0.70 0.400 0.300 0.0006768750  0.03072 0.0000000000  
 0.00000 0.0024818750    0.03584
#18  3  2  2  2 0.68 1.00 0.320 0.000 0.0000178125  0.00384 0.0000000000  
 0.00000 0.0024996875    0.03968
#19  3  2  3  0 1.00 0.00 0.500 0.500 0.0001128125  0.00512 0.0000000000  
 0.00000 0.0024996875    0.03968
#20  3  2  3  1 1.00 0.58 0.210 0.210 0.0000118750  0.00256 0.0000000000  
 0.00000 0.0024996875    0.03968
#21  3  2  3  2 1.00 1.00 0.000 0.000 0.0000003125  0.00032 0.0000003125  
 0.00032 0.0025000000    0.04000
#22  2  3  0  0 0.00 0.00 1.000 1.000 0.7737809375  0.32768 0.0000000000  
 0.00000 0.0000000000    0.00000
#23  2  3  0  1 0.00 0.62 1.000 0.380 0.1221759375  0.24576 0.0000000000  
 0.00000 0.0000000000    0.00000
#24  2  3  0  2 0.00 0.69 1.000 0.310 0.0064303125  0.06144 0.0000000000  
 0.00000 0.0000000000    0.00000
#25  2  3  0  3 0.00 1.00 1.000 0.000 0.0001128125  0.00512 0.0000000000  
 0.00000 0.0001128125    0.00512
#26  2  3  1  0 0.63 0.00 0.685 0.685 0.0814506250  0.16384 0.0000000000  
 0.00000 0.0001128125    0.00512
#27  2  3  1  1 0.70 0.54 0.380 0.380 0.0128606250  0.12288 0.0000000000  
 0.00000 0.0001128125    0.00512
#28  2  3  1  2 0.74 0.62 0.320 0.320 0.0006768750  0.03072 0.0000000000  
 0.00000 0.0001128125    0.00512
#29  2  3  1  3 0.68 1.00 0.320 0.000 0.0000118750  0.00256 0.0000000000  
 0.00000 0.0001246875    0.00768
#30  2  3  2  0 1.00 0.00 0.500 0.500 0.0021434375  0.02048 0.0000000000  
 0.00000 0.0001246875    0.00768
#31  2  3  2  1 1.00 0.63 0.185 0.185 0.0003384375  0.01536 0.0003384375  
 0.01536 0.0004631250    0.02304
#32  2  3  2  2 1.00 0.73 0.135 0.135 0.0000178125  0.00384 0.0003562500  
 0.01920 0.0004809375    0.02688  
#33  2  3  2  3 1.00 1.00 0.000 0.000 0.0000003125  0.00032 0.0003565625  
 0.01952 0.0004812500    0.02720

#Sorry, some values in my previous solution didn't look right. I didn't 
A.K.





----- Original Message -----
From: Zjoanna <[hidden email]>
To: [hidden email]
Cc:
Sent: Friday, February 1, 2013 12:19 PM
Subject: Re: [R] cumulative sum by group and under some criteria

Thank you very much for your reply. Your code work well with this example.
I modified a little to fit my real data, I got an error massage.

Error in split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...) :
  Group length is 0 but data length > 0


On Thu, Jan 31, 2013 at 12:21 PM, arun kirshna [via R] <
[hidden email]> wrote:

> Hi,
> Try this:
> colnames(d)<-c("m1","n1","x1","y1","p11","p12")
> library(zoo)
> res1<- do.call(rbind,lapply(lapply(split(d,list(d$m1,d$n1)),function(x)
> {x$cp11[x$x1>1]<- cumsum(x$p11[x$x1>1]);x$cp12[x$y1>1]<-
> cumsum(x$p12[x$y1>1]);x}),function(x)
> {x$cp11<-na.locf(x$cp11,na.rm=F);x$cp12<- na.locf(x$cp12,na.rm=F);x}))
> #there would be a warning here as one of the list element is NULL.  The,
> warning is okay
> row.names(res1)<- 1:nrow(res1)
> res1[,7:8][is.na(res1[,7:8])]<- 0
> res1
>  #  m1 n1 x1 y1  p11  p12 cp11 cp12
> #1   2  2  0  0 0.00 0.00 0.00 0.00
> #2   2  2  0  1 0.00 0.50 0.00 0.00
> #3   2  2  0  2 0.00 1.00 0.00 1.00
> #4   2  2  1  0 0.50 0.00 0.00 1.00
> #5   2  2  1  1 0.50 0.50 0.00 1.00
> #6   2  2  1  2 0.50 1.00 0.00 2.00
> #7   2  2  2  0 1.00 0.00 1.00 2.00
> #8   2  2  2  1 1.00 0.50 2.00 2.00
> #9   2  2  2  2 1.00 1.00 3.00 3.00
> #10  3  2  0  0 0.00 0.00 0.00 0.00
> #11  3  2  0  1 0.00 0.50 0.00 0.00
> #12  3  2  0  2 0.00 1.00 0.00 1.00
> #13  3  2  1  0 0.33 0.00 0.00 1.00
> #14  3  2  1  1 0.33 0.50 0.00 1.00
> #15  3  2  1  2 0.33 1.00 0.00 2.00
> #16  3  2  2  0 0.67 0.00 0.67 2.00
> #17  3  2  2  1 0.67 0.50 1.34 2.00
> #18  3  2  2  2 0.67 1.00 2.01 3.00
> #19  3  2  3  0 1.00 0.00 3.01 3.00
> #20  3  2  3  1 1.00 0.50 4.01 3.00
> #21  3  2  3  2 1.00 1.00 5.01 4.00
> #22  2  3  0  0 0.00 0.00 0.00 0.00
> #23  2  3  0  1 0.00 0.33 0.00 0.00
> #24  2  3  0  2 0.00 0.67 0.00 0.67
> #25  2  3  0  3 0.00 1.00 0.00 1.67
> #26  2  3  1  0 0.50 0.00 0.00 1.67
> #27  2  3  1  1 0.50 0.33 0.00 1.67
> #28  2  3  1  2 0.50 0.67 0.00 2.34
> #29  2  3  1  3 0.50 1.00 0.00 3.34
> #30  2  3  2  0 1.00 0.00 1.00 3.34
> #31  2  3  2  1 1.00 0.33 2.00 3.34
> #32  2  3  2  2 1.00 0.67 3.00 4.01
> #33  2  3  2  3 1.00 1.00 4.00 5.01
> A.K.
>
> ------------------------------
>  If you reply to this email, your message will be added to the discussion
> below:
>
> http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657196.html
> To unsubscribe from cumulative sum by group and under some criteria, click
> here<
> .
> NAML<
http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




--
View this message in context:
http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657315.html
Sent from the R help mailing list archive at Nabble.com.
    [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

</quote>
Quoted from:
http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657360.html


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

arun kirshna

Hi,
res3<-with(res2,aggregate(cbind(cterm1_P1L,cterm1_P0H),by=list(m1,n1),max))

 res3[res3[,3]<0.6 & res3[,4]<0.95,] #this doesn't change the result as the conditions are  not met
#  Group.1 Group.2 cterm1_P1L cterm1_P0H
#1       2       2    0.01440 0.00273750
#2       3       2    0.00032 0.00250000
#3       2       3    0.01952 0.00048125

res3[res3[,3]<0.01 & res3[,4]<0.01,]
 # Group.1 Group.2 cterm1_P1L cterm1_P0H
#2       3       2    0.00032     0.0025
Hope it helps.
A.K.
________________________________
From: Joanna Zhang <[hidden email]>
To: arun <[hidden email]>
Sent: Tuesday, February 5, 2013 9:43 AM
Subject: Re: cumulative sum by group and under some criteria


Yes, it did answer my question, max(x) look at the maximum value. However, how to put both criteria in the code, like both the maximum value of cterm1_p1L <0.6 and the maximum value of cterm1_p0L<0.95. Thanks a lot! 


On Mon, Feb 4, 2013 at 4:26 PM, arun <[hidden email]> wrote:


>
>Hi,
>
>Did this answered your question?
>
>A.K.
>
>
>----- Original Message -----
>
>From: arun <[hidden email]>
>To: "[hidden email]" <[hidden email]>
>Cc: R help <[hidden email]>
>Sent: Monday, February 4, 2013 4:44 PM
>Subject: Re: cumulative sum by group and under some criteria
>
>
>
>Hi,
>Thanks. This extract every row that satisfy the condition, but I need look at the last row (the maximum of cumulative sum) for each block (m1,n1). for example, if I set the criteria 
>
>res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95, this should extract m1= 3, n1 = 2. 
>
>
>Hi,
>I am not sure I understand your question.
>res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95
> #[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
>#[16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
>#[31] TRUE TRUE TRUE
>
>This will extract all the rows.
>
>
>res2[,1:2][res2$cterm1_P1L<0.01 & res2$cterm1_P1L!=0,]  
>#   m1 n1
>#21  3  2
>This extract only the row you wanted.  
>
>For the different groups:
>
>aggregate(cterm1_P1L~m1+n1,data=res2,max)
>#  m1 n1 cterm1_P1L
>#1  2  2    0.01440
>#2  3  2    0.00032
>#3  2  3    0.01952

> aggregate(cterm1_P1L~m1+n1,data=res2,function(x) max(x)<0.01)
> # m1 n1 cterm1_P1L
>#1  2  2      FALSE
>#2  3  2       TRUE
>#3  2  3      FALSE
>
>res4<-aggregate(cterm1_P1L~m1+n1,data=res2,function(x) max(x)<0.01)
>res4[,1:2][res4[,3],]
>#  m1 n1
>#2  3  2
>
>A.K.
>
>
>
>
>----- Original Message -----
>From: "[hidden email]" <[hidden email]>
>To: [hidden email]
>Cc:
>Sent: Sunday, February 3, 2013 3:58 PM
>Subject: Re: cumulative sum by group and under some criteria
>
>Hi,
>Let me restate my questions. I need to get the m1 and n1 that satisfy some criteria, for example in this case, within each group, the maximum cterm1_p1L ( the last row in this group) <0.01. I need to extract m1=3, n1=2, I only need m1, n1 in the row.
>
>Also, how to create the structure from the data.frame, I am new to R, I need to change the maxN and run the loop to different data.
>Thanks very much for your help!
>
><quote author='arun kirshna'>
>HI,
>
>I think this should be more correct:
>maxN<-9 
>c11<-0.2 
>c12<-0.2 
>p0L<-0.05 
>p0H<-0.05 
>p1L<-0.20 
>p1H<-0.20 
>
>d <- structure(list(m1 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
>2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), 
>    n1 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 
>    3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), x1 = c(0, 
>    0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 
>    2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3), y1 = c(0, 1, 2, 0, 
>    1, 2, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 
>    2, 0, 1, 2, 0, 1, 2, 0, 1, 2), Fmm = c(0, 0, 0, 0.7, 0.59, 
>    0.64, 1, 1, 1, 0, 0, 0, 0, 0.63, 0.7, 0.74, 0.68, 1, 1, 1, 
>    1, 0, 0, 0, 0.62, 0.63, 0.6, 0.63, 0.6, 0.68, 1, 1, 1), Fnn = c(0, 
>    0.64, 1, 0, 0.51, 1, 0, 0.67, 1, 0, 0.62, 0.69, 1, 0, 0.54, 
>    0.62, 1, 0, 0.63, 0.73, 1, 0, 0.63, 1, 0, 0.7, 1, 0, 0.7, 
>    1, 0, 0.58, 1), Qm = c(1, 1, 1, 0.65, 0.45, 0.36, 0.5, 0.165, 
>    0, 1, 1, 1, 1, 0.685, 0.38, 0.32, 0.32, 0.5, 0.185, 0.135, 
>    0, 1, 1, 1, 0.69, 0.37, 0.4, 0.685, 0.4, 0.32, 0.5, 0.21, 
>    0), Qn = c(1, 0.36, 0, 0.65, 0.45, 0, 0.5, 0.165, 0, 1, 0.38, 
>    0.31, 0, 0.685, 0.38, 0.32, 0, 0.5, 0.185, 0.135, 0, 1, 0.37, 
>    0, 0.69, 0.3, 0, 0.685, 0.3, 0, 0.5, 0.21, 0), term1_p0 = c(0.81450625, 
>    0.0857375, 0.00225625, 0.0857375, 0.009025, 0.0002375, 0.00225625, 
>    0.0002375, 6.25e-06, 0.7737809375, 0.1221759375, 0.00643031249999999, 
>    0.0001128125, 0.081450625, 0.012860625, 0.000676875, 1.1875e-05, 
>    0.0021434375, 0.0003384375, 1.78125e-05, 3.125e-07, 0.7737809375, 
>    0.081450625, 0.0021434375, 0.1221759375, 0.012860625, 0.0003384375, 
>    0.00643031249999999, 0.000676875, 1.78125e-05, 0.0001128125, 
>    1.1875e-05, 3.125e-07), term1_p1 = c(0.4096, 0.2048, 0.0256, 
>    0.2048, 0.1024, 0.0128, 0.0256, 0.0128, 0.0016, 0.32768, 
>    0.24576, 0.06144, 0.00512, 0.16384, 0.12288, 0.03072, 0.00256, 
>    0.02048, 0.01536, 0.00384, 0.00032, 0.32768, 0.16384, 0.02048, 
>    0.24576, 0.12288, 0.01536, 0.06144, 0.03072, 0.00384, 0.00512, 
>    0.00256, 0.00032)), .Names = c("m1", "n1", "x1", "y1", "Fmm", 
>"Fnn", "Qm", "Qn", "term1_p0", "term1_p1"), row.names = c(NA, 
>33L), class = "data.frame")
>
>library(zoo)
>lst1<- split(d,list(d$m1,d$n1))
>res2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){
>x[,11:14]<-NA;
>x[,11:12][x$Qm<=c11,]<-cumsum(x[,9:10][x$Qm<=c11,]);
>x[,13:14][x$Qn<=c12,]<-cumsum(x[,9:10][x$Qn<=c12,]);
>colnames(x)[11:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H");
>x1<-na.locf(x);
>x1[,11:14][is.na(x1[,11:14])]<-0;
>x1}))
>row.names(res2)<- 1:nrow(res2)
>
> res2
> #  m1 n1 x1 y1  Fmm  Fnn    Qm    Qn     term1_p0 term1_p1   cterm1_P0L
>cterm1_P1L   cterm1_P0H cterm1_P1H
>
>#1   2  2  0  0 0.00 0.00 1.000 1.000 0.8145062500  0.40960 0.0000000000  
> 0.00000 0.0000000000    0.00000
>#2   2  2  0  1 0.00 0.64 1.000 0.360 0.0857375000  0.20480 0.0000000000  
> 0.00000 0.0000000000    0.00000
>#3   2  2  0  2 0.00 1.00 1.000 0.000 0.0022562500  0.02560 0.0000000000  
> 0.00000 0.0022562500    0.02560
>#4   2  2  1  0 0.70 0.00 0.650 0.650 0.0857375000  0.20480 0.0000000000  
> 0.00000 0.0022562500    0.02560
>#5   2  2  1  1 0.59 0.51 0.450 0.450 0.0090250000  0.10240 0.0000000000  
> 0.00000 0.0022562500    0.02560
>#6   2  2  1  2 0.64 1.00 0.360 0.000 0.0002375000  0.01280 0.0000000000  
> 0.00000 0.0024937500    0.03840
>#7   2  2  2  0 1.00 0.00 0.500 0.500 0.0022562500  0.02560 0.0000000000  
> 0.00000 0.0024937500    0.03840
>#8   2  2  2  1 1.00 0.67 0.165 0.165 0.0002375000  0.01280 0.0002375000  
> 0.01280 0.0027312500    0.05120
>#9   2  2  2  2 1.00 1.00 0.000 0.000 0.0000062500  0.00160 0.0002437500  
> 0.01440 0.0027375000    0.05280
>#10  3  2  0  0 0.00 0.00 1.000 1.000 0.7737809375  0.32768 0.0000000000  
> 0.00000 0.0000000000    0.00000
>#11  3  2  0  1 0.00 0.63 1.000 0.370 0.0814506250  0.16384 0.0000000000  
> 0.00000 0.0000000000    0.00000
>#12  3  2  0  2 0.00 1.00 1.000 0.000 0.0021434375  0.02048 0.0000000000  
> 0.00000 0.0021434375    0.02048
>#13  3  2  1  0 0.62 0.00 0.690 0.690 0.1221759375  0.24576 0.0000000000  
> 0.00000 0.0021434375    0.02048
>#14  3  2  1  1 0.63 0.70 0.370 0.300 0.0128606250  0.12288 0.0000000000  
> 0.00000 0.0021434375    0.02048
>#15  3  2  1  2 0.60 1.00 0.400 0.000 0.0003384375  0.01536 0.0000000000  
> 0.00000 0.0024818750    0.03584
>#16  3  2  2  0 0.63 0.00 0.685 0.685 0.0064303125  0.06144 0.0000000000  
> 0.00000 0.0024818750    0.03584
>#17  3  2  2  1 0.60 0.70 0.400 0.300 0.0006768750  0.03072 0.0000000000  
> 0.00000 0.0024818750    0.03584
>#18  3  2  2  2 0.68 1.00 0.320 0.000 0.0000178125  0.00384 0.0000000000  
> 0.00000 0.0024996875    0.03968
>#19  3  2  3  0 1.00 0.00 0.500 0.500 0.0001128125  0.00512 0.0000000000  
> 0.00000 0.0024996875    0.03968
>#20  3  2  3  1 1.00 0.58 0.210 0.210 0.0000118750  0.00256 0.0000000000  
> 0.00000 0.0024996875    0.03968
>#21  3  2  3  2 1.00 1.00 0.000 0.000 0.0000003125  0.00032 0.0000003125  
> 0.00032 0.0025000000    0.04000
>#22  2  3  0  0 0.00 0.00 1.000 1.000 0.7737809375  0.32768 0.0000000000  
> 0.00000 0.0000000000    0.00000
>#23  2  3  0  1 0.00 0.62 1.000 0.380 0.1221759375  0.24576 0.0000000000  
> 0.00000 0.0000000000    0.00000
>#24  2  3  0  2 0.00 0.69 1.000 0.310 0.0064303125  0.06144 0.0000000000  
> 0.00000 0.0000000000    0.00000
>#25  2  3  0  3 0.00 1.00 1.000 0.000 0.0001128125  0.00512 0.0000000000  
> 0.00000 0.0001128125    0.00512
>#26  2  3  1  0 0.63 0.00 0.685 0.685 0.0814506250  0.16384 0.0000000000  
> 0.00000 0.0001128125    0.00512
>#27  2  3  1  1 0.70 0.54 0.380 0.380 0.0128606250  0.12288 0.0000000000  
> 0.00000 0.0001128125    0.00512
>#28  2  3  1  2 0.74 0.62 0.320 0.320 0.0006768750  0.03072 0.0000000000  
> 0.00000 0.0001128125    0.00512
>#29  2  3  1  3 0.68 1.00 0.320 0.000 0.0000118750  0.00256 0.0000000000  
> 0.00000 0.0001246875    0.00768
>#30  2  3  2  0 1.00 0.00 0.500 0.500 0.0021434375  0.02048 0.0000000000  
> 0.00000 0.0001246875    0.00768
>#31  2  3  2  1 1.00 0.63 0.185 0.185 0.0003384375  0.01536 0.0003384375  
> 0.01536 0.0004631250    0.02304
>#32  2  3  2  2 1.00 0.73 0.135 0.135 0.0000178125  0.00384 0.0003562500  
> 0.01920 0.0004809375    0.02688  
>#33  2  3  2  3 1.00 1.00 0.000 0.000 0.0000003125  0.00032 0.0003565625  
> 0.01952 0.0004812500    0.02720
>
>#Sorry, some values in my previous solution didn't look right. I didn't 
>A.K.
>
>
>
>
>
>----- Original Message -----
>From: Zjoanna <[hidden email]>
>To: [hidden email]
>Cc:
>Sent: Friday, February 1, 2013 12:19 PM
>Subject: Re: [R] cumulative sum by group and under some criteria
>
>Thank you very much for your reply. Your code work well with this example.
>I modified a little to fit my real data, I got an error massage.
>
>Error in split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...) :
>  Group length is 0 but data length > 0
>
>
>On Thu, Jan 31, 2013 at 12:21 PM, arun kirshna [via R] <
>[hidden email]> wrote:
>
>> Hi,
>> Try this:
>> colnames(d)<-c("m1","n1","x1","y1","p11","p12")
>> library(zoo)
>> res1<- do.call(rbind,lapply(lapply(split(d,list(d$m1,d$n1)),function(x)
>> {x$cp11[x$x1>1]<- cumsum(x$p11[x$x1>1]);x$cp12[x$y1>1]<-
>> cumsum(x$p12[x$y1>1]);x}),function(x)
>> {x$cp11<-na.locf(x$cp11,na.rm=F);x$cp12<- na.locf(x$cp12,na.rm=F);x}))
>> #there would be a warning here as one of the list element is NULL.  The,
>> warning is okay
>> row.names(res1)<- 1:nrow(res1)
>> res1[,7:8][is.na(res1[,7:8])]<- 0
>> res1
>>  #  m1 n1 x1 y1  p11  p12 cp11 cp12
>> #1   2  2  0  0 0.00 0.00 0.00 0.00
>> #2   2  2  0  1 0.00 0.50 0.00 0.00
>> #3   2  2  0  2 0.00 1.00 0.00 1.00
>> #4   2  2  1  0 0.50 0.00 0.00 1.00
>> #5   2  2  1  1 0.50 0.50 0.00 1.00
>> #6   2  2  1  2 0.50 1.00 0.00 2.00
>> #7   2  2  2  0 1.00 0.00 1.00 2.00
>> #8   2  2  2  1 1.00 0.50 2.00 2.00
>> #9   2  2  2  2 1.00 1.00 3.00 3.00
>> #10  3  2  0  0 0.00 0.00 0.00 0.00
>> #11  3  2  0  1 0.00 0.50 0.00 0.00
>> #12  3  2  0  2 0.00 1.00 0.00 1.00
>> #13  3  2  1  0 0.33 0.00 0.00 1.00
>> #14  3  2  1  1 0.33 0.50 0.00 1.00
>> #15  3  2  1  2 0.33 1.00 0.00 2.00
>> #16  3  2  2  0 0.67 0.00 0.67 2.00
>> #17  3  2  2  1 0.67 0.50 1.34 2.00
>> #18  3  2  2  2 0.67 1.00 2.01 3.00
>> #19  3  2  3  0 1.00 0.00 3.01 3.00
>> #20  3  2  3  1 1.00 0.50 4.01 3.00
>> #21  3  2  3  2 1.00 1.00 5.01 4.00
>> #22  2  3  0  0 0.00 0.00 0.00 0.00
>> #23  2  3  0  1 0.00 0.33 0.00 0.00
>> #24  2  3  0  2 0.00 0.67 0.00 0.67
>> #25  2  3  0  3 0.00 1.00 0.00 1.67
>> #26  2  3  1  0 0.50 0.00 0.00 1.67
>> #27  2  3  1  1 0.50 0.33 0.00 1.67
>> #28  2  3  1  2 0.50 0.67 0.00 2.34
>> #29  2  3  1  3 0.50 1.00 0.00 3.34
>> #30  2  3  2  0 1.00 0.00 1.00 3.34
>> #31  2  3  2  1 1.00 0.33 2.00 3.34
>> #32  2  3  2  2 1.00 0.67 3.00 4.01
>> #33  2  3  2  3 1.00 1.00 4.00 5.01
>> A.K.
>>
>> ------------------------------
>>  If you reply to this email, your message will be added to the discussion
>> below:
>>
>> http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657196.html
>> To unsubscribe from cumulative sum by group and under some criteria, click
>> here<
>> .
>> NAML<
http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>
>
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657315.html
>Sent from the R help mailing list archive at Nabble.com.
>    [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
>
>
>______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
>
></quote>
>Quoted from:
>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657360.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

Zjoanna

Thanks very much for your help!
 
I want to add some more columns based on the results. Is the following code good way to create such a data frame and How to see the column m and n in the updated data?
 
d2<-reres3[res3[,3]<0.01 & res3[,4]<0.01,]
colnames(d2)[1:2]<- c("m1","n1");
d2
d3<-data.frame(d2)
   for (m in (m1+2):10){
        for (n in (n1+2):10){
 d3<-rbind(d3, c(d2))}}

 

 
On Tue, Feb 5, 2013 at 9:32 AM, arun kirshna [via R] <[hidden email]> wrote:

Hi,
res3<-with(res2,aggregate(cbind(cterm1_P1L,cterm1_P0H),by=list(m1,n1),max))

 res3[res3[,3]<0.6 & res3[,4]<0.95,] #this doesn't change the result as the conditions are  not met
#  Group.1 Group.2 cterm1_P1L cterm1_P0H
#1       2       2    0.01440 0.00273750
#2       3       2    0.00032 0.00250000
#3       2       3    0.01952 0.00048125

res3[res3[,3]<0.01 & res3[,4]<0.01,]
 # Group.1 Group.2 cterm1_P1L cterm1_P0H
#2       3       2    0.00032     0.0025
Hope it helps.
A.K.
________________________________
From: Joanna Zhang <[hidden email]>
To: arun <[hidden email]>
Sent: Tuesday, February 5, 2013 9:43 AM
Subject: Re: cumulative sum by group and under some criteria


Yes, it did answer my question, max(x) look at the maximum value. However, how to put both criteria in the code, like both the maximum value of cterm1_p1L <0.6 and the maximum value of cterm1_p0L<0.95. Thanks a lot! 


On Mon, Feb 4, 2013 at 4:26 PM, arun <[hidden email]> wrote:


>
>Hi,
>
>Did this answered your question?
>
>A.K.
>
>
>----- Original Message -----
>
>From: arun <[hidden email]>
>To: "[hidden email]" <[hidden email]>
>Cc: R help <[hidden email]>
>Sent: Monday, February 4, 2013 4:44 PM
>Subject: Re: cumulative sum by group and under some criteria
>
>
>
>Hi,
>Thanks. This extract every row that satisfy the condition, but I need look at the last row (the maximum of cumulative sum) for each block (m1,n1). for example, if I set the criteria 
>
>res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95, this should extract m1= 3, n1 = 2. 
>
>
>Hi,
>I am not sure I understand your question.
>res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95
> #[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
>#[16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
>#[31] TRUE TRUE TRUE
>
>This will extract all the rows.
>
>
>res2[,1:2][res2$cterm1_P1L<0.01 & res2$cterm1_P1L!=0,]  
>#   m1 n1
>#21  3  2
>This extract only the row you wanted.  
>
>For the different groups:
>
>aggregate(cterm1_P1L~m1+n1,data=res2,max)
>#  m1 n1 cterm1_P1L
>#1  2  2    0.01440
>#2  3  2    0.00032
>#3  2  3    0.01952

> aggregate(cterm1_P1L~m1+n1,data=res2,function(x) max(x)<0.01)
> # m1 n1 cterm1_P1L
>#1  2  2      FALSE
>#2  3  2       TRUE
>#3  2  3      FALSE
>
>res4<-aggregate(cterm1_P1L~m1+n1,data=res2,function(x) max(x)<0.01)
>res4[,1:2][res4[,3],]
>#  m1 n1
>#2  3  2
>
>A.K.
>
>
>
>
>----- Original Message -----
>Cc:
>Sent: Sunday, February 3, 2013 3:58 PM
>Subject: Re: cumulative sum by group and under some criteria
>
>Hi,
>Let me restate my questions. I need to get the m1 and n1 that satisfy some criteria, for example in this case, within each group, the maximum cterm1_p1L ( the last row in this group) <0.01. I need to extract m1=3, n1=2, I only need m1, n1 in the row.
>
>Also, how to create the structure from the data.frame, I am new to R, I need to change the maxN and run the loop to different data.
>Thanks very much for your help!
>
><quote author='arun kirshna'>
>HI,

>
>I think this should be more correct:
>maxN<-9 
>c11<-0.2 
>c12<-0.2 
>p0L<-0.05 
>p0H<-0.05 
>p1L<-0.20 
>p1H<-0.20 
>
>d <- structure(list(m1 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
>2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), 
>    n1 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 
>    3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), x1 = c(0, 
>    0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 
>    2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3), y1 = c(0, 1, 2, 0, 
>    1, 2, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 
>    2, 0, 1, 2, 0, 1, 2, 0, 1, 2), Fmm = c(0, 0, 0, 0.7, 0.59, 
>    0.64, 1, 1, 1, 0, 0, 0, 0, 0.63, 0.7, 0.74, 0.68, 1, 1, 1, 
>    1, 0, 0, 0, 0.62, 0.63, 0.6, 0.63, 0.6, 0.68, 1, 1, 1), Fnn = c(0, 
>    0.64, 1, 0, 0.51, 1, 0, 0.67, 1, 0, 0.62, 0.69, 1, 0, 0.54, 
>    0.62, 1, 0, 0.63, 0.73, 1, 0, 0.63, 1, 0, 0.7, 1, 0, 0.7, 
>    1, 0, 0.58, 1), Qm = c(1, 1, 1, 0.65, 0.45, 0.36, 0.5, 0.165, 
>    0, 1, 1, 1, 1, 0.685, 0.38, 0.32, 0.32, 0.5, 0.185, 0.135, 
>    0, 1, 1, 1, 0.69, 0.37, 0.4, 0.685, 0.4, 0.32, 0.5, 0.21, 
>    0), Qn = c(1, 0.36, 0, 0.65, 0.45, 0, 0.5, 0.165, 0, 1, 0.38, 
>    0.31, 0, 0.685, 0.38, 0.32, 0, 0.5, 0.185, 0.135, 0, 1, 0.37, 
>    0, 0.69, 0.3, 0, 0.685, 0.3, 0, 0.5, 0.21, 0), term1_p0 = c(0.81450625, 
>    0.0857375, 0.00225625, 0.0857375, 0.009025, 0.0002375, 0.00225625, 
>    0.0002375, 6.25e-06, 0.7737809375, 0.1221759375, 0.00643031249999999, 
>    0.0001128125, 0.081450625, 0.012860625, 0.000676875, 1.1875e-05, 
>    0.0021434375, 0.0003384375, 1.78125e-05, 3.125e-07, 0.7737809375, 
>    0.081450625, 0.0021434375, 0.1221759375, 0.012860625, 0.0003384375, 
>    0.00643031249999999, 0.000676875, 1.78125e-05, 0.0001128125, 
>    1.1875e-05, 3.125e-07), term1_p1 = c(0.4096, 0.2048, 0.0256, 
>    0.2048, 0.1024, 0.0128, 0.0256, 0.0128, 0.0016, 0.32768, 
>    0.24576, 0.06144, 0.00512, 0.16384, 0.12288, 0.03072, 0.00256, 
>    0.02048, 0.01536, 0.00384, 0.00032, 0.32768, 0.16384, 0.02048, 
>    0.24576, 0.12288, 0.01536, 0.06144, 0.03072, 0.00384, 0.00512, 
>    0.00256, 0.00032)), .Names = c("m1", "n1", "x1", "y1", "Fmm", 
>"Fnn", "Qm", "Qn", "term1_p0", "term1_p1"), row.names = c(NA, 
>33L), class = "data.frame")
>
>library(zoo)
>lst1<- split(d,list(d$m1,d$n1))
>res2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){
>x[,11:14]<-NA;
>x[,11:12][x$Qm<=c11,]<-cumsum(x[,9:10][x$Qm<=c11,]);
>x[,13:14][x$Qn<=c12,]<-cumsum(x[,9:10][x$Qn<=c12,]);
>colnames(x)[11:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H");
>x1<-na.locf(x);
>x1[,11:14][is.na(x1[,11:14])]<-0;
>x1}))
>row.names(res2)<- 1:nrow(res2)
>
> res2
> #  m1 n1 x1 y1  Fmm  Fnn    Qm    Qn     term1_p0 term1_p1   cterm1_P0L
>cterm1_P1L   cterm1_P0H cterm1_P1H
>
>#1   2  2  0  0 0.00 0.00 1.000 1.000 0.8145062500  0.40960 0.0000000000  
> 0.00000 0.0000000000    0.00000
>#2   2  2  0  1 0.00 0.64 1.000 0.360 0.0857375000  0.20480 0.0000000000  
> 0.00000 0.0000000000    0.00000
>#3   2  2  0  2 0.00 1.00 1.000 0.000 0.0022562500  0.02560 0.0000000000  
> 0.00000 0.0022562500    0.02560
>#4   2  2  1  0 0.70 0.00 0.650 0.650 0.0857375000  0.20480 0.0000000000  
> 0.00000 0.0022562500    0.02560
>#5   2  2  1  1 0.59 0.51 0.450 0.450 0.0090250000  0.10240 0.0000000000  
> 0.00000 0.0022562500    0.02560
>#6   2  2  1  2 0.64 1.00 0.360 0.000 0.0002375000  0.01280 0.0000000000  
> 0.00000 0.0024937500    0.03840
>#7   2  2  2  0 1.00 0.00 0.500 0.500 0.0022562500  0.02560 0.0000000000  
> 0.00000 0.0024937500    0.03840
>#8   2  2  2  1 1.00 0.67 0.165 0.165 0.0002375000  0.01280 0.0002375000  
> 0.01280 0.0027312500    0.05120
>#9   2  2  2  2 1.00 1.00 0.000 0.000 0.0000062500  0.00160 0.0002437500  
> 0.01440 0.0027375000    0.05280
>#10  3  2  0  0 0.00 0.00 1.000 1.000 0.7737809375  0.32768 0.0000000000  
> 0.00000 0.0000000000    0.00000
>#11  3  2  0  1 0.00 0.63 1.000 0.370 0.0814506250  0.16384 0.0000000000  
> 0.00000 0.0000000000    0.00000
>#12  3  2  0  2 0.00 1.00 1.000 0.000 0.0021434375  0.02048 0.0000000000  
> 0.00000 0.0021434375    0.02048
>#13  3  2  1  0 0.62 0.00 0.690 0.690 0.1221759375  0.24576 0.0000000000  
> 0.00000 0.0021434375    0.02048
>#14  3  2  1  1 0.63 0.70 0.370 0.300 0.0128606250  0.12288 0.0000000000  
> 0.00000 0.0021434375    0.02048
>#15  3  2  1  2 0.60 1.00 0.400 0.000 0.0003384375  0.01536 0.0000000000  
> 0.00000 0.0024818750    0.03584
>#16  3  2  2  0 0.63 0.00 0.685 0.685 0.0064303125  0.06144 0.0000000000  
> 0.00000 0.0024818750    0.03584
>#17  3  2  2  1 0.60 0.70 0.400 0.300 0.0006768750  0.03072 0.0000000000  
> 0.00000 0.0024818750    0.03584
>#18  3  2  2  2 0.68 1.00 0.320 0.000 0.0000178125  0.00384 0.0000000000  
> 0.00000 0.0024996875    0.03968
>#19  3  2  3  0 1.00 0.00 0.500 0.500 0.0001128125  0.00512 0.0000000000  
> 0.00000 0.0024996875    0.03968
>#20  3  2  3  1 1.00 0.58 0.210 0.210 0.0000118750  0.00256 0.0000000000  
> 0.00000 0.0024996875    0.03968
>#21  3  2  3  2 1.00 1.00 0.000 0.000 0.0000003125  0.00032 0.0000003125  
> 0.00032 0.0025000000    0.04000
>#22  2  3  0  0 0.00 0.00 1.000 1.000 0.7737809375  0.32768 0.0000000000  
> 0.00000 0.0000000000    0.00000
>#23  2  3  0  1 0.00 0.62 1.000 0.380 0.1221759375  0.24576 0.0000000000  
> 0.00000 0.0000000000    0.00000
>#24  2  3  0  2 0.00 0.69 1.000 0.310 0.0064303125  0.06144 0.0000000000  
> 0.00000 0.0000000000    0.00000
>#25  2  3  0  3 0.00 1.00 1.000 0.000 0.0001128125  0.00512 0.0000000000  
> 0.00000 0.0001128125    0.00512
>#26  2  3  1  0 0.63 0.00 0.685 0.685 0.0814506250  0.16384 0.0000000000  
> 0.00000 0.0001128125    0.00512
>#27  2  3  1  1 0.70 0.54 0.380 0.380 0.0128606250  0.12288 0.0000000000  
> 0.00000 0.0001128125    0.00512
>#28  2  3  1  2 0.74 0.62 0.320 0.320 0.0006768750  0.03072 0.0000000000  
> 0.00000 0.0001128125    0.00512
>#29  2  3  1  3 0.68 1.00 0.320 0.000 0.0000118750  0.00256 0.0000000000  
> 0.00000 0.0001246875    0.00768
>#30  2  3  2  0 1.00 0.00 0.500 0.500 0.0021434375  0.02048 0.0000000000  
> 0.00000 0.0001246875    0.00768
>#31  2  3  2  1 1.00 0.63 0.185 0.185 0.0003384375  0.01536 0.0003384375  
> 0.01536 0.0004631250    0.02304
>#32  2  3  2  2 1.00 0.73 0.135 0.135 0.0000178125  0.00384 0.0003562500  
> 0.01920 0.0004809375    0.02688  
>#33  2  3  2  3 1.00 1.00 0.000 0.000 0.0000003125  0.00032 0.0003565625  
> 0.01952 0.0004812500    0.02720
>
>#Sorry, some values in my previous solution didn't look right. I didn't 
>A.K.
>
>
>
>
>
>----- Original Message -----
>From: Zjoanna <[hidden email]>
>To: [hidden email]
>Cc:
>Sent: Friday, February 1, 2013 12:19 PM
>Subject: Re: [R] cumulative sum by group and under some criteria
>
>Thank you very much for your reply. Your code work well with this example.
>I modified a little to fit my real data, I got an error massage.
>
>Error in split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...) :
>  Group length is 0 but data length > 0
>
>
>On Thu, Jan 31, 2013 at 12:21 PM, arun kirshna [via R] <
>[hidden email]> wrote:
>
>> Hi,
>> Try this:
>> colnames(d)<-c("m1","n1","x1","y1","p11","p12")
>> library(zoo)
>> res1<- do.call(rbind,lapply(lapply(split(d,list(d$m1,d$n1)),function(x)
>> {x$cp11[x$x1>1]<- cumsum(x$p11[x$x1>1]);x$cp12[x$y1>1]<-
>> cumsum(x$p12[x$y1>1]);x}),function(x)
>> {x$cp11<-na.locf(x$cp11,na.rm=F);x$cp12<- na.locf(x$cp12,na.rm=F);x}))
>> #there would be a warning here as one of the list element is NULL.  The,
>> warning is okay
>> row.names(res1)<- 1:nrow(res1)
>> res1[,7:8][is.na(res1[,7:8])]<- 0
>> res1
>>  #  m1 n1 x1 y1  p11  p12 cp11 cp12
>> #1   2  2  0  0 0.00 0.00 0.00 0.00
>> #2   2  2  0  1 0.00 0.50 0.00 0.00
>> #3   2  2  0  2 0.00 1.00 0.00 1.00
>> #4   2  2  1  0 0.50 0.00 0.00 1.00
>> #5   2  2  1  1 0.50 0.50 0.00 1.00
>> #6   2  2  1  2 0.50 1.00 0.00 2.00
>> #7   2  2  2  0 1.00 0.00 1.00 2.00
>> #8   2  2  2  1 1.00 0.50 2.00 2.00
>> #9   2  2  2  2 1.00 1.00 3.00 3.00
>> #10  3  2  0  0 0.00 0.00 0.00 0.00
>> #11  3  2  0  1 0.00 0.50 0.00 0.00
>> #12  3  2  0  2 0.00 1.00 0.00 1.00
>> #13  3  2  1  0 0.33 0.00 0.00 1.00
>> #14  3  2  1  1 0.33 0.50 0.00 1.00
>> #15  3  2  1  2 0.33 1.00 0.00 2.00
>> #16  3  2  2  0 0.67 0.00 0.67 2.00
>> #17  3  2  2  1 0.67 0.50 1.34 2.00
>> #18  3  2  2  2 0.67 1.00 2.01 3.00
>> #19  3  2  3  0 1.00 0.00 3.01 3.00
>> #20  3  2  3  1 1.00 0.50 4.01 3.00
>> #21  3  2  3  2 1.00 1.00 5.01 4.00
>> #22  2  3  0  0 0.00 0.00 0.00 0.00
>> #23  2  3  0  1 0.00 0.33 0.00 0.00
>> #24  2  3  0  2 0.00 0.67 0.00 0.67
>> #25  2  3  0  3 0.00 1.00 0.00 1.67
>> #26  2  3  1  0 0.50 0.00 0.00 1.67
>> #27  2  3  1  1 0.50 0.33 0.00 1.67
>> #28  2  3  1  2 0.50 0.67 0.00 2.34
>> #29  2  3  1  3 0.50 1.00 0.00 3.34
>> #30  2  3  2  0 1.00 0.00 1.00 3.34
>> #31  2  3  2  1 1.00 0.33 2.00 3.34
>> #32  2  3  2  2 1.00 0.67 3.00 4.01
>> #33  2  3  2  3 1.00 1.00 4.00 5.01
>> A.K.
>>
>> ------------------------------
>>  If you reply to this email, your message will be added to the discussion
>> below:
>>
>> http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657196.html
>> To unsubscribe from cumulative sum by group and under some criteria, click
>> here<

>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
>
>
>______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
>
></quote>
>Quoted from:
>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657360.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



If you reply to this email, your message will be added to the discussion below:
http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657614.html
To unsubscribe from cumulative sum by group and under some criteria, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

arun kirshna
In reply to this post by Zjoanna
Hi,

You can reduce the steps to reach d2:
res3<- with(res2,aggregate(cbind(cterm1_P1L,cterm1_P0H),by=list(m1,n1),max))

#Change it to:
res3new<-  aggregate(.~m1+n1,data=res2[,c(1:2,12:13)],max)
res3new
 m1 n1 cterm1_P1L cterm1_P0H
1  2  2    0.01440 0.00273750
2  3  2    0.00032 0.00250000
3  2  3    0.01952 0.00048125
d2<-res3new[res3new[,3]<0.01 & res3new[,4]<0.01,]

 dnew<-expand.grid(4:10,5:10)
 names(dnew)<-c("n","m")
resF<-cbind(dnew[,c(2,1)],d2[rep(1:nrow(d2),nrow(dnew)),])

row.names(resF)<-1:nrow(resF)
 head(resF)
#  m n m1 n1 cterm1_P1L cterm1_P0H
#1 5 4  3  2    0.00032     0.0025
#2 5 5  3  2    0.00032     0.0025
#3 5 6  3  2    0.00032     0.0025
#4 5 7  3  2    0.00032     0.0025
#5 5 8  3  2    0.00032     0.0025
#6 5 9  3  2    0.00032     0.0025
A.K.

________________________________
From: Joanna Zhang <[hidden email]>
To: arun <[hidden email]>
Sent: Tuesday, February 5, 2013 2:48 PM
Subject: Re: cumulative sum by group and under some criteria


  Hi ,
what I want is :
m   n    m1    n1 cterm1_P1L   cterm1_P0H
 5   4    3       2    0.00032         0.00250000
 5   5    3       2    0.00032         0.00250000
 5   6    3       2    0.00032         0.00250000
 5   7    3       2    0.00032         0.00250000
 5   8   3       2    0.00032         0.00250000
 5   9   3       2    0.00032         0.00250000
5   10  3       2    0.00032         0.00250000
6    4   3       2    0.00032         0.00250000
6    5   3       2    0.00032         0.00250000
6    6   3       2    0.00032         0.00250000
6    7   3       2    0.00032         0.00250000
.....
6    10  3       2    0.00032         0.00250000



On Tue, Feb 5, 2013 at 1:12 PM, arun <[hidden email]> wrote:

Hi,

>
>Saw your message on Nabble.
>
>
>"I want to add some more columns based on the results. Is the following code good way to create such a data frame and How to see the column m and n in the updated data?

>d2<- reres3[res3[,3]<0.01 & res3[,4]<0.01,]  
># should be a typo
>
>colnames(d2)[1:2]<- c("m1","n1"); 
>d2 #already a data.frame
>
>d3<-data.frame(d2)
>   for (m in (m1+2):10){
>        for (n in (n1+2):10){
> d3<-rbind(d3, c(d2))}}" #this is not making much sense to me.  Especially, you mentioned you wanted add more columns.
>#Running this step gave error
>#Error: object 'm1' not found
>
>Not sure what you want as output.
>Could you show the ouput that is expected:
>
>A.K.
>
>
>
>
>
>
>
>
>________________________________
>From: Joanna Zhang <[hidden email]>
>To: arun <[hidden email]>
>Sent: Tuesday, February 5, 2013 10:23 AM
>
>Subject: Re: cumulative sum by group and under some criteria
>
>
>Hi,
>
>Yes, I changed code. You answered the questions. But how can I put two criteria in the code, if both the maximum value of cterm1_p1L <= 0.01 and cterm1_p1H <=0.01, the output the m1,n1.
>
>
>
>
>On Tue, Feb 5, 2013 at 8:47 AM, arun <[hidden email]> wrote:
>
>
>>
>> HI,
>>
>>
>>I am not getting the same results as yours:  You must have changed the dataset.
>> res2[,1:2][res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95,] 
>>   m1 n1
>>1   2  2
>>2   2  2
>>3   2  2
>>4   2  2
>>5   2  2
>>6   2  2
>>7   2  2
>>8   2  2
>>9   2  2
>>10  3  2
>>11  3  2
>>12  3  2
>>13  3  2
>>14  3  2
>>15  3  2
>>16  3  2
>>17  3  2
>>18  3  2
>>19  3  2
>>20  3  2
>>21  3  2
>>22  2  3
>>23  2  3
>>24  2  3
>>25  2  3
>>26  2  3
>>27  2  3
>>28  2  3
>>29  2  3
>>30  2  3
>>31  2  3
>>32  2  3
>>33  2  3
>>
>>
>>Regarding the maximum value within each block, haven't I answered in the earlier post.
>>
>>aggregate(cterm1_P1L~m1+n1,data=res2,max)
>>#  m1 n1 cterm1_P1L
>>#1  2  2    0.01440
>>#2  3  2    0.00032
>>#3  2  3    0.01952
>> 
>>
>> with(res2,aggregate(cbind(cterm1_P1L,cterm1_P0H),by=list(m1,n1),max))
>>#  Group.1 Group.2 cterm1_P1L cterm1_P0H
>>#1       2       2    0.01440 0.00273750
>>#2       3       2    0.00032 0.00250000
>>#3       2       3    0.01952 0.00048125
>>
>>
>>A.K.
>>
>>
>>----- Original Message -----
>>From: "[hidden email]" <[hidden email]>
>>To: [hidden email]
>>Cc:
>>
>>Sent: Tuesday, February 5, 2013 9:33 AM
>>Subject: Re: cumulative sum by group and under some criteria
>>
>>Hi,
>>If use this
>>
>>res2[,1:2][res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95,]
>>
>>the results are the following, but actually only m1=3, n1=2 sastify the criteria, as I need to look at the row with maximum value within each block,not every row.
>>
>>
>>   m1 n1
>>1   2  2
>>10  3  2
>>11  3  2
>>12  3  2
>>13  3  2
>>14  3  2
>>15  3  2
>>16  3  2
>>17  3  2
>>18  3  2
>>19  3  2
>>20  3  2
>>21  3  2
>>22  2  3
>>23  2  3
>>
>>
>><quote author='arun kirshna'>
>>
>>
>>
>>Hi,
>>Thanks. This extract every row that satisfy the condition, but I need look
>>at the last row (the maximum of cumulative sum) for each block (m1,n1). for
>>example, if I set the criteria 
>>
>>res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95, this should extract m1= 3, n1 =
>>2. 
>>
>>
>>Hi,
>>I am not sure I understand your question.
>>res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95
>> #[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
>>TRUE
>>#[16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
>>TRUE
>>#[31] TRUE TRUE TRUE
>>
>>This will extract all the rows.
>>
>>
>>res2[,1:2][res2$cterm1_P1L<0.01 & res2$cterm1_P1L!=0,]  
>>#   m1 n1
>>#21  3  2
>>This extract only the row you wanted.  
>>
>>For the different groups:
>>
>>aggregate(cterm1_P1L~m1+n1,data=res2,max)
>>#  m1 n1 cterm1_P1L
>>#1  2  2    0.01440
>>#2  3  2    0.00032
>>#3  2  3    0.01952
>> 
>> aggregate(cterm1_P1L~m1+n1,data=res2,function(x) max(x)<0.01)
>> # m1 n1 cterm1_P1L
>>#1  2  2      FALSE
>>#2  3  2       TRUE
>>#3  2  3      FALSE
>>
>>res4<-aggregate(cterm1_P1L~m1+n1,data=res2,function(x) max(x)<0.01)
>>res4[,1:2][res4[,3],]
>>#  m1 n1
>>#2  3  2
>>
>>A.K.
>>
>>
>>
>>
>>----- Original Message -----
>>From: "[hidden email]" <[hidden email]>
>>To: [hidden email]
>>Cc:
>>Sent: Sunday, February 3, 2013 3:58 PM
>>Subject: Re: cumulative sum by group and under some criteria
>>
>>Hi,
>>Let me restate my questions. I need to get the m1 and n1 that satisfy some
>>criteria, for example in this case, within each group, the maximum
>>cterm1_p1L ( the last row in this group) <0.01. I need to extract m1=3,
>>n1=2, I only need m1, n1 in the row.
>>
>>Also, how to create the structure from the data.frame, I am new to R, I need
>>to change the maxN and run the loop to different data.
>>Thanks very much for your help!
>>
>><quote author='arun kirshna'>
>>HI,
>>
>>I think this should be more correct:
>>maxN<-9 
>>c11<-0.2 
>>c12<-0.2 
>>p0L<-0.05 
>>p0H<-0.05 
>>p1L<-0.20 
>>p1H<-0.20 
>>
>>d <- structure(list(m1 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
>>2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), 
>>    n1 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 
>>    3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), x1 = c(0, 
>>    0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 
>>    2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3), y1 = c(0, 1, 2, 0, 
>>    1, 2, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 
>>    2, 0, 1, 2, 0, 1, 2, 0, 1, 2), Fmm = c(0, 0, 0, 0.7, 0.59, 
>>    0.64, 1, 1, 1, 0, 0, 0, 0, 0.63, 0.7, 0.74, 0.68, 1, 1, 1, 
>>    1, 0, 0, 0, 0.62, 0.63, 0.6, 0.63, 0.6, 0.68, 1, 1, 1), Fnn = c(0, 
>>    0.64, 1, 0, 0.51, 1, 0, 0.67, 1, 0, 0.62, 0.69, 1, 0, 0.54, 
>>    0.62, 1, 0, 0.63, 0.73, 1, 0, 0.63, 1, 0, 0.7, 1, 0, 0.7, 
>>    1, 0, 0.58, 1), Qm = c(1, 1, 1, 0.65, 0.45, 0.36, 0.5, 0.165, 
>>    0, 1, 1, 1, 1, 0.685, 0.38, 0.32, 0.32, 0.5, 0.185, 0.135, 
>>    0, 1, 1, 1, 0.69, 0.37, 0.4, 0.685, 0.4, 0.32, 0.5, 0.21, 
>>    0), Qn = c(1, 0.36, 0, 0.65, 0.45, 0, 0.5, 0.165, 0, 1, 0.38, 
>>    0.31, 0, 0.685, 0.38, 0.32, 0, 0.5, 0.185, 0.135, 0, 1, 0.37, 
>>    0, 0.69, 0.3, 0, 0.685, 0.3, 0, 0.5, 0.21, 0), term1_p0 = c(0.81450625, 
>>    0.0857375, 0.00225625, 0.0857375, 0.009025, 0.0002375, 0.00225625, 
>>    0.0002375, 6.25e-06, 0.7737809375, 0.1221759375, 0.00643031249999999, 
>>    0.0001128125, 0.081450625, 0.012860625, 0.000676875, 1.1875e-05, 
>>    0.0021434375, 0.0003384375, 1.78125e-05, 3.125e-07, 0.7737809375, 
>>    0.081450625, 0.0021434375, 0.1221759375, 0.012860625, 0.0003384375, 
>>    0.00643031249999999, 0.000676875, 1.78125e-05, 0.0001128125, 
>>    1.1875e-05, 3.125e-07), term1_p1 = c(0.4096, 0.2048, 0.0256, 
>>    0.2048, 0.1024, 0.0128, 0.0256, 0.0128, 0.0016, 0.32768, 
>>    0.24576, 0.06144, 0.00512, 0.16384, 0.12288, 0.03072, 0.00256, 
>>    0.02048, 0.01536, 0.00384, 0.00032, 0.32768, 0.16384, 0.02048, 
>>    0.24576, 0.12288, 0.01536, 0.06144, 0.03072, 0.00384, 0.00512, 
>>    0.00256, 0.00032)), .Names = c("m1", "n1", "x1", "y1", "Fmm", 
>>"Fnn", "Qm", "Qn", "term1_p0", "term1_p1"), row.names = c(NA, 
>>33L), class = "data.frame")
>>
>>library(zoo)
>>lst1<- split(d,list(d$m1,d$n1))
>>res2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){
>>x[,11:14]<-NA;
>>x[,11:12][x$Qm<=c11,]<-cumsum(x[,9:10][x$Qm<=c11,]);
>>x[,13:14][x$Qn<=c12,]<-cumsum(x[,9:10][x$Qn<=c12,]);
>>colnames(x)[11:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H");
>>x1<-na.locf(x);
>>x1[,11:14][is.na(x1[,11:14])]<-0;
>>x1}))
>>row.names(res2)<- 1:nrow(res2)
>>
>> res2
>> #  m1 n1 x1 y1  Fmm  Fnn    Qm    Qn     term1_p0 term1_p1   cterm1_P0L
>>cterm1_P1L   cterm1_P0H cterm1_P1H
>>
>>#1   2  2  0  0 0.00 0.00 1.000 1.000 0.8145062500  0.40960 0.0000000000  
>> 0.00000 0.0000000000    0.00000
>>#2   2  2  0  1 0.00 0.64 1.000 0.360 0.0857375000  0.20480 0.0000000000  
>> 0.00000 0.0000000000    0.00000
>>#3   2  2  0  2 0.00 1.00 1.000 0.000 0.0022562500  0.02560 0.0000000000  
>> 0.00000 0.0022562500    0.02560
>>#4   2  2  1  0 0.70 0.00 0.650 0.650 0.0857375000  0.20480 0.0000000000  
>> 0.00000 0.0022562500    0.02560
>>#5   2  2  1  1 0.59 0.51 0.450 0.450 0.0090250000  0.10240 0.0000000000  
>> 0.00000 0.0022562500    0.02560
>>#6   2  2  1  2 0.64 1.00 0.360 0.000 0.0002375000  0.01280 0.0000000000  
>> 0.00000 0.0024937500    0.03840
>>#7   2  2  2  0 1.00 0.00 0.500 0.500 0.0022562500  0.02560 0.0000000000  
>> 0.00000 0.0024937500    0.03840
>>#8   2  2  2  1 1.00 0.67 0.165 0.165 0.0002375000  0.01280 0.0002375000  
>> 0.01280 0.0027312500    0.05120
>>#9   2  2  2  2 1.00 1.00 0.000 0.000 0.0000062500  0.00160 0.0002437500  
>> 0.01440 0.0027375000    0.05280
>>#10  3  2  0  0 0.00 0.00 1.000 1.000 0.7737809375  0.32768 0.0000000000  
>> 0.00000 0.0000000000    0.00000
>>#11  3  2  0  1 0.00 0.63 1.000 0.370 0.0814506250  0.16384 0.0000000000  
>> 0.00000 0.0000000000    0.00000
>>#12  3  2  0  2 0.00 1.00 1.000 0.000 0.0021434375  0.02048 0.0000000000  
>> 0.00000 0.0021434375    0.02048
>>#13  3  2  1  0 0.62 0.00 0.690 0.690 0.1221759375  0.24576 0.0000000000  
>> 0.00000 0.0021434375    0.02048
>>#14  3  2  1  1 0.63 0.70 0.370 0.300 0.0128606250  0.12288 0.0000000000  
>> 0.00000 0.0021434375    0.02048
>>#15  3  2  1  2 0.60 1.00 0.400 0.000 0.0003384375  0.01536 0.0000000000  
>> 0.00000 0.0024818750    0.03584
>>#16  3  2  2  0 0.63 0.00 0.685 0.685 0.0064303125  0.06144 0.0000000000  
>> 0.00000 0.0024818750    0.03584
>>#17  3  2  2  1 0.60 0.70 0.400 0.300 0.0006768750  0.03072 0.0000000000  
>> 0.00000 0.0024818750    0.03584
>>#18  3  2  2  2 0.68 1.00 0.320 0.000 0.0000178125  0.00384 0.0000000000  
>> 0.00000 0.0024996875    0.03968
>>#19  3  2  3  0 1.00 0.00 0.500 0.500 0.0001128125  0.00512 0.0000000000  
>> 0.00000 0.0024996875    0.03968
>>#20  3  2  3  1 1.00 0.58 0.210 0.210 0.0000118750  0.00256 0.0000000000  
>> 0.00000 0.0024996875    0.03968
>>#21  3  2  3  2 1.00 1.00 0.000 0.000 0.0000003125  0.00032 0.0000003125  
>> 0.00032 0.0025000000    0.04000
>>#22  2  3  0  0 0.00 0.00 1.000 1.000 0.7737809375  0.32768 0.0000000000  
>> 0.00000 0.0000000000    0.00000
>>#23  2  3  0  1 0.00 0.62 1.000 0.380 0.1221759375  0.24576 0.0000000000  
>> 0.00000 0.0000000000    0.00000
>>#24  2  3  0  2 0.00 0.69 1.000 0.310 0.0064303125  0.06144 0.0000000000  
>> 0.00000 0.0000000000    0.00000
>>#25  2  3  0  3 0.00 1.00 1.000 0.000 0.0001128125  0.00512 0.0000000000  
>> 0.00000 0.0001128125    0.00512
>>#26  2  3  1  0 0.63 0.00 0.685 0.685 0.0814506250  0.16384 0.0000000000  
>> 0.00000 0.0001128125    0.00512
>>#27  2  3  1  1 0.70 0.54 0.380 0.380 0.0128606250  0.12288 0.0000000000  
>> 0.00000 0.0001128125    0.00512
>>#28  2  3  1  2 0.74 0.62 0.320 0.320 0.0006768750  0.03072 0.0000000000  
>> 0.00000 0.0001128125    0.00512
>>#29  2  3  1  3 0.68 1.00 0.320 0.000 0.0000118750  0.00256 0.0000000000  
>> 0.00000 0.0001246875    0.00768
>>#30  2  3  2  0 1.00 0.00 0.500 0.500 0.0021434375  0.02048 0.0000000000  
>> 0.00000 0.0001246875    0.00768
>>#31  2  3  2  1 1.00 0.63 0.185 0.185 0.0003384375  0.01536 0.0003384375  
>> 0.01536 0.0004631250    0.02304
>>#32  2  3  2  2 1.00 0.73 0.135 0.135 0.0000178125  0.00384 0.0003562500  
>> 0.01920 0.0004809375    0.02688  
>>#33  2  3  2  3 1.00 1.00 0.000 0.000 0.0000003125  0.00032 0.0003565625  
>> 0.01952 0.0004812500    0.02720
>>
>>#Sorry, some values in my previous solution didn't look right. I didn't 
>>A.K.
>>
>>
>>
>>
>>
>>----- Original Message -----
>>From: Zjoanna <[hidden email]>
>>To: [hidden email]
>>Cc:
>>Sent: Friday, February 1, 2013 12:19 PM
>>Subject: Re: [R] cumulative sum by group and under some criteria
>>
>>Thank you very much for your reply. Your code work well with this example.
>>I modified a little to fit my real data, I got an error massage.
>>
>>Error in split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...) :
>>  Group length is 0 but data length > 0
>>
>>
>>On Thu, Jan 31, 2013 at 12:21 PM, arun kirshna [via R] <
>>[hidden email]> wrote:
>>
>>> Hi,
>>> Try this:
>>> colnames(d)<-c("m1","n1","x1","y1","p11","p12")
>>> library(zoo)
>>> res1<- do.call(rbind,lapply(lapply(split(d,list(d$m1,d$n1)),function(x)
>>> {x$cp11[x$x1>1]<- cumsum(x$p11[x$x1>1]);x$cp12[x$y1>1]<-
>>> cumsum(x$p12[x$y1>1]);x}),function(x)
>>> {x$cp11<-na.locf(x$cp11,na.rm=F);x$cp12<- na.locf(x$cp12,na.rm=F);x}))
>>> #there would be a warning here as one of the list element is NULL.  The,
>>> warning is okay
>>> row.names(res1)<- 1:nrow(res1)
>>> res1[,7:8][is.na(res1[,7:8])]<- 0
>>> res1
>>>  #  m1 n1 x1 y1  p11  p12 cp11 cp12
>>> #1   2  2  0  0 0.00 0.00 0.00 0.00
>>> #2   2  2  0  1 0.00 0.50 0.00 0.00
>>> #3   2  2  0  2 0.00 1.00 0.00 1.00
>>> #4   2  2  1  0 0.50 0.00 0.00 1.00
>>> #5   2  2  1  1 0.50 0.50 0.00 1.00
>>> #6   2  2  1  2 0.50 1.00 0.00 2.00
>>> #7   2  2  2  0 1.00 0.00 1.00 2.00
>>> #8   2  2  2  1 1.00 0.50 2.00 2.00
>>> #9   2  2  2  2 1.00 1.00 3.00 3.00
>>> #10  3  2  0  0 0.00 0.00 0.00 0.00
>>> #11  3  2  0  1 0.00 0.50 0.00 0.00
>>> #12  3  2  0  2 0.00 1.00 0.00 1.00
>>> #13  3  2  1  0 0.33 0.00 0.00 1.00
>>> #14  3  2  1  1 0.33 0.50 0.00 1.00
>>> #15  3  2  1  2 0.33 1.00 0.00 2.00
>>> #16  3  2  2  0 0.67 0.00 0.67 2.00
>>> #17  3  2  2  1 0.67 0.50 1.34 2.00
>>> #18  3  2  2  2 0.67 1.00 2.01 3.00
>>> #19  3  2  3  0 1.00 0.00 3.01 3.00
>>> #20  3  2  3  1 1.00 0.50 4.01 3.00
>>> #21  3  2  3  2 1.00 1.00 5.01 4.00
>>> #22  2  3  0  0 0.00 0.00 0.00 0.00
>>> #23  2  3  0  1 0.00 0.33 0.00 0.00
>>> #24  2  3  0  2 0.00 0.67 0.00 0.67
>>> #25  2  3  0  3 0.00 1.00 0.00 1.67
>>> #26  2  3  1  0 0.50 0.00 0.00 1.67
>>> #27  2  3  1  1 0.50 0.33 0.00 1.67
>>> #28  2  3  1  2 0.50 0.67 0.00 2.34
>>> #29  2  3  1  3 0.50 1.00 0.00 3.34
>>> #30  2  3  2  0 1.00 0.00 1.00 3.34
>>> #31  2  3  2  1 1.00 0.33 2.00 3.34
>>> #32  2  3  2  2 1.00 0.67 3.00 4.01
>>> #33  2  3  2  3 1.00 1.00 4.00 5.01
>>> A.K.
>>>
>>> ------------------------------
>>>  If you reply to this email, your message will be added to the discussion
>>> below:
>>>
>>> http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657196.html
>>> To unsubscribe from cumulative sum by group and under some criteria, click
>>> here<
>>> .
>>> NAML<
http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>>
>>
>>
>>
>>
>>--
>>View this message in context:
>>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657315.html
>>Sent from the R help mailing list archive at Nabble.com.
>>    [[alternative HTML version deleted]]
>>
>>______________________________________________
>>[hidden email] mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>and provide commented, minimal, self-contained, reproducible code.
>>
>>
>>______________________________________________
>>[hidden email] mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>and provide commented, minimal, self-contained, reproducible code.
>>
>></quote>
>>Quoted from:
>>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657360.html
>>
>>
>>______________________________________________
>>[hidden email] mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>and provide commented, minimal, self-contained, reproducible code.
>>
>></quote>
>>Quoted from:
>>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657582.html
>>
>>    
>  

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

arun kirshna
Hi,

maxN<- 9


 res3new<-  aggregate(.~m1+n1,data=res2[,c(1:2,12:13)],max)
 d2<-res3new[res3new[,3]<0.01 & res3new[,4]<0.01,]

 d2
#  m1 n1 cterm1_P1L cterm1_P0H
#2  3  2    0.00032     0.0025

 (d2$m1+2)
#[1] 5
 maxN-(d2$n1+2)

#[1] 5
dnew<-expand.grid((m1+2):(maxN-(n1+2)),(n1+2):(maxN-m),0:m1,0:n1, x1:(x1+m-m1),y1:(y1+n-n1)) #  I couldn't find  "m" or "n" 

        A.K.                          

________________________________
From: Joanna Zhang <[hidden email]>
To: arun <[hidden email]>
Sent: Wednesday, February 6, 2013 12:48 PM
Subject: Re: cumulative sum by group and under some criteria


I tried to change to 4, but it doesn't show all the columns.


On Wed, Feb 6, 2013 at 10:11 AM, arun <[hidden email]> wrote:


>Hi,
>
>By just looking at the faulty code:
>
>"
>resF<-cbind(dnew[,c(2,1)],d2[rep(1:nrow(d2),nrow(dnew)),])    # this is not correct, how to modify it."
>If I am correct, `dnew` should have 4 columns in this case, but dnew[,c(2,1)] is only taking the first and second columns.  Though, I didn't test it (a bit busy now, will test later).
>
>
>A.K.
>
>
>
>________________________________
>From: Joanna Zhang <[hidden email]>
>To: arun <[hidden email]>
>Sent: Wednesday, February 6, 2013 10:29 AM
>
>Subject: Re: cumulative sum by group and under some criteria
>
>
>Hi,
>
>Thanks! I need to do some calculations in the expended data, the expended data would be very large, what is an efficient way, doing calculations while expending thedata, something similiar with the following, or expending data using the code in your message and then add calculations in the expended data?
>
>
>d3<-data.frame(d2)
>   for .......{
>         for {
>              for .... {
>                  for .....{
>                       p1<- x/m
>                       p2<- y/n
>                      ..........
>}}
>}}
>
>I also modified your code for expending data:
>dnew<-expand.grid((m1+2):(maxN-(n1+2)),(n1+2):(maxN-m),0:m1,0:n1, x1:(x1+m-m1),y1:(y1+n-n1))
>names(dnew)<-c("m","n","x1","y1","x","y")
>dnew
>resF<-cbind(dnew[,c(2,1)],d2[rep(1:nrow(d2),nrow(dnew)),])    # this is not correct, how to modify it.
>resF
>row.names(resF)<-1:nrow(resF)
>resF
>
>
>
>
>On Tue, Feb 5, 2013 at 2:46 PM, arun <[hidden email]> wrote:
>
>Hi,
>>
>>You can reduce the steps to reach d2:
>>res3<- with(res2,aggregate(cbind(cterm1_P1L,cterm1_P0H),by=list(m1,n1),max))
>>
>>#Change it to:
>>res3new<-  aggregate(.~m1+n1,data=res2[,c(1:2,12:13)],max)
>>res3new
>> m1 n1 cterm1_P1L cterm1_P0H
>>1  2  2    0.01440 0.00273750
>>2  3  2    0.00032 0.00250000
>>3  2  3    0.01952 0.00048125
>>d2<-res3new[res3new[,3]<0.01 & res3new[,4]<0.01,]
>>
>> dnew<-expand.grid(4:10,5:10)
>> names(dnew)<-c("n","m")
>>resF<-cbind(dnew[,c(2,1)],d2[rep(1:nrow(d2),nrow(dnew)),])
>>
>>row.names(resF)<-1:nrow(resF)
>> head(resF)
>>#  m n m1 n1 cterm1_P1L cterm1_P0H
>>#1 5 4  3  2    0.00032     0.0025
>>#2 5 5  3  2    0.00032     0.0025
>>#3 5 6  3  2    0.00032     0.0025
>>#4 5 7  3  2    0.00032     0.0025
>>#5 5 8  3  2    0.00032     0.0025
>>#6 5 9  3  2    0.00032     0.0025
>>
>>A.K.
>>
>>________________________________
>>From: Joanna Zhang <[hidden email]>
>>To: arun <[hidden email]>
>>Sent: Tuesday, February 5, 2013 2:48 PM
>>
>>Subject: Re: cumulative sum by group and under some criteria
>>
>>
>>  Hi ,
>>what I want is :
>>m   n    m1    n1 cterm1_P1L   cterm1_P0H
>> 5   4    3       2    0.00032         0.00250000
>> 5   5    3       2    0.00032         0.00250000
>> 5   6    3       2    0.00032         0.00250000
>> 5   7    3       2    0.00032         0.00250000
>> 5   8   3       2    0.00032         0.00250000
>> 5   9   3       2    0.00032         0.00250000
>>5   10  3       2    0.00032         0.00250000
>>6    4   3       2    0.00032         0.00250000
>>6    5   3       2    0.00032         0.00250000
>>6    6   3       2    0.00032         0.00250000
>>6    7   3       2    0.00032         0.00250000
>>.....
>>6    10  3       2    0.00032         0.00250000
>>
>>
>>
>>On Tue, Feb 5, 2013 at 1:12 PM, arun <[hidden email]> wrote:
>>
>>Hi,
>>>
>>>Saw your message on Nabble.
>>>
>>>
>>>"I want to add some more columns based on the results. Is the following code good way to create such a data frame and How to see the column m and n in theupdated data?
>
>>> 
>>>d2<- reres3[res3[,3]<0.01 & res3[,4]<0.01,]  
>>># should be a typo
>>>
>>>colnames(d2)[1:2]<- c("m1","n1"); 
>>>d2 #already a data.frame
>>>
>>>d3<-data.frame(d2)
>>>   for (m in (m1+2):10){
>>>        for (n in (n1+2):10){
>>> d3<-rbind(d3, c(d2))}}" #this is not making much sense to me.  Especially, you mentioned you wanted add more columns.
>>>#Running this step gave error
>>>#Error: object 'm1' not found
>>>
>>>Not sure what you want as output.
>>>Could you show the ouput that is expected:
>>>
>>>A.K.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>________________________________
>>>From: Joanna Zhang <[hidden email]>
>>>To: arun <[hidden email]>
>>>Sent: Tuesday, February 5, 2013 10:23 AM
>>>
>>>Subject: Re: cumulative sum by group and under some criteria
>>>
>>>
>>>Hi,
>>>
>>>Yes, I changed code. You answered the questions. But how can I put two criteria in the code, if both the maximum value of cterm1_p1L <= 0.01 and cterm1_p1H<=0.01, the output the m1,n1.
>>>
>>>
>>>
>>>
>>>On Tue, Feb 5, 2013 at 8:47 AM, arun <[hidden email]> wrote:
>>>
>>>
>>>>
>>>> HI,
>>>>
>>>>
>>>>I am not getting the same results as yours:  You must have changed the dataset.
>>>> res2[,1:2][res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95,] 
>>>>   m1 n1
>>>>1   2  2
>>>>2   2  2
>>>>3   2  2
>>>>4   2  2
>>>>5   2  2
>>>>6   2  2
>>>>7   2  2
>>>>8   2  2
>>>>9   2  2
>>>>10  3  2
>>>>11  3  2
>>>>12  3  2
>>>>13  3  2
>>>>14  3  2
>>>>15  3  2
>>>>16  3  2
>>>>17  3  2
>>>>18  3  2
>>>>19  3  2
>>>>20  3  2
>>>>21  3  2
>>>>22  2  3
>>>>23  2  3
>>>>24  2  3
>>>>25  2  3
>>>>26  2  3
>>>>27  2  3
>>>>28  2  3
>>>>29  2  3
>>>>30  2  3
>>>>31  2  3
>>>>32  2  3
>>>>33  2  3
>>>>
>>>>
>>>>Regarding the maximum value within each block, haven't I answered in the earlier post.
>>>>
>>>>aggregate(cterm1_P1L~m1+n1,data=res2,max)
>>>>#  m1 n1 cterm1_P1L
>>>>#1  2  2    0.01440
>>>>#2  3  2    0.00032
>>>>#3  2  3    0.01952
>>>> 
>>>>
>>>> with(res2,aggregate(cbind(cterm1_P1L,cterm1_P0H),by=list(m1,n1),max))
>>>>#  Group.1 Group.2 cterm1_P1L cterm1_P0H
>>>>#1       2       2    0.01440 0.00273750
>>>>#2       3       2    0.00032 0.00250000
>>>>#3       2       3    0.01952 0.00048125
>>>>
>>>>
>>>>A.K.
>>>>
>>>>
>>>>----- Original Message -----
>>>>From: "[hidden email]" <[hidden email]>
>>>>To: [hidden email]
>>>>Cc:
>>>>
>>>>Sent: Tuesday, February 5, 2013 9:33 AM
>>>>Subject: Re: cumulative sum by group and under some criteria
>>>>
>>>>Hi,
>>>>If use this
>>>>
>>>>res2[,1:2][res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95,]
>>>>
>>>>the results are the following, but actually only m1=3, n1=2 sastify the criteria, as I need to look at the row with maximum value within each block,not every row.
>>>>
>>>>
>>>>   m1 n1
>>>>1   2  2
>>>>10  3  2
>>>>11  3  2
>>>>12  3  2
>>>>13  3  2
>>>>14  3  2
>>>>15  3  2
>>>>16  3  2
>>>>17  3  2
>>>>18  3  2
>>>>19  3  2
>>>>20  3  2
>>>>21  3  2
>>>>22  2  3
>>>>23  2  3
>>>>
>>>>
>>>><quote author='arun kirshna'>
>>>>
>>>>
>>>>
>>>>Hi,
>>>>Thanks. This extract every row that satisfy the condition, but I need look
>>>>at the last row (the maximum of cumulative sum) for each block (m1,n1). for
>>>>example, if I set the criteria 
>>>>
>>>>res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95, this should extract m1= 3, n1 =
>>>>2. 
>>>>
>>>>
>>>>Hi,
>>>>I am not sure I understand your question.
>>>>res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95
>>>> #[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
>>>>TRUE
>>>>#[16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
>>>>TRUE
>>>>#[31] TRUE TRUE TRUE
>>>>
>>>>This will extract all the rows.
>>>>
>>>>
>>>>res2[,1:2][res2$cterm1_P1L<0.01 & res2$cterm1_P1L!=0,]  
>>>>#   m1 n1
>>>>#21  3  2
>>>>This extract only the row you wanted.  
>>>>
>>>>For the different groups:
>>>>
>>>>aggregate(cterm1_P1L~m1+n1,data=res2,max)
>>>>#  m1 n1 cterm1_P1L
>>>>#1  2  2    0.01440
>>>>#2  3  2    0.00032
>>>>#3  2  3    0.01952
>>>> 
>>>> aggregate(cterm1_P1L~m1+n1,data=res2,function(x) max(x)<0.01)
>>>> # m1 n1 cterm1_P1L
>>>>#1  2  2      FALSE
>>>>#2  3  2       TRUE
>>>>#3  2  3      FALSE
>>>>
>>>>res4<-aggregate(cterm1_P1L~m1+n1,data=res2,function(x) max(x)<0.01)
>>>>res4[,1:2][res4[,3],]
>>>>#  m1 n1
>>>>#2  3  2
>>>>
>>>>A.K.
>>>>
>>>>
>>>>
>>>>
>>>>----- Original Message -----
>>>>From: "[hidden email]" <[hidden email]>
>>>>To: [hidden email]
>>>>Cc:
>>>>Sent: Sunday, February 3, 2013 3:58 PM
>>>>Subject: Re: cumulative sum by group and under some criteria
>>>>
>>>>Hi,
>>>>Let me restate my questions. I need to get the m1 and n1 that satisfy some
>>>>criteria, for example in this case, within each group, the maximum
>>>>cterm1_p1L ( the last row in this group) <0.01. I need to extract m1=3,
>>>>n1=2, I only need m1, n1 in the row.
>>>>
>>>>Also, how to create the structure from the data.frame, I am new to R, I need
>>>>to change the maxN and run the loop to different data.
>>>>Thanks very much for your help!
>>>>
>>>><quote author='arun kirshna'>
>>>>HI,
>>>>
>>>>I think this should be more correct:
>>>>maxN<-9 
>>>>c11<-0.2 
>>>>c12<-0.2 
>>>>p0L<-0.05 
>>>>p0H<-0.05 
>>>>p1L<-0.20 
>>>>p1H<-0.20 
>>>>
>>>>d <- structure(list(m1 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
>>>>2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), 
>>>>    n1 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 
>>>>    3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), x1 = c(0, 
>>>>    0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 
>>>>    2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3), y1 = c(0, 1, 2, 0, 
>>>>    1, 2, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 
>>>>    2, 0, 1, 2, 0, 1, 2, 0, 1, 2), Fmm = c(0, 0, 0, 0.7, 0.59, 
>>>>    0.64, 1, 1, 1, 0, 0, 0, 0, 0.63, 0.7, 0.74, 0.68, 1, 1, 1, 
>>>>    1, 0, 0, 0, 0.62, 0.63, 0.6, 0.63, 0.6, 0.68, 1, 1, 1), Fnn = c(0, 
>>>>    0.64, 1, 0, 0.51, 1, 0, 0.67, 1, 0, 0.62, 0.69, 1, 0, 0.54, 
>>>>    0.62, 1, 0, 0.63, 0.73, 1, 0, 0.63, 1, 0, 0.7, 1, 0, 0.7, 
>>>>    1, 0, 0.58, 1), Qm = c(1, 1, 1, 0.65, 0.45, 0.36, 0.5, 0.165, 
>>>>    0, 1, 1, 1, 1, 0.685, 0.38, 0.32, 0.32, 0.5, 0.185, 0.135, 
>>>>    0, 1, 1, 1, 0.69, 0.37, 0.4, 0.685, 0.4, 0.32, 0.5, 0.21, 
>>>>    0), Qn = c(1, 0.36, 0, 0.65, 0.45, 0, 0.5, 0.165, 0, 1, 0.38, 
>>>>    0.31, 0, 0.685, 0.38, 0.32, 0, 0.5, 0.185, 0.135, 0, 1, 0.37, 
>>>>    0, 0.69, 0.3, 0, 0.685, 0.3, 0, 0.5, 0.21, 0), term1_p0 = c(0.81450625, 
>>>>    0.0857375, 0.00225625, 0.0857375, 0.009025, 0.0002375, 0.00225625, 
>>>>    0.0002375, 6.25e-06, 0.7737809375, 0.1221759375, 0.00643031249999999, 
>>>>    0.0001128125, 0.081450625, 0.012860625, 0.000676875, 1.1875e-05, 
>>>>    0.0021434375, 0.0003384375, 1.78125e-05, 3.125e-07, 0.7737809375, 
>>>>    0.081450625, 0.0021434375, 0.1221759375, 0.012860625, 0.0003384375, 
>>>>    0.00643031249999999, 0.000676875, 1.78125e-05, 0.0001128125, 
>>>>    1.1875e-05, 3.125e-07), term1_p1 = c(0.4096, 0.2048, 0.0256, 
>>>>    0.2048, 0.1024, 0.0128, 0.0256, 0.0128, 0.0016, 0.32768, 
>>>>    0.24576, 0.06144, 0.00512, 0.16384, 0.12288, 0.03072, 0.00256, 
>>>>    0.02048, 0.01536, 0.00384, 0.00032, 0.32768, 0.16384, 0.02048, 
>>>>    0.24576, 0.12288, 0.01536, 0.06144, 0.03072, 0.00384, 0.00512, 
>>>>    0.00256, 0.00032)), .Names = c("m1", "n1", "x1", "y1", "Fmm", 
>>>>"Fnn", "Qm", "Qn", "term1_p0", "term1_p1"), row.names = c(NA, 
>>>>33L), class = "data.frame")
>>>>
>>>>library(zoo)
>>>>lst1<- split(d,list(d$m1,d$n1))
>>>>res2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){
>>>>x[,11:14]<-NA;
>>>>x[,11:12][x$Qm<=c11,]<-cumsum(x[,9:10][x$Qm<=c11,]);
>>>>x[,13:14][x$Qn<=c12,]<-cumsum(x[,9:10][x$Qn<=c12,]);
>>>>colnames(x)[11:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H");
>>>>x1<-na.locf(x);
>>>>x1[,11:14][is.na(x1[,11:14])]<-0;
>>>>x1}))
>>>>row.names(res2)<- 1:nrow(res2)
>>>>
>>>> res2
>>>> #  m1 n1 x1 y1  Fmm  Fnn    Qm    Qn     term1_p0 term1_p1   cterm1_P0L
>>>>cterm1_P1L   cterm1_P0H cterm1_P1H
>>>>
>>>>#1   2  2  0  0 0.00 0.00 1.000 1.000 0.8145062500  0.40960 0.0000000000  
>>>> 0.00000 0.0000000000    0.00000
>>>>#2   2  2  0  1 0.00 0.64 1.000 0.360 0.0857375000  0.20480 0.0000000000  
>>>> 0.00000 0.0000000000    0.00000
>>>>#3   2  2  0  2 0.00 1.00 1.000 0.000 0.0022562500  0.02560 0.0000000000  
>>>> 0.00000 0.0022562500    0.02560
>>>>#4   2  2  1  0 0.70 0.00 0.650 0.650 0.0857375000  0.20480 0.0000000000  
>>>> 0.00000 0.0022562500    0.02560
>>>>#5   2  2  1  1 0.59 0.51 0.450 0.450 0.0090250000  0.10240 0.0000000000  
>>>> 0.00000 0.0022562500    0.02560
>>>>#6   2  2  1  2 0.64 1.00 0.360 0.000 0.0002375000  0.01280 0.0000000000  
>>>> 0.00000 0.0024937500    0.03840
>>>>#7   2  2  2  0 1.00 0.00 0.500 0.500 0.0022562500  0.02560 0.0000000000  
>>>> 0.00000 0.0024937500    0.03840
>>>>#8   2  2  2  1 1.00 0.67 0.165 0.165 0.0002375000  0.01280 0.0002375000  
>>>> 0.01280 0.0027312500    0.05120
>>>>#9   2  2  2  2 1.00 1.00 0.000 0.000 0.0000062500  0.00160 0.0002437500  
>>>> 0.01440 0.0027375000    0.05280
>>>>#10  3  2  0  0 0.00 0.00 1.000 1.000 0.7737809375  0.32768 0.0000000000  
>>>> 0.00000 0.0000000000    0.00000
>>>>#11  3  2  0  1 0.00 0.63 1.000 0.370 0.0814506250  0.16384 0.0000000000  
>>>> 0.00000 0.0000000000    0.00000
>>>>#12  3  2  0  2 0.00 1.00 1.000 0.000 0.0021434375  0.02048 0.0000000000  
>>>> 0.00000 0.0021434375    0.02048
>>>>#13  3  2  1  0 0.62 0.00 0.690 0.690 0.1221759375  0.24576 0.0000000000  
>>>> 0.00000 0.0021434375    0.02048
>>>>#14  3  2  1  1 0.63 0.70 0.370 0.300 0.0128606250  0.12288 0.0000000000  
>>>> 0.00000 0.0021434375    0.02048
>>>>#15  3  2  1  2 0.60 1.00 0.400 0.000 0.0003384375  0.01536 0.0000000000  
>>>> 0.00000 0.0024818750    0.03584
>>>>#16  3  2  2  0 0.63 0.00 0.685 0.685 0.0064303125  0.06144 0.0000000000  
>>>> 0.00000 0.0024818750    0.03584
>>>>#17  3  2  2  1 0.60 0.70 0.400 0.300 0.0006768750  0.03072 0.0000000000  
>>>> 0.00000 0.0024818750    0.03584
>>>>#18  3  2  2  2 0.68 1.00 0.320 0.000 0.0000178125  0.00384 0.0000000000  
>>>> 0.00000 0.0024996875    0.03968
>>>>#19  3  2  3  0 1.00 0.00 0.500 0.500 0.0001128125  0.00512 0.0000000000  
>>>> 0.00000 0.0024996875    0.03968
>>>>#20  3  2  3  1 1.00 0.58 0.210 0.210 0.0000118750  0.00256 0.0000000000  
>>>> 0.00000 0.0024996875    0.03968
>>>>#21  3  2  3  2 1.00 1.00 0.000 0.000 0.0000003125  0.00032 0.0000003125  
>>>> 0.00032 0.0025000000    0.04000
>>>>#22  2  3  0  0 0.00 0.00 1.000 1.000 0.7737809375  0.32768 0.0000000000  
>>>> 0.00000 0.0000000000    0.00000
>>>>#23  2  3  0  1 0.00 0.62 1.000 0.380 0.1221759375  0.24576 0.0000000000  
>>>> 0.00000 0.0000000000    0.00000
>>>>#24  2  3  0  2 0.00 0.69 1.000 0.310 0.0064303125  0.06144 0.0000000000  
>>>> 0.00000 0.0000000000    0.00000
>>>>#25  2  3  0  3 0.00 1.00 1.000 0.000 0.0001128125  0.00512 0.0000000000  
>>>> 0.00000 0.0001128125    0.00512
>>>>#26  2  3  1  0 0.63 0.00 0.685 0.685 0.0814506250  0.16384 0.0000000000  
>>>> 0.00000 0.0001128125    0.00512
>>>>#27  2  3  1  1 0.70 0.54 0.380 0.380 0.0128606250  0.12288 0.0000000000  
>>>> 0.00000 0.0001128125    0.00512
>>>>#28  2  3  1  2 0.74 0.62 0.320 0.320 0.0006768750  0.03072 0.0000000000  
>>>> 0.00000 0.0001128125    0.00512
>>>>#29  2  3  1  3 0.68 1.00 0.320 0.000 0.0000118750  0.00256 0.0000000000  
>>>> 0.00000 0.0001246875    0.00768
>>>>#30  2  3  2  0 1.00 0.00 0.500 0.500 0.0021434375  0.02048 0.0000000000  
>>>> 0.00000 0.0001246875    0.00768
>>>>#31  2  3  2  1 1.00 0.63 0.185 0.185 0.0003384375  0.01536 0.0003384375  
>>>> 0.01536 0.0004631250    0.02304
>>>>#32  2  3  2  2 1.00 0.73 0.135 0.135 0.0000178125  0.00384 0.0003562500  
>>>> 0.01920 0.0004809375    0.02688  
>>>>#33  2  3  2  3 1.00 1.00 0.000 0.000 0.0000003125  0.00032 0.0003565625  
>>>> 0.01952 0.0004812500    0.02720
>>>>
>>>>#Sorry, some values in my previous solution didn't look right. I didn't 
>>>>A.K.
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>----- Original Message -----
>>>>From: Zjoanna <[hidden email]>
>>>>To: [hidden email]
>>>>Cc:
>>>>Sent: Friday, February 1, 2013 12:19 PM
>>>>Subject: Re: [R] cumulative sum by group and under some criteria
>>>>
>>>>Thank you very much for your reply. Your code work well with this example.
>>>>I modified a little to fit my real data, I got an error massage.
>>>>
>>>>Error in split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...) :
>>>>  Group length is 0 but data length > 0
>>>>
>>>>
>>>>On Thu, Jan 31, 2013 at 12:21 PM, arun kirshna [via R] <
>>>>[hidden email]> wrote:
>>>>
>>>>> Hi,
>>>>> Try this:
>>>>> colnames(d)<-c("m1","n1","x1","y1","p11","p12")
>>>>> library(zoo)
>>>>> res1<- do.call(rbind,lapply(lapply(split(d,list(d$m1,d$n1)),function(x)
>>>>> {x$cp11[x$x1>1]<- cumsum(x$p11[x$x1>1]);x$cp12[x$y1>1]<-
>>>>> cumsum(x$p12[x$y1>1]);x}),function(x)
>>>>> {x$cp11<-na.locf(x$cp11,na.rm=F);x$cp12<- na.locf(x$cp12,na.rm=F);x}))
>>>>> #there would be a warning here as one of the list element is NULL.  The,
>>>>> warning is okay
>>>>> row.names(res1)<- 1:nrow(res1)
>>>>> res1[,7:8][is.na(res1[,7:8])]<- 0
>>>>> res1
>>>>>  #  m1 n1 x1 y1  p11  p12 cp11 cp12
>>>>> #1   2  2  0  0 0.00 0.00 0.00 0.00
>>>>> #2   2  2  0  1 0.00 0.50 0.00 0.00
>>>>> #3   2  2  0  2 0.00 1.00 0.00 1.00
>>>>> #4   2  2  1  0 0.50 0.00 0.00 1.00
>>>>> #5   2  2  1  1 0.50 0.50 0.00 1.00
>>>>> #6   2  2  1  2 0.50 1.00 0.00 2.00
>>>>> #7   2  2  2  0 1.00 0.00 1.00 2.00
>>>>> #8   2  2  2  1 1.00 0.50 2.00 2.00
>>>>> #9   2  2  2  2 1.00 1.00 3.00 3.00
>>>>> #10  3  2  0  0 0.00 0.00 0.00 0.00
>>>>> #11  3  2  0  1 0.00 0.50 0.00 0.00
>>>>> #12  3  2  0  2 0.00 1.00 0.00 1.00
>>>>> #13  3  2  1  0 0.33 0.00 0.00 1.00
>>>>> #14  3  2  1  1 0.33 0.50 0.00 1.00
>>>>> #15  3  2  1  2 0.33 1.00 0.00 2.00
>>>>> #16  3  2  2  0 0.67 0.00 0.67 2.00
>>>>> #17  3  2  2  1 0.67 0.50 1.34 2.00
>>>>> #18  3  2  2  2 0.67 1.00 2.01 3.00
>>>>> #19  3  2  3  0 1.00 0.00 3.01 3.00
>>>>> #20  3  2  3  1 1.00 0.50 4.01 3.00
>>>>> #21  3  2  3  2 1.00 1.00 5.01 4.00
>>>>> #22  2  3  0  0 0.00 0.00 0.00 0.00
>>>>> #23  2  3  0  1 0.00 0.33 0.00 0.00
>>>>> #24  2  3  0  2 0.00 0.67 0.00 0.67
>>>>> #25  2  3  0  3 0.00 1.00 0.00 1.67
>>>>> #26  2  3  1  0 0.50 0.00 0.00 1.67
>>>>> #27  2  3  1  1 0.50 0.33 0.00 1.67
>>>>> #28  2  3  1  2 0.50 0.67 0.00 2.34
>>>>> #29  2  3  1  3 0.50 1.00 0.00 3.34
>>>>> #30  2  3  2  0 1.00 0.00 1.00 3.34
>>>>> #31  2  3  2  1 1.00 0.33 2.00 3.34
>>>>> #32  2  3  2  2 1.00 0.67 3.00 4.01
>>>>> #33  2  3  2  3 1.00 1.00 4.00 5.01
>>>>> A.K.
>>>>>
>>>>> ------------------------------
>>>>>  If you reply to this email, your message will be added to the discussion
>>>>> below:
>>>>>
>>>>> http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657196.html
>>>>> To unsubscribe from cumulative sum by group and under some criteria, click
>>>>> here<
>>>>> .
>>>>> NAML<
http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>>>>
>>>>
>>>>
>>>>
>>>>
>>>>--
>>>>View this message in context:
>>>>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657315.html
>>>>Sent from the R help mailing list archive at Nabble.com.
>>>>    [[alternative HTML version deleted]]
>>>>
>>>>______________________________________________
>>>>[hidden email] mailing list
>>>>https://stat.ethz.ch/mailman/listinfo/r-help
>>>>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>>and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>>
>>>>______________________________________________
>>>>[hidden email] mailing list
>>>>https://stat.ethz.ch/mailman/listinfo/r-help
>>>>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>>and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>></quote>
>>>>Quoted from:
>>>>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657360.html
>>>>
>>>>
>>>>______________________________________________
>>>>[hidden email] mailing list
>>>>https://stat.ethz.ch/mailman/listinfo/r-help
>>>>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>>and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>></quote>
>>>>Quoted from:
>>>>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657582.html
>>>>
>>>>    
>>>  
>>
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: cumulative sum by group and under some criteria

arun kirshna
In reply to this post by arun kirshna
Hi,

Anyway, just using some random combinations:
 dnew<- expand.grid(4:10,5:10,6:10,3:7,4:5,6:8)
names(dnew)<-c("m","n","x1","y1","x","y")
resF<- cbind(dnew,d2[rep(1:nrow(d2),nrow(dnew)),])

 row.names(resF)<- 1:nrow(resF)
 head(resF)
#  m n x1 y1 x y m1 n1 cterm1_P1L cterm1_P0H
#1 4 5  6  3 4 6  3  2    0.00032     0.0025
#2 5 5  6  3 4 6  3  2    0.00032     0.0025
#3 6 5  6  3 4 6  3  2    0.00032     0.0025
#4 7 5  6  3 4 6  3  2    0.00032     0.0025
#5 8 5  6  3 4 6  3  2    0.00032     0.0025
#6 9 5  6  3 4 6  3  2    0.00032     0.0025

 nrow(resF)
#[1] 6300
I am not sure what you want to do with this.
A.K.
________________________________
From: Joanna Zhang <[hidden email]>
To: arun <[hidden email]>
Sent: Wednesday, February 6, 2013 10:29 AM
Subject: Re: cumulative sum by group and under some criteria


Hi,

Thanks! I need to do some calculations in the expended data, the expended data would be very large, what is an efficient way, doing calculations while expending the data, something similiar with the following, or expending data using the code in your message and then add calculations in the expended data?

d3<-data.frame(d2)
   for .......{
         for {
              for .... {
                  for .....{
                       p1<- x/m
                       p2<- y/n
                      ..........
}}
}}

I also modified your code for expending data:
dnew<-expand.grid((m1+2):(maxN-(n1+2)),(n1+2):(maxN-m),0:m1,0:n1, x1:(x1+m-m1),y1:(y1+n-n1))
names(dnew)<-c("m","n","x1","y1","x","y")
dnew
resF<-cbind(dnew[,c(2,1)],d2[rep(1:nrow(d2),nrow(dnew)),])    # this is not correct, how to modify it.
resF
row.names(resF)<-1:nrow(resF)
resF




On Tue, Feb 5, 2013 at 2:46 PM, arun <[hidden email]> wrote:

Hi,

>
>You can reduce the steps to reach d2:
>res3<- with(res2,aggregate(cbind(cterm1_P1L,cterm1_P0H),by=list(m1,n1),max))
>
>#Change it to:
>res3new<-  aggregate(.~m1+n1,data=res2[,c(1:2,12:13)],max)
>res3new
> m1 n1 cterm1_P1L cterm1_P0H
>1  2  2    0.01440 0.00273750
>2  3  2    0.00032 0.00250000
>3  2  3    0.01952 0.00048125
>d2<-res3new[res3new[,3]<0.01 & res3new[,4]<0.01,]
>
> dnew<-expand.grid(4:10,5:10)
> names(dnew)<-c("n","m")
>resF<-cbind(dnew[,c(2,1)],d2[rep(1:nrow(d2),nrow(dnew)),])
>
>row.names(resF)<-1:nrow(resF)
> head(resF)
>#  m n m1 n1 cterm1_P1L cterm1_P0H
>#1 5 4  3  2    0.00032     0.0025
>#2 5 5  3  2    0.00032     0.0025
>#3 5 6  3  2    0.00032     0.0025
>#4 5 7  3  2    0.00032     0.0025
>#5 5 8  3  2    0.00032     0.0025
>#6 5 9  3  2    0.00032     0.0025
>
>A.K.
>
>________________________________
>From: Joanna Zhang <[hidden email]>
>To: arun <[hidden email]>
>Sent: Tuesday, February 5, 2013 2:48 PM
>
>Subject: Re: cumulative sum by group and under some criteria
>
>
>  Hi ,
>what I want is :
>m   n    m1    n1 cterm1_P1L   cterm1_P0H
> 5   4    3       2    0.00032         0.00250000
> 5   5    3       2    0.00032         0.00250000
> 5   6    3       2    0.00032         0.00250000
> 5   7    3       2    0.00032         0.00250000
> 5   8   3       2    0.00032         0.00250000
> 5   9   3       2    0.00032         0.00250000
>5   10  3       2    0.00032         0.00250000
>6    4   3       2    0.00032         0.00250000
>6    5   3       2    0.00032         0.00250000
>6    6   3       2    0.00032         0.00250000
>6    7   3       2    0.00032         0.00250000
>.....
>6    10  3       2    0.00032         0.00250000
>
>
>
>On Tue, Feb 5, 2013 at 1:12 PM, arun <[hidden email]> wrote:
>
>Hi,
>>
>>Saw your message on Nabble.
>>
>>
>>"I want to add some more columns based on the results. Is the following code good way to create such a data frame and How to see the column m and n in the updated data?
>> 
>>d2<- reres3[res3[,3]<0.01 & res3[,4]<0.01,]  
>># should be a typo
>>
>>colnames(d2)[1:2]<- c("m1","n1"); 
>>d2 #already a data.frame
>>
>>d3<-data.frame(d2)
>>   for (m in (m1+2):10){
>>        for (n in (n1+2):10){
>> d3<-rbind(d3, c(d2))}}" #this is not making much sense to me.  Especially, you mentioned you wanted add more columns.
>>#Running this step gave error
>>#Error: object 'm1' not found
>>
>>Not sure what you want as output.
>>Could you show the ouput that is expected:
>>
>>A.K.
>>
>>
>>
>>
>>
>>
>>
>>
>>________________________________
>>From: Joanna Zhang <[hidden email]>
>>To: arun <[hidden email]>
>>Sent: Tuesday, February 5, 2013 10:23 AM
>>
>>Subject: Re: cumulative sum by group and under some criteria
>>
>>
>>Hi,
>>
>>Yes, I changed code. You answered the questions. But how can I put two criteria in the code, if both the maximum value of cterm1_p1L <= 0.01 and cterm1_p1H <=0.01, the output the m1,n1.
>>
>>
>>
>>
>>On Tue, Feb 5, 2013 at 8:47 AM, arun <[hidden email]> wrote:
>>
>>
>>>
>>> HI,
>>>
>>>
>>>I am not getting the same results as yours:  You must have changed the dataset.
>>> res2[,1:2][res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95,] 
>>>   m1 n1
>>>1   2  2
>>>2   2  2
>>>3   2  2
>>>4   2  2
>>>5   2  2
>>>6   2  2
>>>7   2  2
>>>8   2  2
>>>9   2  2
>>>10  3  2
>>>11  3  2
>>>12  3  2
>>>13  3  2
>>>14  3  2
>>>15  3  2
>>>16  3  2
>>>17  3  2
>>>18  3  2
>>>19  3  2
>>>20  3  2
>>>21  3  2
>>>22  2  3
>>>23  2  3
>>>24  2  3
>>>25  2  3
>>>26  2  3
>>>27  2  3
>>>28  2  3
>>>29  2  3
>>>30  2  3
>>>31  2  3
>>>32  2  3
>>>33  2  3
>>>
>>>
>>>Regarding the maximum value within each block, haven't I answered in the earlier post.
>>>
>>>aggregate(cterm1_P1L~m1+n1,data=res2,max)
>>>#  m1 n1 cterm1_P1L
>>>#1  2  2    0.01440
>>>#2  3  2    0.00032
>>>#3  2  3    0.01952
>>> 
>>>
>>> with(res2,aggregate(cbind(cterm1_P1L,cterm1_P0H),by=list(m1,n1),max))
>>>#  Group.1 Group.2 cterm1_P1L cterm1_P0H
>>>#1       2       2    0.01440 0.00273750
>>>#2       3       2    0.00032 0.00250000
>>>#3       2       3    0.01952 0.00048125
>>>
>>>
>>>A.K.
>>>
>>>
>>>----- Original Message -----
>>>From: "[hidden email]" <[hidden email]>
>>>To: [hidden email]
>>>Cc:
>>>
>>>Sent: Tuesday, February 5, 2013 9:33 AM
>>>Subject: Re: cumulative sum by group and under some criteria
>>>
>>>Hi,
>>>If use this
>>>
>>>res2[,1:2][res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95,]
>>>
>>>the results are the following, but actually only m1=3, n1=2 sastify the criteria, as I need to look at the row with maximum value within each block,not every row.
>>>
>>>
>>>   m1 n1
>>>1   2  2
>>>10  3  2
>>>11  3  2
>>>12  3  2
>>>13  3  2
>>>14  3  2
>>>15  3  2
>>>16  3  2
>>>17  3  2
>>>18  3  2
>>>19  3  2
>>>20  3  2
>>>21  3  2
>>>22  2  3
>>>23  2  3
>>>
>>>
>>><quote author='arun kirshna'>
>>>
>>>
>>>
>>>Hi,
>>>Thanks. This extract every row that satisfy the condition, but I need look
>>>at the last row (the maximum of cumulative sum) for each block (m1,n1). for
>>>example, if I set the criteria 
>>>
>>>res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95, this should extract m1= 3, n1 =
>>>2. 
>>>
>>>
>>>Hi,
>>>I am not sure I understand your question.
>>>res2$cterm1_P1L<0.6 & res2$cterm1_P0H<0.95
>>> #[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
>>>TRUE
>>>#[16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
>>>TRUE
>>>#[31] TRUE TRUE TRUE
>>>
>>>This will extract all the rows.
>>>
>>>
>>>res2[,1:2][res2$cterm1_P1L<0.01 & res2$cterm1_P1L!=0,]  
>>>#   m1 n1
>>>#21  3  2
>>>This extract only the row you wanted.  
>>>
>>>For the different groups:
>>>
>>>aggregate(cterm1_P1L~m1+n1,data=res2,max)
>>>#  m1 n1 cterm1_P1L
>>>#1  2  2    0.01440
>>>#2  3  2    0.00032
>>>#3  2  3    0.01952
>>> 
>>> aggregate(cterm1_P1L~m1+n1,data=res2,function(x) max(x)<0.01)
>>> # m1 n1 cterm1_P1L
>>>#1  2  2      FALSE
>>>#2  3  2       TRUE
>>>#3  2  3      FALSE
>>>
>>>res4<-aggregate(cterm1_P1L~m1+n1,data=res2,function(x) max(x)<0.01)
>>>res4[,1:2][res4[,3],]
>>>#  m1 n1
>>>#2  3  2
>>>
>>>A.K.
>>>
>>>
>>>
>>>
>>>----- Original Message -----
>>>From: "[hidden email]" <[hidden email]>
>>>To: [hidden email]
>>>Cc:
>>>Sent: Sunday, February 3, 2013 3:58 PM
>>>Subject: Re: cumulative sum by group and under some criteria
>>>
>>>Hi,
>>>Let me restate my questions. I need to get the m1 and n1 that satisfy some
>>>criteria, for example in this case, within each group, the maximum
>>>cterm1_p1L ( the last row in this group) <0.01. I need to extract m1=3,
>>>n1=2, I only need m1, n1 in the row.
>>>
>>>Also, how to create the structure from the data.frame, I am new to R, I need
>>>to change the maxN and run the loop to different data.
>>>Thanks very much for your help!
>>>
>>><quote author='arun kirshna'>
>>>HI,
>>>
>>>I think this should be more correct:
>>>maxN<-9 
>>>c11<-0.2 
>>>c12<-0.2 
>>>p0L<-0.05 
>>>p0H<-0.05 
>>>p1L<-0.20 
>>>p1H<-0.20 
>>>
>>>d <- structure(list(m1 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
>>>2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), 
>>>    n1 = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 
>>>    3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), x1 = c(0, 
>>>    0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 
>>>    2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3), y1 = c(0, 1, 2, 0, 
>>>    1, 2, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 
>>>    2, 0, 1, 2, 0, 1, 2, 0, 1, 2), Fmm = c(0, 0, 0, 0.7, 0.59, 
>>>    0.64, 1, 1, 1, 0, 0, 0, 0, 0.63, 0.7, 0.74, 0.68, 1, 1, 1, 
>>>    1, 0, 0, 0, 0.62, 0.63, 0.6, 0.63, 0.6, 0.68, 1, 1, 1), Fnn = c(0, 
>>>    0.64, 1, 0, 0.51, 1, 0, 0.67, 1, 0, 0.62, 0.69, 1, 0, 0.54, 
>>>    0.62, 1, 0, 0.63, 0.73, 1, 0, 0.63, 1, 0, 0.7, 1, 0, 0.7, 
>>>    1, 0, 0.58, 1), Qm = c(1, 1, 1, 0.65, 0.45, 0.36, 0.5, 0.165, 
>>>    0, 1, 1, 1, 1, 0.685, 0.38, 0.32, 0.32, 0.5, 0.185, 0.135, 
>>>    0, 1, 1, 1, 0.69, 0.37, 0.4, 0.685, 0.4, 0.32, 0.5, 0.21, 
>>>    0), Qn = c(1, 0.36, 0, 0.65, 0.45, 0, 0.5, 0.165, 0, 1, 0.38, 
>>>    0.31, 0, 0.685, 0.38, 0.32, 0, 0.5, 0.185, 0.135, 0, 1, 0.37, 
>>>    0, 0.69, 0.3, 0, 0.685, 0.3, 0, 0.5, 0.21, 0), term1_p0 = c(0.81450625, 
>>>    0.0857375, 0.00225625, 0.0857375, 0.009025, 0.0002375, 0.00225625, 
>>>    0.0002375, 6.25e-06, 0.7737809375, 0.1221759375, 0.00643031249999999, 
>>>    0.0001128125, 0.081450625, 0.012860625, 0.000676875, 1.1875e-05, 
>>>    0.0021434375, 0.0003384375, 1.78125e-05, 3.125e-07, 0.7737809375, 
>>>    0.081450625, 0.0021434375, 0.1221759375, 0.012860625, 0.0003384375, 
>>>    0.00643031249999999, 0.000676875, 1.78125e-05, 0.0001128125, 
>>>    1.1875e-05, 3.125e-07), term1_p1 = c(0.4096, 0.2048, 0.0256, 
>>>    0.2048, 0.1024, 0.0128, 0.0256, 0.0128, 0.0016, 0.32768, 
>>>    0.24576, 0.06144, 0.00512, 0.16384, 0.12288, 0.03072, 0.00256, 
>>>    0.02048, 0.01536, 0.00384, 0.00032, 0.32768, 0.16384, 0.02048, 
>>>    0.24576, 0.12288, 0.01536, 0.06144, 0.03072, 0.00384, 0.00512, 
>>>    0.00256, 0.00032)), .Names = c("m1", "n1", "x1", "y1", "Fmm", 
>>>"Fnn", "Qm", "Qn", "term1_p0", "term1_p1"), row.names = c(NA, 
>>>33L), class = "data.frame")
>>>
>>>library(zoo)
>>>lst1<- split(d,list(d$m1,d$n1))
>>>res2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){
>>>x[,11:14]<-NA;
>>>x[,11:12][x$Qm<=c11,]<-cumsum(x[,9:10][x$Qm<=c11,]);
>>>x[,13:14][x$Qn<=c12,]<-cumsum(x[,9:10][x$Qn<=c12,]);
>>>colnames(x)[11:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H");
>>>x1<-na.locf(x);
>>>x1[,11:14][is.na(x1[,11:14])]<-0;
>>>x1}))
>>>row.names(res2)<- 1:nrow(res2)
>>>
>>> res2
>>> #  m1 n1 x1 y1  Fmm  Fnn    Qm    Qn     term1_p0 term1_p1   cterm1_P0L
>>>cterm1_P1L   cterm1_P0H cterm1_P1H
>>>
>>>#1   2  2  0  0 0.00 0.00 1.000 1.000 0.8145062500  0.40960 0.0000000000  
>>> 0.00000 0.0000000000    0.00000
>>>#2   2  2  0  1 0.00 0.64 1.000 0.360 0.0857375000  0.20480 0.0000000000  
>>> 0.00000 0.0000000000    0.00000
>>>#3   2  2  0  2 0.00 1.00 1.000 0.000 0.0022562500  0.02560 0.0000000000  
>>> 0.00000 0.0022562500    0.02560
>>>#4   2  2  1  0 0.70 0.00 0.650 0.650 0.0857375000  0.20480 0.0000000000  
>>> 0.00000 0.0022562500    0.02560
>>>#5   2  2  1  1 0.59 0.51 0.450 0.450 0.0090250000  0.10240 0.0000000000  
>>> 0.00000 0.0022562500    0.02560
>>>#6   2  2  1  2 0.64 1.00 0.360 0.000 0.0002375000  0.01280 0.0000000000  
>>> 0.00000 0.0024937500    0.03840
>>>#7   2  2  2  0 1.00 0.00 0.500 0.500 0.0022562500  0.02560 0.0000000000  
>>> 0.00000 0.0024937500    0.03840
>>>#8   2  2  2  1 1.00 0.67 0.165 0.165 0.0002375000  0.01280 0.0002375000  
>>> 0.01280 0.0027312500    0.05120
>>>#9   2  2  2  2 1.00 1.00 0.000 0.000 0.0000062500  0.00160 0.0002437500  
>>> 0.01440 0.0027375000    0.05280
>>>#10  3  2  0  0 0.00 0.00 1.000 1.000 0.7737809375  0.32768 0.0000000000  
>>> 0.00000 0.0000000000    0.00000
>>>#11  3  2  0  1 0.00 0.63 1.000 0.370 0.0814506250  0.16384 0.0000000000  
>>> 0.00000 0.0000000000    0.00000
>>>#12  3  2  0  2 0.00 1.00 1.000 0.000 0.0021434375  0.02048 0.0000000000  
>>> 0.00000 0.0021434375    0.02048
>>>#13  3  2  1  0 0.62 0.00 0.690 0.690 0.1221759375  0.24576 0.0000000000  
>>> 0.00000 0.0021434375    0.02048
>>>#14  3  2  1  1 0.63 0.70 0.370 0.300 0.0128606250  0.12288 0.0000000000  
>>> 0.00000 0.0021434375    0.02048
>>>#15  3  2  1  2 0.60 1.00 0.400 0.000 0.0003384375  0.01536 0.0000000000  
>>> 0.00000 0.0024818750    0.03584
>>>#16  3  2  2  0 0.63 0.00 0.685 0.685 0.0064303125  0.06144 0.0000000000  
>>> 0.00000 0.0024818750    0.03584
>>>#17  3  2  2  1 0.60 0.70 0.400 0.300 0.0006768750  0.03072 0.0000000000  
>>> 0.00000 0.0024818750    0.03584
>>>#18  3  2  2  2 0.68 1.00 0.320 0.000 0.0000178125  0.00384 0.0000000000  
>>> 0.00000 0.0024996875    0.03968
>>>#19  3  2  3  0 1.00 0.00 0.500 0.500 0.0001128125  0.00512 0.0000000000  
>>> 0.00000 0.0024996875    0.03968
>>>#20  3  2  3  1 1.00 0.58 0.210 0.210 0.0000118750  0.00256 0.0000000000  
>>> 0.00000 0.0024996875    0.03968
>>>#21  3  2  3  2 1.00 1.00 0.000 0.000 0.0000003125  0.00032 0.0000003125  
>>> 0.00032 0.0025000000    0.04000
>>>#22  2  3  0  0 0.00 0.00 1.000 1.000 0.7737809375  0.32768 0.0000000000  
>>> 0.00000 0.0000000000    0.00000
>>>#23  2  3  0  1 0.00 0.62 1.000 0.380 0.1221759375  0.24576 0.0000000000  
>>> 0.00000 0.0000000000    0.00000
>>>#24  2  3  0  2 0.00 0.69 1.000 0.310 0.0064303125  0.06144 0.0000000000  
>>> 0.00000 0.0000000000    0.00000
>>>#25  2  3  0  3 0.00 1.00 1.000 0.000 0.0001128125  0.00512 0.0000000000  
>>> 0.00000 0.0001128125    0.00512
>>>#26  2  3  1  0 0.63 0.00 0.685 0.685 0.0814506250  0.16384 0.0000000000  
>>> 0.00000 0.0001128125    0.00512
>>>#27  2  3  1  1 0.70 0.54 0.380 0.380 0.0128606250  0.12288 0.0000000000  
>>> 0.00000 0.0001128125    0.00512
>>>#28  2  3  1  2 0.74 0.62 0.320 0.320 0.0006768750  0.03072 0.0000000000  
>>> 0.00000 0.0001128125    0.00512
>>>#29  2  3  1  3 0.68 1.00 0.320 0.000 0.0000118750  0.00256 0.0000000000  
>>> 0.00000 0.0001246875    0.00768
>>>#30  2  3  2  0 1.00 0.00 0.500 0.500 0.0021434375  0.02048 0.0000000000  
>>> 0.00000 0.0001246875    0.00768
>>>#31  2  3  2  1 1.00 0.63 0.185 0.185 0.0003384375  0.01536 0.0003384375  
>>> 0.01536 0.0004631250    0.02304
>>>#32  2  3  2  2 1.00 0.73 0.135 0.135 0.0000178125  0.00384 0.0003562500  
>>> 0.01920 0.0004809375    0.02688  
>>>#33  2  3  2  3 1.00 1.00 0.000 0.000 0.0000003125  0.00032 0.0003565625  
>>> 0.01952 0.0004812500    0.02720
>>>
>>>#Sorry, some values in my previous solution didn't look right. I didn't 
>>>A.K.
>>>
>>>
>>>
>>>
>>>
>>>----- Original Message -----
>>>From: Zjoanna <[hidden email]>
>>>To: [hidden email]
>>>Cc:
>>>Sent: Friday, February 1, 2013 12:19 PM
>>>Subject: Re: [R] cumulative sum by group and under some criteria
>>>
>>>Thank you very much for your reply. Your code work well with this example.
>>>I modified a little to fit my real data, I got an error massage.
>>>
>>>Error in split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...) :
>>>  Group length is 0 but data length > 0
>>>
>>>
>>>On Thu, Jan 31, 2013 at 12:21 PM, arun kirshna [via R] <
>>>[hidden email]> wrote:
>>>
>>>> Hi,
>>>> Try this:
>>>> colnames(d)<-c("m1","n1","x1","y1","p11","p12")
>>>> library(zoo)
>>>> res1<- do.call(rbind,lapply(lapply(split(d,list(d$m1,d$n1)),function(x)
>>>> {x$cp11[x$x1>1]<- cumsum(x$p11[x$x1>1]);x$cp12[x$y1>1]<-
>>>> cumsum(x$p12[x$y1>1]);x}),function(x)
>>>> {x$cp11<-na.locf(x$cp11,na.rm=F);x$cp12<- na.locf(x$cp12,na.rm=F);x}))
>>>> #there would be a warning here as one of the list element is NULL.  The,
>>>> warning is okay
>>>> row.names(res1)<- 1:nrow(res1)
>>>> res1[,7:8][is.na(res1[,7:8])]<- 0
>>>> res1
>>>>  #  m1 n1 x1 y1  p11  p12 cp11 cp12
>>>> #1   2  2  0  0 0.00 0.00 0.00 0.00
>>>> #2   2  2  0  1 0.00 0.50 0.00 0.00
>>>> #3   2  2  0  2 0.00 1.00 0.00 1.00
>>>> #4   2  2  1  0 0.50 0.00 0.00 1.00
>>>> #5   2  2  1  1 0.50 0.50 0.00 1.00
>>>> #6   2  2  1  2 0.50 1.00 0.00 2.00
>>>> #7   2  2  2  0 1.00 0.00 1.00 2.00
>>>> #8   2  2  2  1 1.00 0.50 2.00 2.00
>>>> #9   2  2  2  2 1.00 1.00 3.00 3.00
>>>> #10  3  2  0  0 0.00 0.00 0.00 0.00
>>>> #11  3  2  0  1 0.00 0.50 0.00 0.00
>>>> #12  3  2  0  2 0.00 1.00 0.00 1.00
>>>> #13  3  2  1  0 0.33 0.00 0.00 1.00
>>>> #14  3  2  1  1 0.33 0.50 0.00 1.00
>>>> #15  3  2  1  2 0.33 1.00 0.00 2.00
>>>> #16  3  2  2  0 0.67 0.00 0.67 2.00
>>>> #17  3  2  2  1 0.67 0.50 1.34 2.00
>>>> #18  3  2  2  2 0.67 1.00 2.01 3.00
>>>> #19  3  2  3  0 1.00 0.00 3.01 3.00
>>>> #20  3  2  3  1 1.00 0.50 4.01 3.00
>>>> #21  3  2  3  2 1.00 1.00 5.01 4.00
>>>> #22  2  3  0  0 0.00 0.00 0.00 0.00
>>>> #23  2  3  0  1 0.00 0.33 0.00 0.00
>>>> #24  2  3  0  2 0.00 0.67 0.00 0.67
>>>> #25  2  3  0  3 0.00 1.00 0.00 1.67
>>>> #26  2  3  1  0 0.50 0.00 0.00 1.67
>>>> #27  2  3  1  1 0.50 0.33 0.00 1.67
>>>> #28  2  3  1  2 0.50 0.67 0.00 2.34
>>>> #29  2  3  1  3 0.50 1.00 0.00 3.34
>>>> #30  2  3  2  0 1.00 0.00 1.00 3.34
>>>> #31  2  3  2  1 1.00 0.33 2.00 3.34
>>>> #32  2  3  2  2 1.00 0.67 3.00 4.01
>>>> #33  2  3  2  3 1.00 1.00 4.00 5.01
>>>> A.K.
>>>>
>>>> ------------------------------
>>>>  If you reply to this email, your message will be added to the discussion
>>>> below:
>>>>
>>>> http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657196.html
>>>> To unsubscribe from cumulative sum by group and under some criteria, click
>>>> here<
>>>> .
>>>> NAML<
http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>>>
>>>
>>>
>>>
>>>
>>>--
>>>View this message in context:
>>>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657315.html
>>>Sent from the R help mailing list archive at Nabble.com.
>>>    [[alternative HTML version deleted]]
>>>
>>>______________________________________________
>>>[hidden email] mailing list
>>>https://stat.ethz.ch/mailman/listinfo/r-help
>>>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>and provide commented, minimal, self-contained, reproducible code.
>>>
>>>
>>>______________________________________________
>>>[hidden email] mailing list
>>>https://stat.ethz.ch/mailman/listinfo/r-help
>>>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>and provide commented, minimal, self-contained, reproducible code.
>>>
>>></quote>
>>>Quoted from:
>>>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657360.html
>>>
>>>
>>>______________________________________________
>>>[hidden email] mailing list
>>>https://stat.ethz.ch/mailman/listinfo/r-help
>>>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>and provide commented, minimal, self-contained, reproducible code.
>>>
>>></quote>
>>>Quoted from:
>>>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657582.html
>>>
>>>    
>>  
>     

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
123