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@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]]

