[R] cumulative sum by group and under some criteria

arun smartpink111 at yahoo.com
Sat Feb 2 04:17:49 CET 2013


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 <Zjoanna2013 at gmail.com>
To: r-help at r-project.org
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] <
ml-node+s789695n4657196h87 at n4.nabble.com> 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<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4657074&code=WmpvYW5uYTIwMTNAZ21haWwuY29tfDQ2NTcwNzR8LTE3NTE1MDA0MzY=>
> .
> 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]]

______________________________________________
R-help at r-project.org 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.




More information about the R-help mailing list