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]]
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] < ml-node+s789695n4657316h7@n4.nabble.com> 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<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-tp4657074p4657319.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
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.
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 <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.
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: "Zjoanna2013 at gmail.com" <Zjoanna2013 at gmail.com> To: smartpink111 at yahoo.com 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 <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. ______________________________________________ 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. </quote> Quoted from: http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657360.html
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: "Zjoanna2013 at gmail.com" <Zjoanna2013 at gmail.com> To: smartpink111 at yahoo.com 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 <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. ______________________________________________ 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. </quote> Quoted from: http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657360.html
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 <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> 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 <smartpink111 at yahoo.com> wrote:> >Hi, > >Did this answered your question? > >A.K. > > >----- Original Message ----- > >From: arun <smartpink111 at yahoo.com> >To: "Zjoanna2013 at gmail.com" <Zjoanna2013 at gmail.com> >Cc: R help <r-help at r-project.org> >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: "Zjoanna2013 at gmail.com" <Zjoanna2013 at gmail.com> >To: smartpink111 at yahoo.com >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 <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. > > >______________________________________________ >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. > ></quote> >Quoted from: >http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657360.html >
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 <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> 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 <smartpink111 at yahoo.com> 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 <zjoanna2013 at gmail.com> >To: arun <smartpink111 at yahoo.com> >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 <smartpink111 at yahoo.com> 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: "Zjoanna2013 at gmail.com" <Zjoanna2013 at gmail.com> >>To: smartpink111 at yahoo.com >>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: "Zjoanna2013 at gmail.com" <Zjoanna2013 at gmail.com> >>To: smartpink111 at yahoo.com >>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 <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. >> >> >>______________________________________________ >>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. >> >></quote> >>Quoted from: >>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657360.html >> >> >>______________________________________________ >>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. >> >></quote> >>Quoted from: >>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657582.html >> >>???? >??
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 <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> 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 <smartpink111 at yahoo.com> 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 <zjoanna2013 at gmail.com> >To: arun <smartpink111 at yahoo.com> >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 <smartpink111 at yahoo.com> 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 <zjoanna2013 at gmail.com> >>To: arun <smartpink111 at yahoo.com> >>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 <smartpink111 at yahoo.com> 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 <zjoanna2013 at gmail.com> >>>To: arun <smartpink111 at yahoo.com> >>>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 <smartpink111 at yahoo.com> 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: "Zjoanna2013 at gmail.com" <Zjoanna2013 at gmail.com> >>>>To: smartpink111 at yahoo.com >>>>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: "Zjoanna2013 at gmail.com" <Zjoanna2013 at gmail.com> >>>>To: smartpink111 at yahoo.com >>>>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 <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. >>>> >>>> >>>>______________________________________________ >>>>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. >>>> >>>></quote> >>>>Quoted from: >>>>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657360.html >>>> >>>> >>>>______________________________________________ >>>>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. >>>> >>>></quote> >>>>Quoted from: >>>>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657582.html >>>> >>>>???? >>>?? >> > >
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 <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> 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 <smartpink111 at yahoo.com> 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 <zjoanna2013 at gmail.com> >To: arun <smartpink111 at yahoo.com> >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 <smartpink111 at yahoo.com> 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 <zjoanna2013 at gmail.com> >>To: arun <smartpink111 at yahoo.com> >>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 <smartpink111 at yahoo.com> 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: "Zjoanna2013 at gmail.com" <Zjoanna2013 at gmail.com> >>>To: smartpink111 at yahoo.com >>>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: "Zjoanna2013 at gmail.com" <Zjoanna2013 at gmail.com> >>>To: smartpink111 at yahoo.com >>>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 <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. >>> >>> >>>______________________________________________ >>>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. >>> >>></quote> >>>Quoted from: >>>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657360.html >>> >>> >>>______________________________________________ >>>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. >>> >>></quote> >>>Quoted from: >>>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4657582.html >>> >>>???? >>?? >?????
do.call(rbind,lapply(2:3,function(m1) do.call(rbind,lapply(2:2,function(n1) do.call(rbind,lapply(0:(m1-1),function(x1) do.call(rbind,lapply(0:(n1-1),function(y1) expand.grid(m1,n1,x1,y1))))))))) #?? Var1 Var2 Var3 Var4 #1???? 2??? 2??? 0??? 0 #2???? 2??? 2??? 0??? 1 #3???? 2??? 2??? 1??? 0 #4???? 2??? 2??? 1??? 1 #5???? 3??? 2??? 0??? 0 #6???? 3??? 2??? 0??? 1 #7???? 3??? 2??? 1??? 0 #8???? 3??? 2??? 1??? 1 #9???? 3??? 2??? 2??? 0 #10??? 3??? 2??? 2??? 1 res<-do.call(rbind,lapply(2:3,function(m1) do.call(rbind,lapply(2:2,function(n1) do.call(rbind,lapply(0:(m1-1),function(x1) do.call(rbind,lapply(0:(n1-1),function(y1) do.call(rbind,lapply((m1+2):(7-n1),function(m) do.call(rbind,lapply((n1+2):(9-m),function(n) do.call(rbind,lapply(x1:(x1+m-m1), function(x) expand.grid(m1,n1,x1,y1,m,n,x)) ))))))))))))) names(res)<- c("m1","n1","x1","y1","m","n","x") ?res #?? m1 n1 x1 y1 m n x #1?? 2? 2? 0? 0 4 4 0 #2?? 2? 2? 0? 0 4 4 1 #3?? 2? 2? 0? 0 4 4 2 #4?? 2? 2? 0? 0 4 5 0 #5?? 2? 2? 0? 0 4 5 1 #6?? 2? 2? 0? 0 4 5 2 #7?? 2? 2? 0? 0 5 4 0 #8?? 2? 2? 0? 0 5 4 1 #9?? 2? 2? 0? 0 5 4 2 #10? 2? 2? 0? 0 5 4 3 #11? 2? 2? 0? 1 4 4 0 #12? 2? 2? 0? 1 4 4 1 #13? 2? 2? 0? 1 4 4 2 #14? 2? 2? 0? 1 4 5 0 #15? 2? 2? 0? 1 4 5 1 #16? 2? 2? 0? 1 4 5 2 #17? 2? 2? 0? 1 5 4 0 #18? 2? 2? 0? 1 5 4 1 #19? 2? 2? 0? 1 5 4 2 #20? 2? 2? 0? 1 5 4 3 #21? 2? 2? 1? 0 4 4 1 #22? 2? 2? 1? 0 4 4 2 #23? 2? 2? 1? 0 4 4 3 #24? 2? 2? 1? 0 4 5 1 #25? 2? 2? 1? 0 4 5 2 #26? 2? 2? 1? 0 4 5 3 #27? 2? 2? 1? 0 5 4 1 #28? 2? 2? 1? 0 5 4 2 #29? 2? 2? 1? 0 5 4 3 #30? 2? 2? 1? 0 5 4 4 #31? 2? 2? 1? 1 4 4 1 #32? 2? 2? 1? 1 4 4 2 #33? 2? 2? 1? 1 4 4 3 #34? 2? 2? 1? 1 4 5 1 #35? 2? 2? 1? 1 4 5 2 #36? 2? 2? 1? 1 4 5 3 #37? 2? 2? 1? 1 5 4 1 #38? 2? 2? 1? 1 5 4 2 #39? 2? 2? 1? 1 5 4 3 #40? 2? 2? 1? 1 5 4 4 #41? 3? 2? 0? 0 5 4 0 #42? 3? 2? 0? 0 5 4 1 #43? 3? 2? 0? 0 5 4 2 #44? 3? 2? 0? 1 5 4 0 #45? 3? 2? 0? 1 5 4 1 #46? 3? 2? 0? 1 5 4 2 #47? 3? 2? 1? 0 5 4 1 #48? 3? 2? 1? 0 5 4 2 #49? 3? 2? 1? 0 5 4 3 #50? 3? 2? 1? 1 5 4 1 #51? 3? 2? 1? 1 5 4 2 #52? 3? 2? 1? 1 5 4 3 #53? 3? 2? 2? 0 5 4 2 #54? 3? 2? 2? 0 5 4 3 #55? 3? 2? 2? 0 5 4 4 #56? 3? 2? 2? 1 5 4 2 #57? 3? 2? 2? 1 5 4 3 #58? 3? 2? 2? 1 5 4 4 A.K. ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Saturday, February 16, 2013 8:46 PM Subject: Re: [R] cumulative sum by group and under some criteria Hi, What I need is to expand each row by adding several columns and . Let me restate the question. I have a dataset d, I want to expand it to d2 showed below. d<-data.frame() for (m1 in 2:3) { ??? for (n1 in 2:2) { ??????? for (x1 in 0:(m1-1)) { ??????????? for (y1 in 0:(n1-1)) { d<-rbind(d,c(m1,n1,x1,y1)) } } }} colnames(d)<-c("m1","n1","x1","y1") d ?? m1 n1 x1 y1 1?? 2? 2? 0? 0 2?? 2? 2? 0? 1 3?? 2? 2? 1? 0 4?? 2? 2? 1? 1 5?? 3? 2? 0? 0 6?? 3? 2? 0? 1 7?? 3? 2? 1? 0 8?? 3? 2? 1? 1 9?? 3? 2? 2? 0 10? 3? 2? 2? 1 I want to expand it as follows: for (m in (m1+2): (7-n1){ ??? for (n in (n1+2):(9-m){ ??????? for (x in x1:(x1+m-m1){????????? }}} so for the first row, ?? m1 n1 x1 y1 1?? 2? 2? 0? 0 it should be expanded as ?? m1 n1 x1 y1? m? n? x ??? 2? 2? 0? 0? 4? 4? 0 ??? 2? 2? 0? 0? 4? 4? 1 ??? 2? 2? 0? 0? 4? 4? 2 ??? 2? 2? 0? 0? 4? 5? 0 ??? 2? 2? 0? 0? 4? 5? 1 ??? 2? 2? 0? 0? 4? 5? 2 On Tue, Feb 12, 2013 at 8:19 PM, arun <smartpink111 at yahoo.com> wrote: Hi,> >Saw your reply again in Nabble. ?I thought I sent you the solution previously. > > >res3new<- ?aggregate(.~m1+n1,data=res2[,c(1:2,12:13)],max)? > >?d2<-res3new[res3new[,3]<0.01 & res3new[,4]<0.01,]? > > >m1<- 3 #from d2 >maxN<- 9 >n1<- 2 #from d2 > >In the example that you provided: >?(m1+2):(maxN-(n1+2)) >#[1] 5? >?(n1+2):(maxN-5) >#[1] 4? >#Suppose >?x1<- 4? >?y1<- 2? >?x1:(x1+5-m1) >#[1] 4 5 6 >?y1:(y1+4-n1) >#[1] 2 3 4 > >?datnew<-expand.grid(5,4,4:6,2:4) >?colnames(datnew)<- c("m","n","x","y") >datnew<-within(datnew,{p1<- x/m;p2<-y/n}) >res<-cbind(datnew,d2[rep(1:nrow(d2),nrow(datnew)),]) >?row.names(res)<- 1:nrow(res) >?res ># ?m n x y ? p2 ?p1 m1 n1 cterm1_P1L cterm1_P0H >#1 5 4 4 2 0.50 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#2 5 4 5 2 0.50 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#3 5 4 6 2 0.50 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#4 5 4 4 3 0.75 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#5 5 4 5 3 0.75 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#6 5 4 6 3 0.75 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#7 5 4 4 4 1.00 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#8 5 4 5 4 1.00 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#9 5 4 6 4 1.00 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 > >A.K. > > > > > >----- Original Message ----- >From: Zjoanna <Zjoanna2013 at gmail.com> >To: r-help at r-project.org >Cc: > >Sent: Sunday, February 10, 2013 6:04 PM >Subject: Re: [R] cumulative sum by group and under some criteria > > >Hi, >How to expand or loop for one variable n based on another variable? for >example, I want to add m (from m1 to maxN- n1-2) and for each m, I want to >add n (n1+2 to maxN-m), and similarly add x and y, then I need to do some >calculations. > >d3<-data.frame(d2) >? ? for (m in (m1+2):(maxN-(n1+2)){ >? ? ? ?for (n in (n1+2):(maxN-m)){ >? ? ? ? ? ? ?for (x in x1:(x1+m-m1)){ >? ? ? ? ? ? ? ? ? for (y in y1:(y1+n-n1)){ >? ? ? ? ? ? ? ? ? ? ? ?p1<- x/m >? ? ? ? ? ? ? ? ? ? ? ?p2<- y/n >}}}} > >On Thu, Feb 7, 2013 at 12:16 AM, arun kirshna [via R] < >ml-node+s789695n4657773h74 at n4.nabble.com> wrote: > >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=0>> >> >> To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=1>> > >> >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=2>> > >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=3>> >> >> >To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=4>> > >> >> >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]<http://user/SendEmail.jtp?type=node&node=4657773&i=5>> > >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=6>> >> >> >>To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=7>> > >> >> >>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=8>> > >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=9>"; >> <[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=10>> >> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=11> >>? >>>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=12>"; >> <[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=13>> >> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=14> >>? >>>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=15>> >> >> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=16> > >> >>>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] <http://user/SendEmail.jtp?type=node&node=4657773&i=17>> > >> 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] <http://user/SendEmail.jtp?type=node&node=4657773&i=18>mailing list > >> >>>https://stat.ethz.ch/mailman/listinfo/r-help >> >>>PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > >> >>>and provide commented, minimal, self-contained, reproducible code. >> >>> >> >>> >> >>>______________________________________________ >> >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=19>mailing list > >> >>>https://stat.ethz.ch/mailman/listinfo/r-help >> >>>PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<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] <http://user/SendEmail.jtp?type=node&node=4657773&i=20>mailing list > >> >>>https://stat.ethz.ch/mailman/listinfo/r-help >> >>>PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<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] <http://user/SendEmail.jtp?type=node&node=4657773&i=21>mailing list > >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<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-tp4657074p4657773.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-tp4657074p4658133.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. > >?? ?
d2<- data.frame() for (m1 in 2:3) { ??? for (n1 in 2:2) { ??????? for (x1 in 0:(m1-1)) { ??????????? for (y1 in 0:(n1-1)) { ??? ??? for (m in (m1+2): (7-n1)){ ???? ??? ??? ? for (n in (n1+2):(9-m)){ ? ??? ??? ???? for (x in x1:(x1+m-m1)){ ?d2<- rbind(d2,c(m1,n1,x1,y1,m,n,x)) ?}}}}}}} colnames(d2)<-c("m1","n1","x1","y1","m","n","x") res<-do.call(rbind,lapply(2:3,function(m1) do.call(rbind,lapply(2:2,function(n1) do.call(rbind,lapply(0:(m1-1),function(x1) do.call(rbind,lapply(0:(n1-1),function(y1) do.call(rbind,lapply((m1+2):(7-n1),function(m) do.call(rbind,lapply((n1+2):(9-m),function(n) do.call(rbind,lapply(x1:(x1+m-m1), function(x) expand.grid(m1,n1,x1,y1,m,n,x)) ))))))))))))) names(res)<- c("m1","n1","x1","y1","m","n","x") ?attr(res,"out.attrs")<-NULL ?identical(d2,res) #[1] TRUE A.K. ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Saturday, February 16, 2013 8:46 PM Subject: Re: [R] cumulative sum by group and under some criteria Hi, What I need is to expand each row by adding several columns and . Let me restate the question. I have a dataset d, I want to expand it to d2 showed below. d<-data.frame() for (m1 in 2:3) { ??? for (n1 in 2:2) { ??????? for (x1 in 0:(m1-1)) { ??????????? for (y1 in 0:(n1-1)) { d<-rbind(d,c(m1,n1,x1,y1)) } } }} colnames(d)<-c("m1","n1","x1","y1") d ?? m1 n1 x1 y1 1?? 2? 2? 0? 0 2?? 2? 2? 0? 1 3?? 2? 2? 1? 0 4?? 2? 2? 1? 1 5?? 3? 2? 0? 0 6?? 3? 2? 0? 1 7?? 3? 2? 1? 0 8?? 3? 2? 1? 1 9?? 3? 2? 2? 0 10? 3? 2? 2? 1 I want to expand it as follows: for (m in (m1+2): (7-n1){ ??? for (n in (n1+2):(9-m){ ??????? for (x in x1:(x1+m-m1){????????? }}} so for the first row, ?? m1 n1 x1 y1 1?? 2? 2? 0? 0 it should be expanded as ?? m1 n1 x1 y1? m? n? x ??? 2? 2? 0? 0? 4? 4? 0 ??? 2? 2? 0? 0? 4? 4? 1 ??? 2? 2? 0? 0? 4? 4? 2 ??? 2? 2? 0? 0? 4? 5? 0 ??? 2? 2? 0? 0? 4? 5? 1 ??? 2? 2? 0? 0? 4? 5? 2 On Tue, Feb 12, 2013 at 8:19 PM, arun <smartpink111 at yahoo.com> wrote: Hi,> >Saw your reply again in Nabble. ?I thought I sent you the solution previously. > > >res3new<- ?aggregate(.~m1+n1,data=res2[,c(1:2,12:13)],max)? > >?d2<-res3new[res3new[,3]<0.01 & res3new[,4]<0.01,]? > > >m1<- 3 #from d2 >maxN<- 9 >n1<- 2 #from d2 > >In the example that you provided: >?(m1+2):(maxN-(n1+2)) >#[1] 5? >?(n1+2):(maxN-5) >#[1] 4? >#Suppose >?x1<- 4? >?y1<- 2? >?x1:(x1+5-m1) >#[1] 4 5 6 >?y1:(y1+4-n1) >#[1] 2 3 4 > >?datnew<-expand.grid(5,4,4:6,2:4) >?colnames(datnew)<- c("m","n","x","y") >datnew<-within(datnew,{p1<- x/m;p2<-y/n}) >res<-cbind(datnew,d2[rep(1:nrow(d2),nrow(datnew)),]) >?row.names(res)<- 1:nrow(res) >?res ># ?m n x y ? p2 ?p1 m1 n1 cterm1_P1L cterm1_P0H >#1 5 4 4 2 0.50 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#2 5 4 5 2 0.50 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#3 5 4 6 2 0.50 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#4 5 4 4 3 0.75 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#5 5 4 5 3 0.75 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#6 5 4 6 3 0.75 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#7 5 4 4 4 1.00 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#8 5 4 5 4 1.00 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#9 5 4 6 4 1.00 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 > >A.K. > > > > > >----- Original Message ----- >From: Zjoanna <Zjoanna2013 at gmail.com> >To: r-help at r-project.org >Cc: > >Sent: Sunday, February 10, 2013 6:04 PM >Subject: Re: [R] cumulative sum by group and under some criteria > > >Hi, >How to expand or loop for one variable n based on another variable? for >example, I want to add m (from m1 to maxN- n1-2) and for each m, I want to >add n (n1+2 to maxN-m), and similarly add x and y, then I need to do some >calculations. > >d3<-data.frame(d2) >? ? for (m in (m1+2):(maxN-(n1+2)){ >? ? ? ?for (n in (n1+2):(maxN-m)){ >? ? ? ? ? ? ?for (x in x1:(x1+m-m1)){ >? ? ? ? ? ? ? ? ? for (y in y1:(y1+n-n1)){ >? ? ? ? ? ? ? ? ? ? ? ?p1<- x/m >? ? ? ? ? ? ? ? ? ? ? ?p2<- y/n >}}}} > >On Thu, Feb 7, 2013 at 12:16 AM, arun kirshna [via R] < >ml-node+s789695n4657773h74 at n4.nabble.com> wrote: > >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=0>> >> >> To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=1>> > >> >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=2>> > >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=3>> >> >> >To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=4>> > >> >> >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]<http://user/SendEmail.jtp?type=node&node=4657773&i=5>> > >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=6>> >> >> >>To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=7>> > >> >> >>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=8>> > >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=9>"; >> <[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=10>> >> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=11> >>? >>>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=12>"; >> <[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=13>> >> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=14> >>? >>>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=15>> >> >> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=16> > >> >>>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] <http://user/SendEmail.jtp?type=node&node=4657773&i=17>> > >> 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] <http://user/SendEmail.jtp?type=node&node=4657773&i=18>mailing list > >> >>>https://stat.ethz.ch/mailman/listinfo/r-help >> >>>PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > >> >>>and provide commented, minimal, self-contained, reproducible code. >> >>> >> >>> >> >>>______________________________________________ >> >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=19>mailing list > >> >>>https://stat.ethz.ch/mailman/listinfo/r-help >> >>>PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<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] <http://user/SendEmail.jtp?type=node&node=4657773&i=20>mailing list > >> >>>https://stat.ethz.ch/mailman/listinfo/r-help >> >>>PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<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] <http://user/SendEmail.jtp?type=node&node=4657773&i=21>mailing list > >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<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-tp4657074p4657773.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-tp4657074p4658133.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. > >?? ?
Hi, "" Thanks. I tried this code below, why does expanded dataset 'res1' has m1=3 and n1=3 , dataset 'd3' doesn't have m1=3, n1=3. ? d3<-structure(list(m1 = c(2, 3, 2), n1 = c(2, 2, 3), cterm1_P0L = c(0.9025, 0.857375, 0.9025), cterm1_P1L = c(0.64, 0.512, 0.64), cterm1_P0H = c(0.9025, 0.9025, 0.857375), cterm1_P1H = c(0.64, 0.64, 0.512)), .Names = c("m1", "n1", "cterm1_P0L", "cterm1_P1L", "cterm1_P0H", "cterm1_P1H"), row.names = c(NA, 3L), class = "data.frame") d3 "" d3 #? m1 n1 cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H #1? 2? 2?? 0.902500????? 0.640?? 0.902500????? 0.640 #2? 3? 2?? 0.857375????? 0.512?? 0.902500????? 0.640 #3? 2? 3?? 0.902500????? 0.640?? 0.857375????? 0.512 Here, there is no row with m1=3 and n1=3.? d2<- data.frame()?? #this was part of your original code.? In this, m1 is expanding on each value of n1 for (m1 in 2:3) { ??? for (n1 in 2:3) { ??????? for (x1 in 0:(m1-1)) { ??????????? for (y1 in 0:(n1-1)) { ??? ??? for (m in (m1+2): (7-n1)){ ???? ??? ??? ? for (n in (n1+2):(9-m)){ ? ??? ??? ???? for (x in x1:(x1+m-m1)){ ??? ??? ???? for(y in y1:(y1+n-n1)){??? ? ?d2<- rbind(d2,c(m1,n1,x1,y1,m,n,x,y)) ?}}}}}}}} colnames(d2)<-c("m1","n1","x1","y1","m","n","x","y")?? #Here the combination is there ?expand.grid(2:3,2:3) #? Var1 Var2 #1??? 2??? 2 #2??? 3??? 2 #3??? 2??? 3 #4??? 3??? 3 res1<-do.call(rbind,lapply(unique(d3$m1),function(m1) ?do.call(rbind,lapply(unique(d3$n1),function(n1) ?do.call(rbind,lapply(0:(m1-1),function(x1) ?do.call(rbind,lapply(0:(n1-1),function(y1) ?do.call(rbind,lapply((m1+2):(7-n1),function(m) ?do.call(rbind,lapply((n1+2):(9-m),function(n) ?do.call(rbind,lapply(x1:(x1+m-m1), function(x) ?do.call(rbind,lapply(y1:(y1+n-n1), function(y) ?expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) ?names(res1)<- c("m1","n1","x1","y1","m","n","x","y") ?attr(res1,"out.attrs")<-NULL res1[]<- sapply(d2,as.integer)? ?res1 #here too identical(d2,res1) #[1] TRUE library(plyr) res2<- join(res1,d3,by=c("m1","n1"),type="full")? #combination with m1=3, n1=3 didn't exist in d3, so that rows are left as missing or NA ?tail(res2,3) ?# ? m1 n1 x1 y1 m n x y cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H #427? 3? 3? 2? 2 4 5 3 2???????? NA???????? NA???????? NA???????? NA #428? 3? 3? 2? 2 4 5 3 3???????? NA???????? NA???????? NA???????? NA #429? 3? 3? 2? 2 4 5 3 4???????? NA???????? NA???????? NA???????? NA I hope it helps. A.K. ______________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Saturday, February 16, 2013 8:46 PM Subject: Re: [R] cumulative sum by group and under some criteria Hi, What I need is to expand each row by adding several columns and . Let me restate the question. I have a dataset d, I want to expand it to d2 showed below. d<-data.frame() for (m1 in 2:3) { ??? for (n1 in 2:2) { ??????? for (x1 in 0:(m1-1)) { ??????????? for (y1 in 0:(n1-1)) { d<-rbind(d,c(m1,n1,x1,y1)) } } }} colnames(d)<-c("m1","n1","x1","y1") d ?? m1 n1 x1 y1 1?? 2? 2? 0? 0 2?? 2? 2? 0? 1 3?? 2? 2? 1? 0 4?? 2? 2? 1? 1 5?? 3? 2? 0? 0 6?? 3? 2? 0? 1 7?? 3? 2? 1? 0 8?? 3? 2? 1? 1 9?? 3? 2? 2? 0 10? 3? 2? 2? 1 I want to expand it as follows: for (m in (m1+2): (7-n1){ ??? for (n in (n1+2):(9-m){ ??????? for (x in x1:(x1+m-m1){????????? }}} so for the first row, ?? m1 n1 x1 y1 1?? 2? 2? 0? 0 it should be expanded as ?? m1 n1 x1 y1? m? n? x ??? 2? 2? 0? 0? 4? 4? 0 ??? 2? 2? 0? 0? 4? 4? 1 ??? 2? 2? 0? 0? 4? 4? 2 ??? 2? 2? 0? 0? 4? 5? 0 ??? 2? 2? 0? 0? 4? 5? 1 ??? 2? 2? 0? 0? 4? 5? 2 On Tue, Feb 12, 2013 at 8:19 PM, arun <smartpink111 at yahoo.com> wrote: Hi,> >Saw your reply again in Nabble. ?I thought I sent you the solution previously. > > >res3new<- ?aggregate(.~m1+n1,data=res2[,c(1:2,12:13)],max)? > >?d2<-res3new[res3new[,3]<0.01 & res3new[,4]<0.01,]? > > >m1<- 3 #from d2 >maxN<- 9 >n1<- 2 #from d2 > >In the example that you provided: >?(m1+2):(maxN-(n1+2)) >#[1] 5? >?(n1+2):(maxN-5) >#[1] 4? >#Suppose >?x1<- 4? >?y1<- 2? >?x1:(x1+5-m1) >#[1] 4 5 6 >?y1:(y1+4-n1) >#[1] 2 3 4 > >?datnew<-expand.grid(5,4,4:6,2:4) >?colnames(datnew)<- c("m","n","x","y") >datnew<-within(datnew,{p1<- x/m;p2<-y/n}) >res<-cbind(datnew,d2[rep(1:nrow(d2),nrow(datnew)),]) >?row.names(res)<- 1:nrow(res) >?res ># ?m n x y ? p2 ?p1 m1 n1 cterm1_P1L cterm1_P0H >#1 5 4 4 2 0.50 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#2 5 4 5 2 0.50 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#3 5 4 6 2 0.50 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#4 5 4 4 3 0.75 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#5 5 4 5 3 0.75 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#6 5 4 6 3 0.75 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#7 5 4 4 4 1.00 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#8 5 4 5 4 1.00 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#9 5 4 6 4 1.00 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 > >A.K. > > > > > >----- Original Message ----- >From: Zjoanna <Zjoanna2013 at gmail.com> >To: r-help at r-project.org >Cc: > >Sent: Sunday, February 10, 2013 6:04 PM >Subject: Re: [R] cumulative sum by group and under some criteria > > >Hi, >How to expand or loop for one variable n based on another variable? for >example, I want to add m (from m1 to maxN- n1-2) and for each m, I want to >add n (n1+2 to maxN-m), and similarly add x and y, then I need to do some >calculations. > >d3<-data.frame(d2) >? ? for (m in (m1+2):(maxN-(n1+2)){ >? ? ? ?for (n in (n1+2):(maxN-m)){ >? ? ? ? ? ? ?for (x in x1:(x1+m-m1)){ >? ? ? ? ? ? ? ? ? for (y in y1:(y1+n-n1)){ >? ? ? ? ? ? ? ? ? ? ? ?p1<- x/m >? ? ? ? ? ? ? ? ? ? ? ?p2<- y/n >}}}} > >On Thu, Feb 7, 2013 at 12:16 AM, arun kirshna [via R] < >ml-node+s789695n4657773h74 at n4.nabble.com> wrote: > >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=0>> >> >> To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=1>> > >> >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=2>> > >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=3>> >> >> >To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=4>> > >> >> >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]<http://user/SendEmail.jtp?type=node&node=4657773&i=5>> > >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=6>> >> >> >>To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=7>> > >> >> >>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=8>> > >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=9>"; >> <[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=10>> >> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=11> >>? >>>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=12>"; >> <[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=13>> >> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=14> >>? >>>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=15>> >> >> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=16> > >> >>>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] <http://user/SendEmail.jtp?type=node&node=4657773&i=17>> > >> 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] <http://user/SendEmail.jtp?type=node&node=4657773&i=18>mailing list > >> >>>https://stat.ethz.ch/mailman/listinfo/r-help >> >>>PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > >> >>>and provide commented, minimal, self-contained, reproducible code. >> >>> >> >>> >> >>>______________________________________________ >> >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=19>mailing list > >> >>>https://stat.ethz.ch/mailman/listinfo/r-help >> >>>PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<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] <http://user/SendEmail.jtp?type=node&node=4657773&i=20>mailing list > >> >>>https://stat.ethz.ch/mailman/listinfo/r-help >> >>>PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<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] <http://user/SendEmail.jtp?type=node&node=4657773&i=21>mailing list > >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<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-tp4657074p4657773.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-tp4657074p4658133.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. > >?? ?
Hello, The expansion was based on the unique values of m1 and n1 in dataset d3.? I guess that is the way it works for expansion. I am not sure what kind of results you are expecting.? Even the code that you provided will also give the combination of m1=3 and n1=3.? As I mentioned in the earlier reply, if you don't want the combination of m1=3 and n1=3 in the expanded dataset, use type="inner" in ?join(). library(plyr) res2<- join(res1,d3,by=c("m1","n1"),type="inner") A.K. ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Tuesday, February 19, 2013 10:42 AM Subject: Re: [R] cumulative sum by group and under some criteria Thanks. But I thougth the expanded dataset 'res1' should not have combination of m1=3 and n1=3 because it is based on dataset 'd3' which doesn't have m1=3 and n1=3, right?>>In the example that you provided: >?(m1+2):(maxN-(n1+2)) >#[1] 5? >?(n1+2):(maxN-5) >#[1] 4? >#Suppose >?x1<- 4? >?y1<- 2? >?x1:(x1+5-m1) >#[1] 4 5 6 >?y1:(y1+4-n1) >#[1] 2 3 4 > >?datnew<-expand.grid(5,4,4:6,2:4) >?colnames(datnew)<- c("m","n","x","y") >datnew<-within(datnew,{p1<- x/m;p2<-y/n}) >res<-cbind(datnew,d2[rep(1:nrow(d2),nrow(datnew)),]) >?row.names(res)<- 1:nrow(res) >?res ># ?m n x y ? p2 ?p1 m1 n1 cterm1_P1L cterm1_P0H >#1 5 4 4 2 0.50 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#2 5 4 5 2 0.50 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#3 5 4 6 2 0.50 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#4 5 4 4 3 0.75 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#5 5 4 5 3 0.75 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#6 5 4 6 3 0.75 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#7 5 4 4 4 1.00 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#8 5 4 5 4 1.00 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >#9 5 4 6 4 1.00 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 > >A.K. > > > > > >----- Original Message ----- >From: Zjoanna <Zjoanna2013 at gmail.com> >To: r-help at r-project.org >Cc: > >Sent: Sunday, February 10, 2013 6:04 PM >Subject: Re: [R] cumulative sum by group and under some criteria > > >Hi, >How to expand or loop for one variable n based on another variable? for >example, I want to add m (from m1 to maxN- n1-2) and for each m, I want to >add n (n1+2 to maxN-m), and similarly add x and y, then I need to do some >calculations. > >d3<-data.frame(d2) >? ? for (m in (m1+2):(maxN-(n1+2)){ >? ? ? ?for (n in (n1+2):(maxN-m)){ >? ? ? ? ? ? ?for (x in x1:(x1+m-m1)){ >? ? ? ? ? ? ? ? ? for (y in y1:(y1+n-n1)){ >? ? ? ? ? ? ? ? ? ? ? ?p1<- x/m >? ? ? ? ? ? ? ? ? ? ? ?p2<- y/n >}}}} > >On Thu, Feb 7, 2013 at 12:16 AM, arun kirshna [via R] < >ml-node+s789695n4657773h74 at n4.nabble.com> wrote: > >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=0>> >> >> To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=1>> > >> >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=2>> > >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=3>> >> >> >To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=4>> > >> >> >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]<http://user/SendEmail.jtp?type=node&node=4657773&i=5>> > >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=6>> >> >> >>To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=7>> > >> >> >>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=8>> > >> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=9>";; >> <[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=10>> >> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=11> >>? >>>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=12>";; >> <[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=13>> >> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=14> >>? >>>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=15>> >> >> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=16> > >> >>>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] <http://user/SendEmail.jtp?type=node&node=4657773&i=17>> > >> 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] <http://user/SendEmail.jtp?type=node&node=4657773&i=18>mailing list > >> >>>https://stat.ethz.ch/mailman/listinfo/r-help >> >>>PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > >> >>>and provide commented, minimal, self-contained, reproducible code. >> >>> >> >>> >> >>>______________________________________________ >> >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=19>mailing list > >> >>>https://stat.ethz.ch/mailman/listinfo/r-help >> >>>PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<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] <http://user/SendEmail.jtp?type=node&node=4657773&i=20>mailing list > >> >>>https://stat.ethz.ch/mailman/listinfo/r-help >> >>>PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<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] <http://user/SendEmail.jtp?type=node&node=4657773&i=21>mailing list > >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<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-tp4657074p4657773.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-tp4657074p4658133.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. > >????
Hi,Another thing you could do will be to use ?paste() #for example. #d3 dataset ?paste(d3$m1,d3$n1) #[1] "2 2" "3 2" "2 3" #then you use that instead of unique(d3$m1), unique(d3$n1) in the loop. I didn't try it.? But, that is one possibility. You still didn't show me the results you expected in the expansion.? So, I am not sure about how it will look like. A.K. ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Tuesday, February 19, 2013 11:43 AM Subject: Re: [R] cumulative sum by group and under some criteria Thanks. I can get the data I expected (get rid of the m1=3, n1=3) using the join and 'inner' code, but just curious about the way to expand the data. There should be a way to expand the data based on?each row (combination of the variables), unique(d3$m1 & d3$n1) ?. or is there a way to use 'data.frame' and 'for' loop to expand directly from the data? like res1<-data.frame (d3) for () {.... On Tue, Feb 19, 2013 at 9:55 AM, arun <smartpink111 at yahoo.com> wrote: If you can provide me the output that you expect with all the rows of the combination in the res2, I can take a look.>? > > > > > >________________________________ > >From: Joanna Zhang <zjoanna2013 at gmail.com> >To: arun <smartpink111 at yahoo.com> > >Sent: Tuesday, February 19, 2013 10:42 AM > >Subject: Re: [R] cumulative sum by group and under some criteria > > >Thanks. But I thougth the expanded dataset 'res1' should not have combination of m1=3 and n1=3 because it is based on dataset 'd3' which doesn't have m1=3 and n1=3, right?> >>In the example that you provided: >>?(m1+2):(maxN-(n1+2)) >>#[1] 5? >>?(n1+2):(maxN-5) >>#[1] 4? >>#Suppose >>?x1<- 4? >>?y1<- 2? >>?x1:(x1+5-m1) >>#[1] 4 5 6 >>?y1:(y1+4-n1) >>#[1] 2 3 4 >> >>?datnew<-expand.grid(5,4,4:6,2:4) >>?colnames(datnew)<- c("m","n","x","y") >>datnew<-within(datnew,{p1<- x/m;p2<-y/n}) >>res<-cbind(datnew,d2[rep(1:nrow(d2),nrow(datnew)),]) >>?row.names(res)<- 1:nrow(res) >>?res >># ?m n x y ? p2 ?p1 m1 n1 cterm1_P1L cterm1_P0H >>#1 5 4 4 2 0.50 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#2 5 4 5 2 0.50 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#3 5 4 6 2 0.50 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#4 5 4 4 3 0.75 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#5 5 4 5 3 0.75 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#6 5 4 6 3 0.75 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#7 5 4 4 4 1.00 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#8 5 4 5 4 1.00 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#9 5 4 6 4 1.00 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >> >>A.K. >> >> >> >> >> >>----- Original Message ----- >>From: Zjoanna <Zjoanna2013 at gmail.com> >>To: r-help at r-project.org >>Cc: >> >>Sent: Sunday, February 10, 2013 6:04 PM >>Subject: Re: [R] cumulative sum by group and under some criteria >> >> >>Hi, >>How to expand or loop for one variable n based on another variable? for >>example, I want to add m (from m1 to maxN- n1-2) and for each m, I want to >>add n (n1+2 to maxN-m), and similarly add x and y, then I need to do some >>calculations. >> >>d3<-data.frame(d2) >>? ? for (m in (m1+2):(maxN-(n1+2)){ >>? ? ? ?for (n in (n1+2):(maxN-m)){ >>? ? ? ? ? ? ?for (x in x1:(x1+m-m1)){ >>? ? ? ? ? ? ? ? ? for (y in y1:(y1+n-n1)){ >>? ? ? ? ? ? ? ? ? ? ? ?p1<- x/m >>? ? ? ? ? ? ? ? ? ? ? ?p2<- y/n >>}}}} >> >>On Thu, Feb 7, 2013 at 12:16 AM, arun kirshna [via R] < >>ml-node+s789695n4657773h74 at n4.nabble.com> wrote: >> >>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=0>> >>> >>> To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=1>> >> >>> >>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=2>> >> >>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=3>> >>> >>> >To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=4>> >> >>> >>> >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]<http://user/SendEmail.jtp?type=node&node=4657773&i=5>> >> >>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=6>> >>> >>> >>To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=7>> >> >>> >>> >>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=8>> >> >>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=9>";;; >>> <[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=10>> >>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=11> >>>? >>>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=12>";;; >>> <[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=13>> >>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=14> >>>? >>>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=15>> >>> >>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=16> >> >>> >>>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] <http://user/SendEmail.jtp?type=node&node=4657773&i=17>> >> >>> 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] <http://user/SendEmail.jtp?type=node&node=4657773&i=18>mailing list >> >>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >> >>> >>>and provide commented, minimal, self-contained, reproducible code. >>> >>> >>> >>> >>> >>>______________________________________________ >>> >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=19>mailing list >> >>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html<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] <http://user/SendEmail.jtp?type=node&node=4657773&i=20>mailing list >> >>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html<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] <http://user/SendEmail.jtp?type=node&node=4657773&i=21>mailing list >> >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html<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-tp4657074p4657773.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-tp4657074p4658133.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. >> >>???? >???
Hi, Try this: res1<- do.call(rbind,lapply(paste(d3$m1,d3$n1),function(m1) do.call(rbind,lapply(0:(as.numeric(substr(m1,1,1))-1),function(x1) do.call(rbind,lapply(0:(as.numeric(substr(m1,3,3))-1),function(y1) do.call(rbind,lapply((as.numeric(substr(m1,1,1))+2):(7-as.numeric(substr(m1,3,3))),function(m) do.call(rbind,lapply((as.numeric(substr(m1,3,3))+2):(9-m),function(n) ?do.call(rbind,lapply(x1:(x1+m-as.numeric(substr(m1,1,1))), function(x) ?do.call(rbind,lapply(y1:(y1+n-as.numeric(substr(m1,3,3))), function(y) ?expand.grid(m1,x1,y1,m,n,x,y)) ))))))))))))) names(res1)<- c("m1n1","x1","y1","m","n","x","y") ?res1$m1<- NA; res1$n1<- NA res1[,8:9]<-do.call(rbind,lapply(strsplit(as.character(res1$m1n1)," "),as.numeric)) res2<- res1[,c(8:9,3:7)] library(plyr) res2<-join(res1,d3,by=c("m1","n1"),type="full") #Instead of this step, you can paste() the whole row of d3 and make suitable changes to the code above ?tail(res2) ?# ? m1n1 x1 y1 m n x y m1 n1 cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H #235? 2 3? 1? 2 4 5 2 2? 2? 3???? 0.9025?????? 0.64?? 0.857375????? 0.512 #236? 2 3? 1? 2 4 5 2 3? 2? 3???? 0.9025?????? 0.64?? 0.857375????? 0.512 #237? 2 3? 1? 2 4 5 2 4? 2? 3???? 0.9025?????? 0.64?? 0.857375????? 0.512 #238? 2 3? 1? 2 4 5 3 2? 2? 3???? 0.9025?????? 0.64?? 0.857375????? 0.512 #239? 2 3? 1? 2 4 5 3 3? 2? 3???? 0.9025?????? 0.64?? 0.857375????? 0.512 #240? 2 3? 1? 2 4 5 3 4? 2? 3???? 0.9025?????? 0.64?? 0.857375????? 0.512 A.K. ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Tuesday, February 19, 2013 11:43 AM Subject: Re: [R] cumulative sum by group and under some criteria Thanks. I can get the data I expected (get rid of the m1=3, n1=3) using the join and 'inner' code, but just curious about the way to expand the data. There should be a way to expand the data based on?each row (combination of the variables), unique(d3$m1 & d3$n1) ?. or is there a way to use 'data.frame' and 'for' loop to expand directly from the data? like res1<-data.frame (d3) for () {.... On Tue, Feb 19, 2013 at 9:55 AM, arun <smartpink111 at yahoo.com> wrote: If you can provide me the output that you expect with all the rows of the combination in the res2, I can take a look.>? > > > > > >________________________________ > >From: Joanna Zhang <zjoanna2013 at gmail.com> >To: arun <smartpink111 at yahoo.com> > >Sent: Tuesday, February 19, 2013 10:42 AM > >Subject: Re: [R] cumulative sum by group and under some criteria > > >Thanks. But I thougth the expanded dataset 'res1' should not have combination of m1=3 and n1=3 because it is based on dataset 'd3' which doesn't have m1=3 and n1=3, right?> >>In the example that you provided: >>?(m1+2):(maxN-(n1+2)) >>#[1] 5? >>?(n1+2):(maxN-5) >>#[1] 4? >>#Suppose >>?x1<- 4? >>?y1<- 2? >>?x1:(x1+5-m1) >>#[1] 4 5 6 >>?y1:(y1+4-n1) >>#[1] 2 3 4 >> >>?datnew<-expand.grid(5,4,4:6,2:4) >>?colnames(datnew)<- c("m","n","x","y") >>datnew<-within(datnew,{p1<- x/m;p2<-y/n}) >>res<-cbind(datnew,d2[rep(1:nrow(d2),nrow(datnew)),]) >>?row.names(res)<- 1:nrow(res) >>?res >># ?m n x y ? p2 ?p1 m1 n1 cterm1_P1L cterm1_P0H >>#1 5 4 4 2 0.50 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#2 5 4 5 2 0.50 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#3 5 4 6 2 0.50 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#4 5 4 4 3 0.75 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#5 5 4 5 3 0.75 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#6 5 4 6 3 0.75 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#7 5 4 4 4 1.00 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#8 5 4 5 4 1.00 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#9 5 4 6 4 1.00 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >> >>A.K. >> >> >> >> >> >>----- Original Message ----- >>From: Zjoanna <Zjoanna2013 at gmail.com> >>To: r-help at r-project.org >>Cc: >> >>Sent: Sunday, February 10, 2013 6:04 PM >>Subject: Re: [R] cumulative sum by group and under some criteria >> >> >>Hi, >>How to expand or loop for one variable n based on another variable? for >>example, I want to add m (from m1 to maxN- n1-2) and for each m, I want to >>add n (n1+2 to maxN-m), and similarly add x and y, then I need to do some >>calculations. >> >>d3<-data.frame(d2) >>? ? for (m in (m1+2):(maxN-(n1+2)){ >>? ? ? ?for (n in (n1+2):(maxN-m)){ >>? ? ? ? ? ? ?for (x in x1:(x1+m-m1)){ >>? ? ? ? ? ? ? ? ? for (y in y1:(y1+n-n1)){ >>? ? ? ? ? ? ? ? ? ? ? ?p1<- x/m >>? ? ? ? ? ? ? ? ? ? ? ?p2<- y/n >>}}}} >> >>On Thu, Feb 7, 2013 at 12:16 AM, arun kirshna [via R] < >>ml-node+s789695n4657773h74 at n4.nabble.com> wrote: >> >>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=0>> >>> >>> To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=1>> >> >>> >>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=2>> >> >>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=3>> >>> >>> >To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=4>> >> >>> >>> >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]<http://user/SendEmail.jtp?type=node&node=4657773&i=5>> >> >>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=6>> >>> >>> >>To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=7>> >> >>> >>> >>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=8>> >> >>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=9>";;; >>> <[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=10>> >>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=11> >>>? >>>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=12>";;; >>> <[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=13>> >>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=14> >>>? >>>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=15>> >>> >>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=16> >> >>> >>>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] <http://user/SendEmail.jtp?type=node&node=4657773&i=17>> >> >>> 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] <http://user/SendEmail.jtp?type=node&node=4657773&i=18>mailing list >> >>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >> >>> >>>and provide commented, minimal, self-contained, reproducible code. >>> >>> >>> >>> >>> >>>______________________________________________ >>> >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=19>mailing list >> >>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html<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] <http://user/SendEmail.jtp?type=node&node=4657773&i=20>mailing list >> >>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html<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] <http://user/SendEmail.jtp?type=node&node=4657773&i=21>mailing list >> >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html<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-tp4657074p4657773.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-tp4657074p4658133.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. >> >>???? >???
Hi, ""suppose that I have a dataset 'd' ?? m1? n1??? A???????????? B???? C???????? D 1? 2? 2?? 0.902500??? 0.640?? 0.9025??? 0.64 2? 3? 2?? 0.857375??? 0.512?? 0.9025??? 0.64 I want to add? x1 (from 0 to m1), y1(from 0 to n1), m (range from m1+2 to 7-n1), n(from n1+2 to 9-m), x (x1 to x1+m-m1), y(y1 to y1+n-n1), expanding to another dataset 'd2' based on each row (combination of m1 and n1)"" Try: ?d<-read.table(text=" m1? n1??? A???????????? B???? C???????? D 1? 2? 2?? 0.902500??? 0.640?? 0.9025??? 0.64 2? 3? 2?? 0.857375??? 0.512?? 0.9025??? 0.64 ",sep="",header=TRUE) vec1<- paste(d[,1],d[,2],d[,3],d[,4],d[,5],d[,6]) res1<- do.call(rbind,lapply(vec1,function(m1) do.call(rbind,lapply(0:(as.numeric(substr(m1,1,1))),function(x1) do.call(rbind,lapply(0:(as.numeric(substr(m1,3,3))),function(y1) do.call(rbind,lapply((as.numeric(substr(m1,1,1))+2):(7-as.numeric(substr(m1,3,3))),function(m) do.call(rbind,lapply((as.numeric(substr(m1,3,3))+2):(9-m),function(n) ?do.call(rbind,lapply(x1:(x1+m-as.numeric(substr(m1,1,1))), function(x) ?do.call(rbind,lapply(y1:(y1+n-as.numeric(substr(m1,3,3))), function(y) ?expand.grid(m1,x1,y1,m,n,x,y)) ))))))))))))) names(res1)<- c("group","x1","y1","m","n","x","y") ?res1$m1<- NA; res1$n1<- NA; res1$A<- NA; res1$B<- NA; res1$C<- NA;res1$D <- NA res1[,8:13]<-do.call(rbind,lapply(strsplit(as.character(res1$group)," "),as.numeric)) res2<- res1[,c(8:9,2:7,10:13)] ?head(res2) #? m1 n1 x1 y1 m n x y????? A??? B????? C??? D #1? 2? 2? 0? 0 4 4 0 0 0.9025 0.64 0.9025 0.64 #2? 2? 2? 0? 0 4 4 0 1 0.9025 0.64 0.9025 0.64 #3? 2? 2? 0? 0 4 4 0 2 0.9025 0.64 0.9025 0.64 #4? 2? 2? 0? 0 4 4 1 0 0.9025 0.64 0.9025 0.64 #5? 2? 2? 0? 0 4 4 1 1 0.9025 0.64 0.9025 0.64 #6? 2? 2? 0? 0 4 4 1 2 0.9025 0.64 0.9025 0.64 ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Tuesday, February 19, 2013 11:43 AM Subject: Re: [R] cumulative sum by group and under some criteria Thanks. I can get the data I expected (get rid of the m1=3, n1=3) using the join and 'inner' code, but just curious about the way to expand the data. There should be a way to expand the data based on?each row (combination of the variables), unique(d3$m1 & d3$n1) ?. or is there a way to use 'data.frame' and 'for' loop to expand directly from the data? like res1<-data.frame (d3) for () {.... On Tue, Feb 19, 2013 at 9:55 AM, arun <smartpink111 at yahoo.com> wrote: If you can provide me the output that you expect with all the rows of the combination in the res2, I can take a look.>? > > > > > >________________________________ > >From: Joanna Zhang <zjoanna2013 at gmail.com> >To: arun <smartpink111 at yahoo.com> > >Sent: Tuesday, February 19, 2013 10:42 AM > >Subject: Re: [R] cumulative sum by group and under some criteria > > >Thanks. But I thougth the expanded dataset 'res1' should not have combination of m1=3 and n1=3 because it is based on dataset 'd3' which doesn't have m1=3 and n1=3, right?> >>In the example that you provided: >>?(m1+2):(maxN-(n1+2)) >>#[1] 5? >>?(n1+2):(maxN-5) >>#[1] 4? >>#Suppose >>?x1<- 4? >>?y1<- 2? >>?x1:(x1+5-m1) >>#[1] 4 5 6 >>?y1:(y1+4-n1) >>#[1] 2 3 4 >> >>?datnew<-expand.grid(5,4,4:6,2:4) >>?colnames(datnew)<- c("m","n","x","y") >>datnew<-within(datnew,{p1<- x/m;p2<-y/n}) >>res<-cbind(datnew,d2[rep(1:nrow(d2),nrow(datnew)),]) >>?row.names(res)<- 1:nrow(res) >>?res >># ?m n x y ? p2 ?p1 m1 n1 cterm1_P1L cterm1_P0H >>#1 5 4 4 2 0.50 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#2 5 4 5 2 0.50 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#3 5 4 6 2 0.50 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#4 5 4 4 3 0.75 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#5 5 4 5 3 0.75 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#6 5 4 6 3 0.75 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#7 5 4 4 4 1.00 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#8 5 4 5 4 1.00 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>#9 5 4 6 4 1.00 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >> >>A.K. >> >> >> >> >> >>----- Original Message ----- >>From: Zjoanna <Zjoanna2013 at gmail.com> >>To: r-help at r-project.org >>Cc: >> >>Sent: Sunday, February 10, 2013 6:04 PM >>Subject: Re: [R] cumulative sum by group and under some criteria >> >> >>Hi, >>How to expand or loop for one variable n based on another variable? for >>example, I want to add m (from m1 to maxN- n1-2) and for each m, I want to >>add n (n1+2 to maxN-m), and similarly add x and y, then I need to do some >>calculations. >> >>d3<-data.frame(d2) >>? ? for (m in (m1+2):(maxN-(n1+2)){ >>? ? ? ?for (n in (n1+2):(maxN-m)){ >>? ? ? ? ? ? ?for (x in x1:(x1+m-m1)){ >>? ? ? ? ? ? ? ? ? for (y in y1:(y1+n-n1)){ >>? ? ? ? ? ? ? ? ? ? ? ?p1<- x/m >>? ? ? ? ? ? ? ? ? ? ? ?p2<- y/n >>}}}} >> >>On Thu, Feb 7, 2013 at 12:16 AM, arun kirshna [via R] < >>ml-node+s789695n4657773h74 at n4.nabble.com> wrote: >> >>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=0>> >>> >>> To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=1>> >> >>> >>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=2>> >> >>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=3>> >>> >>> >To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=4>> >> >>> >>> >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]<http://user/SendEmail.jtp?type=node&node=4657773&i=5>> >> >>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=6>> >>> >>> >>To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=7>> >> >>> >>> >>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=8>> >> >>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=9>";;; >>> <[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=10>> >>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=11> >>>? >>>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=12>";;; >>> <[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=13>> >>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=14> >>>? >>>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=15>> >>> >>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=16> >> >>> >>>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] <http://user/SendEmail.jtp?type=node&node=4657773&i=17>> >> >>> 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] <http://user/SendEmail.jtp?type=node&node=4657773&i=18>mailing list >> >>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >> >>> >>>and provide commented, minimal, self-contained, reproducible code. >>> >>> >>> >>> >>> >>>______________________________________________ >>> >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=19>mailing list >> >>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html<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] <http://user/SendEmail.jtp?type=node&node=4657773&i=20>mailing list >> >>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html<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] <http://user/SendEmail.jtp?type=node&node=4657773&i=21>mailing list >> >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html<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-tp4657074p4657773.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-tp4657074p4658133.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. >> >>???? >???
Hi, d3<-structure(list(m1 = c(2, 3, 2), n1 = c(2, 2, 3), cterm1_P0L = c(0.9025, 0.857375, 0.9025), cterm1_P1L = c(0.64, 0.512, 0.64), cterm1_P0H = c(0.9025, 0.9025, 0.857375), cterm1_P1H = c(0.64, 0.64, 0.512)), .Names = c("m1", "n1", "cterm1_P0L", "cterm1_P1L", "cterm1_P0H", "cterm1_P1H"), row.names = c(NA, 3L), class = "data.frame") d2<- data.frame() for (m1 in 2:3) { ??? for (n1 in 2:3) { ??????? for (x1 in 0:(m1-1)) { ??????????? for (y1 in 0:(n1-1)) { ??????? for (m in (m1+2): (7-n1)){ ?????????????? for (n in (n1+2):(9-m)){ ?????????????? for (x in x1:(x1+m-m1)){ ???????????? for(y in y1:(y1+n-n1)){???? ?d2<- rbind(d2,c(m1,n1,x1,y1,m,n,x,y)) ?}}}}}}}} colnames(d2)<-c("m1","n1","x1","y1","m","n","x","y")? ?#or ? res1<-do.call(rbind,lapply(unique(d3$m1),function(m1) ?do.call(rbind,lapply(unique(d3$n1),function(n1) ?do.call(rbind,lapply(0:(m1-1),function(x1) ?do.call(rbind,lapply(0:(n1-1),function(y1) ?do.call(rbind,lapply((m1+2):(7-n1),function(m) ?do.call(rbind,lapply((n1+2):(9-m),function(n) ?do.call(rbind,lapply(x1:(x1+m-m1), function(x) ?do.call(rbind,lapply(y1:(y1+n-n1), function(y) ?expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) ?names(res1)<- c("m1","n1","x1","y1","m","n","x","y") ?attr(res1,"out.attrs")<-NULL res1[]<- sapply(res1,as.integer) library(plyr) res2<- join(res1,d3,by=c("m1","n1"),type="inner") #Assuming that these are the values you used: p0L<-0.05 p0H<-0.05 p1L<-0.20 p1H<-0.20 res2<- within(res2,{p1<- x/m; p2<- y/n;term2_p0<-dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, log=FALSE);term2_p1<- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, log=FALSE);Pm2<-rbeta(240, 0.2+x, 0.8+m-x);Pn2<-rbeta(240, 0.2+y, 0.8+n-y)}) Fm2<- ecdf(res2$Pm2) Fn2<- ecdf(res2$Pn2) res3<- within(res2,{Fmm2<-Fm2(p1);Fnn2<- Fn2(p2);R2<- (Fmm2+Fnn2)/2}) #not sure about this step especially the Fm2() or Fn2() res3$Fmm_f2<-apply(res3[,c("R2","Fmm2")],1,min) ?res3$Fnn_f2<-apply(res3[,c("R2","Fnn2")],1,max) res3<- within(res3,{Qm2<- 1-Fmm_f2;Qn2<- 1-Fnn_f2}) head(res3) #? m1 n1 x1 y1 m n x y cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H???????? Pn2 #1? 2? 2? 0? 0 4 4 0 0???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.001084648 #2? 2? 2? 0? 0 4 4 0 1???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.504593909 #3? 2? 2? 0? 0 4 4 0 2???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.541379357 #4? 2? 2? 0? 0 4 4 1 0???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.138947785 #5? 2? 2? 0? 0 4 4 1 1???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.272364957 #6? 2? 2? 0? 0 4 4 1 2???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.761635059 #?????????? Pm2?? term2_p1???? term2_p0?? p2?? p1??????? R2????? Fnn2 Fmm2 #1 1.212348e-05 0.16777216 0.6634204313 0.00 0.00 0.0000000 0.0000000? 0.0 #2 1.007697e-03 0.08388608 0.0698337296 0.25 0.00 0.1791667 0.3583333? 0.0 #3 1.106946e-05 0.01048576 0.0018377297 0.50 0.00 0.3479167 0.6958333? 0.0 # 2.086758e-01 0.08388608 0.0698337296 0.00 0.25 0.2000000 0.0000000? 0.4 #5 2.382179e-01 0.04194304 0.0073509189 0.25 0.25 0.3791667 0.3583333? 0.4 #6 4.494673e-01 0.00524288 0.0001934452 0.50 0.25 0.5479167 0.6958333? 0.4 #???? Fmm_f2??? Fnn_f2?????? Qn2?????? Qm2 #1 0.0000000 0.0000000 1.0000000 1.0000000 #2 0.0000000 0.3583333 0.6416667 1.0000000 #3 0.0000000 0.6958333 0.3041667 1.0000000 #4 0.2000000 0.2000000 0.8000000 0.8000000 #5 0.3791667 0.3791667 0.6208333 0.6208333 #6 0.4000000 0.6958333 0.3041667 0.6000000 A.K. ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Friday, February 22, 2013 11:02 AM Subject: Re: [R] cumulative sum by group and under some criteria Thanks!? Then I need to create new variables based on the res2.? I can't find Fmm_f1, Fnn_f2, R2, Qm2, Qn2 until? running the code several times and the values of Fnn_f2, Fmm_f2 are correct. attach(res2) res2$p1<-x/m res2$p2<-y/n res2$term2_p0 <- dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, log=FALSE) res2$term2_p1 <- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, log=FALSE)?? Pm2<-rbeta(1000, 0.2+x, 0.8+m-x) Fm2<-ecdf(Pm2) res2$Fmm2<-Fm2(x/m)? #not correct, it comes out after running code two times?????????????????????? Pn2<-rbeta(1000, 0.2+y, 0.8+n-y) Fn2<-ecdf(Pn2) res2$Fnn2<-Fn2(y/n)??? res2$R2<-(Fmm2+Fnn2)/2 res2$Fmm_f2<-min(R2,Fmm2)? # not correct res2$Fnn_f2<-max(R2,Fnn2)?????????????????????? res2$Qm2<-(1-Fmm_f2) res2$Qn2<-(1-Fnn_f2)???? detach(res2) res2 head(res2)?? On Tue, Feb 19, 2013 at 4:09 PM, arun <smartpink111 at yahoo.com> wrote: Hi,> >""suppose that I have a dataset 'd' >?? m1? n1??? A???????????? B???? C???????? D >1? 2? 2?? 0.902500??? 0.640?? 0.9025??? 0.64 >2? 3? 2?? 0.857375??? 0.512?? 0.9025??? 0.64 >I want to add? x1 (from 0 to m1), y1(from 0 to n1), m (range from >m1+2 to 7-n1), n(from n1+2 to 9-m), x (x1 to x1+m-m1), y(y1 to y1+n-n1), expanding to another dataset 'd2' based on each row (combination of m1 >and n1)"" > > >Try: > > >?d<-read.table(text=" > >m1? n1??? A???????????? B???? C???????? D >1? 2? 2?? 0.902500??? 0.640?? 0.9025??? 0.64 >2? 3? 2?? 0.857375??? 0.512?? 0.9025??? 0.64 >",sep="",header=TRUE) > >vec1<- paste(d[,1],d[,2],d[,3],d[,4],d[,5],d[,6]) >res1<- do.call(rbind,lapply(vec1,function(m1) do.call(rbind,lapply(0:(as.numeric(substr(m1,1,1))),function(x1) do.call(rbind,lapply(0:(as.numeric(substr(m1,3,3))),function(y1) do.call(rbind,lapply((as.numeric(substr(m1,1,1))+2):(7-as.numeric(substr(m1,3,3))),function(m) do.call(rbind,lapply((as.numeric(substr(m1,3,3))+2):(9-m),function(n) > >?do.call(rbind,lapply(x1:(x1+m-as.numeric(substr(m1,1,1))), function(x) >?do.call(rbind,lapply(y1:(y1+n-as.numeric(substr(m1,3,3))), function(y) >?expand.grid(m1,x1,y1,m,n,x,y)) ))))))))))))) >names(res1)<- c("group","x1","y1","m","n","x","y")>?res1$m1<- NA; res1$n1<- NA; res1$A<- NA; res1$B<- NA; res1$C<- NA;res1$D <- NA >res1[,8:13]<-do.call(rbind,lapply(strsplit(as.character(res1$group)," "),as.numeric)) >res2<- res1[,c(8:9,2:7,10:13)] > > >?head(res2) >#? m1 n1 x1 y1 m n x y????? A??? B????? C??? D >#1? 2? 2? 0? 0 4 4 0 0 0.9025 0.64 0.9025 0.64 >#2? 2? 2? 0? 0 4 4 0 1 0.9025 0.64 0.9025 0.64 >#3? 2? 2? 0? 0 4 4 0 2 0.9025 0.64 0.9025 0.64 >#4? 2? 2? 0? 0 4 4 1 0 0.9025 0.64 0.9025 0.64 >#5? 2? 2? 0? 0 4 4 1 1 0.9025 0.64 0.9025 0.64 >#6? 2? 2? 0? 0 4 4 1 2 0.9025 0.64 0.9025 0.64 > > > > > > >________________________________ >From: Joanna Zhang <zjoanna2013 at gmail.com> >To: arun <smartpink111 at yahoo.com> >Sent: Tuesday, February 19, 2013 11:43 AM > >Subject: Re: [R] cumulative sum by group and under some criteria > > >Thanks. I can get the data I expected (get rid of the m1=3, n1=3) using the join and 'inner' code, but just curious about the way to expand the data. There should be a way to expand the data based on?each row (combination of the variables), unique(d3$m1 & d3$n1) ?. > >or is there a way to use 'data.frame' and 'for' loop to expand directly from the data? like res1<-data.frame (d3) for () {.... > > >On Tue, Feb 19, 2013 at 9:55 AM, arun <smartpink111 at yahoo.com> wrote: > >If you can provide me the output that you expect with all the rows of the combination in the res2, I can take a look. >>? >> >> >> >> >> >>________________________________ >> >>From: Joanna Zhang <zjoanna2013 at gmail.com> >>To: arun <smartpink111 at yahoo.com> >> >>Sent: Tuesday, February 19, 2013 10:42 AM >> >>Subject: Re: [R] cumulative sum by group and under some criteria >> >> >>Thanks. But I thougth the expanded dataset 'res1' should not have combination of m1=3 and n1=3 because it is based on dataset 'd3' which doesn't have m1=3 and n1=3, right?> >>>In the example that you provided: >>>?(m1+2):(maxN-(n1+2)) >>>#[1] 5? >>>?(n1+2):(maxN-5) >>>#[1] 4? >>>#Suppose >>>?x1<- 4? >>>?y1<- 2? >>>?x1:(x1+5-m1) >>>#[1] 4 5 6 >>>?y1:(y1+4-n1) >>>#[1] 2 3 4 >>> >>>?datnew<-expand.grid(5,4,4:6,2:4) >>>?colnames(datnew)<- c("m","n","x","y") >>>datnew<-within(datnew,{p1<- x/m;p2<-y/n}) >>>res<-cbind(datnew,d2[rep(1:nrow(d2),nrow(datnew)),]) >>>?row.names(res)<- 1:nrow(res) >>>?res >>># ?m n x y ? p2 ?p1 m1 n1 cterm1_P1L cterm1_P0H >>>#1 5 4 4 2 0.50 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>>#2 5 4 5 2 0.50 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>>#3 5 4 6 2 0.50 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>>#4 5 4 4 3 0.75 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>>#5 5 4 5 3 0.75 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>>#6 5 4 6 3 0.75 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>>#7 5 4 4 4 1.00 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>>#8 5 4 5 4 1.00 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>>#9 5 4 6 4 1.00 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>> >>>A.K. >>> >>> >>> >>> >>> >>>----- Original Message ----- >>>From: Zjoanna <Zjoanna2013 at gmail.com> >>>To: r-help at r-project.org >>>Cc: >>> >>>Sent: Sunday, February 10, 2013 6:04 PM >>>Subject: Re: [R] cumulative sum by group and under some criteria >>> >>> >>>Hi, >>>How to expand or loop for one variable n based on another variable? for >>>example, I want to add m (from m1 to maxN- n1-2) and for each m, I want to >>>add n (n1+2 to maxN-m), and similarly add x and y, then I need to do some >>>calculations. >>> >>>d3<-data.frame(d2) >>>? ? for (m in (m1+2):(maxN-(n1+2)){ >>>? ? ? ?for (n in (n1+2):(maxN-m)){ >>>? ? ? ? ? ? ?for (x in x1:(x1+m-m1)){ >>>? ? ? ? ? ? ? ? ? for (y in y1:(y1+n-n1)){ >>>? ? ? ? ? ? ? ? ? ? ? ?p1<- x/m >>>? ? ? ? ? ? ? ? ? ? ? ?p2<- y/n >>>}}}} >>> >>>On Thu, Feb 7, 2013 at 12:16 AM, arun kirshna [via R] < >>>ml-node+s789695n4657773h74 at n4.nabble.com> wrote: >>> >>>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=0>> >>>> >>>> To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=1>> >>> >>>> >>>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=2>> >>> >>>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=3>> >>>> >>>> >To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=4>> >>> >>>> >>>> >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]<http://user/SendEmail.jtp?type=node&node=4657773&i=5>> >>> >>>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=6>> >>>> >>>> >>To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=7>> >>> >>>> >>>> >>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=8>> >>> >>>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=9>";;;; >>>> <[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=10>> >>>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=11> >>>>? >>>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=12>";;;; >>>> <[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=13>> >>>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=14> >>>>? >>>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=15>> >>>> >>>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=16> >>> >>>> >>>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] <http://user/SendEmail.jtp?type=node&node=4657773&i=17>> >>> >>>> 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] <http://user/SendEmail.jtp?type=node&node=4657773&i=18>mailing list >>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>> >>>PLEASE do read the posting guide >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>> >>>> >>>and provide commented, minimal, self-contained, reproducible code. >>>> >>> >>>> >>> >>>> >>>______________________________________________ >>>> >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=19>mailing list >>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>> >>>PLEASE do read the posting guide >>>> http://www.R-project.org/posting-guide.html<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] <http://user/SendEmail.jtp?type=node&node=4657773&i=20>mailing list >>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>> >>>PLEASE do read the posting guide >>>> http://www.R-project.org/posting-guide.html<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] <http://user/SendEmail.jtp?type=node&node=4657773&i=21>mailing list >>> >>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>> PLEASE do read the posting guide >>>> http://www.R-project.org/posting-guide.html<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-tp4657074p4657773.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-tp4657074p4658133.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. >>> >>>???? >>??? >
Hi, You can also use ?rowMins() or ?rowMaxs() from library(matrixStats) library(plyr) res2<- join(res1,d3,by=c("m1","n1"),type="inner") ? p0L<-0.05 p0H<-0.05 p1L<-0.20 p1H<-0.20 ? res2<- within(res2,{p1<- x/m; p2<- y/n;term2_p0<-dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, log=FALSE);term2_p1<- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, log=FALSE)}) Pm2<-rbeta(1000, 0.2+res2$x, 0.8+res2$m-res2$x) Pn2<-rbeta(1000, 0.2+res2$y, 0.8+res2$n-res2$y) Fm2<- ecdf(Pm2) Fn2<- ecdf(Pn2) library(matrixStats) ?res3<- within(res2,{Fmm2<-Fm2(p1);Fnn2<- Fn2(p2);R2<- (Fmm2+Fnn2)/2;Fmm_f2<-rowMins(cbind(R2,Fmm2));Fnn_f2<-rowMaxs(cbind(R2,Fnn2));Qm2<- 1-Fmm_f2;Qn2<- 1-Fnn_f2})? head(res3) #? m1 n1 x1 y1 m n x y cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H?? term2_p1 #1? 2? 2? 0? 0 4 4 0 0???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.16777216 #2? 2? 2? 0? 0 4 4 0 1???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.08388608 #3? 2? 2? 0? 0 4 4 0 2???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.01048576 #4? 2? 2? 0? 0 4 4 1 0???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.08388608 #5? 2? 2? 0? 0 4 4 1 1???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.04194304 #6? 2? 2? 0? 0 4 4 1 2???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.00524288 #????? term2_p0?? p2?? p1?? Qn2 Qm2 Fnn_f2 Fmm_f2???? R2? Fnn2 Fmm2 #1 0.6634204313 0.00 0.00 1.000 1.0? 0.000??? 0.0 0.0000 0.000? 0.0 #2 0.0698337296 0.25 0.00 0.593 1.0? 0.407??? 0.0 0.2035 0.407? 0.0 #3 0.0018377297 0.50 0.00 0.302 1.0? 0.698??? 0.0 0.3490 0.698? 0.0 #4 0.0698337296 0.00 0.25 0.800 0.8? 0.200??? 0.2 0.2000 0.000? 0.4 #5 0.0073509189 0.25 0.25 0.593 0.6? 0.407??? 0.4 0.4035 0.407? 0.4 # 0.0001934452 0.50 0.25 0.302 0.6? 0.698??? 0.4 0.5490 0.698? 0.4 ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Saturday, February 23, 2013 5:35 PM Subject: Re: [R] cumulative sum by group and under some criteria the row wise min or max of R2, Rmm2 , for example, for the first row, suppose R2 = 0.63 and Fmm2 = 0.56, then Fmm_f2 = 0.63. means draw 1000 samples for each row from a beta distribution. On Sat, Feb 23, 2013 at 3:49 PM, arun <smartpink111 at yahoo.com> wrote: Hi,> >I have a doubt: >When you say that min(R2,Fmm2) or max(R2,Fnn2), do you mean the corresponding row wise min or max. for these two columns or the maximum from the entire two columns. > >Also, why do you need rbeta(1000,...), should it be rbeta(240,..) because if you use the former the nrow from res2 is around 240 and in the formula, you are also using x, m , from res2, which makes no sense to me. >Arun > > > > > > > > >________________________________ >From: Joanna Zhang <zjoanna2013 at gmail.com> >To: arun <smartpink111 at yahoo.com> >Sent: Saturday, February 23, 2013 4:12 PM > >Subject: Re: [R] cumulative sum by group and under some criteria > > >sorry, these are >p0L<-0.05 >p0H<-0.05 >p1L<-0.20 >p1H<-0.20 > > > >On Sat, Feb 23, 2013 at 2:48 PM, arun <smartpink111 at yahoo.com> wrote: > >Hi, >> >>If you give only half the information, it will be difficult to solve. >> >> >>d3<-structure(list(m1 = c(2, 3, 2), n1 = c(2, 2, 3), cterm1_P0L = c(0.9025, >>0.857375, 0.9025), cterm1_P1L = c(0.64, 0.512, 0.64), cterm1_P0H = c(0.9025, >>0.9025, 0.857375), cterm1_P1H = c(0.64, 0.64, 0.512)), .Names = c("m1", >>"n1", "cterm1_P0L", "cterm1_P1L", "cterm1_P0H", "cterm1_P1H"), row.names = c(NA, >>3L), class = "data.frame") >>d2<- data.frame() >> >>for (m1 in 2:3) { >>??? for (n1 in 2:3) { >>??????? for (x1 in 0:(m1-1)) { >>??????????? for (y1 in 0:(n1-1)) { >>??????? for (m in (m1+2): (7-n1)){ >>?????????????? for (n in (n1+2):(9-m)){ >> >>?????????????? for (x in x1:(x1+m-m1)){ >> >>???????????? for(y in y1:(y1+n-n1)){????? >>?d2<- rbind(d2,c(m1,n1,x1,y1,m,n,x,y)) >>?}}}}}}}} >>colnames(d2)<-c("m1","n1","x1","y1","m","n","x","y")?? >> >>? >>? >>res1<-do.call(rbind,lapply(unique(d3$m1),function(m1) >>?do.call(rbind,lapply(unique(d3$n1),function(n1) >>?do.call(rbind,lapply(0:(m1-1),function(x1) >>?do.call(rbind,lapply(0:(n1-1),function(y1) >>?do.call(rbind,lapply((m1+2):(7-n1),function(m) >>?do.call(rbind,lapply((n1+2):(9-m),function(n) >>?do.call(rbind,lapply(x1:(x1+m-m1), function(x) >>?do.call(rbind,lapply(y1:(y1+n-n1), function(y) >>?expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) >>?names(res1)<- c("m1","n1","x1","y1","m","n","x","y") >>?attr(res1,"out.attrs")<-NULL >>res1[]<- sapply(res1,as.integer) >> >>?identical(d2,res1) >>#[1] TRUE >> >>library(plyr) >>res2<- join(res1,d3,by=c("m1","n1"),type="inner") >>res2$p1<- x/m >>?res2$p2<- y/n >> >> >> >>dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, log=FALSE) >>#Error in dbinom(x1, m1, p0L, log = FALSE) : object 'p0L' not found >>A.K. >> >> >> >>________________________________ >> >>From: Joanna Zhang <zjoanna2013 at gmail.com> >>To: arun <smartpink111 at yahoo.com> >>Sent: Friday, February 22, 2013 11:02 AM >> >>Subject: Re: [R] cumulative sum by group and under some criteria >> >> >>Thanks!? Then I need to create new variables based on the res2.? I can't find Fmm_f1, Fnn_f2, R2, Qm2, Qn2 until? running the code several times and the values of Fnn_f2, Fmm_f2 are correct. >> >>attach(res2) >>res2$p1<-x/m >>res2$p2<-y/n >>res2$term2_p0 <- dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, log=FALSE) >>res2$term2_p1 <- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, log=FALSE)?? >>Pm2<-rbeta(1000, 0.2+x, 0.8+m-x) >>Fm2<-ecdf(Pm2) >>res2$Fmm2<-Fm2(x/m)? #not correct, it comes out after running code two times?????????????????????? >>Pn2<-rbeta(1000, 0.2+y, 0.8+n-y) >>Fn2<-ecdf(Pn2) >>res2$Fnn2<-Fn2(y/n)??? >>res2$R2<-(Fmm2+Fnn2)/2 >>res2$Fmm_f2<-min(R2,Fmm2)? # not correct >>res2$Fnn_f2<-max(R2,Fnn2)?????????????????????? >>res2$Qm2<-(1-Fmm_f2) >>res2$Qn2<-(1-Fnn_f2)???? >>detach(res2) >>res2 >>head(res2)?? >> >> >> >>On Tue, Feb 19, 2013 at 4:09 PM, arun <smartpink111 at yahoo.com> wrote: >> >>Hi, >>> >>>""suppose that I have a dataset 'd' >>>?? m1? n1??? A???????????? B???? C???????? D >>>1? 2? 2?? 0.902500??? 0.640?? 0.9025??? 0.64 >>>2? 3? 2?? 0.857375??? 0.512?? 0.9025??? 0.64 >>>I want to add? x1 (from 0 to m1), y1(from 0 to n1), m (range from >>>m1+2 to 7-n1), n(from n1+2 to 9-m), x (x1 to x1+m-m1), y(y1 to y1+n-n1), expanding to another dataset 'd2' based on each row (combination of m1 >>>and n1)"" >>> >>> >>>Try: >>> >>> >>>?d<-read.table(text=" >>> >>>m1? n1??? A???????????? B???? C???????? D >>>1? 2? 2?? 0.902500??? 0.640?? 0.9025??? 0.64 >>>2? 3? 2?? 0.857375??? 0.512?? 0.9025??? 0.64 >>>",sep="",header=TRUE) >>> >>>vec1<- paste(d[,1],d[,2],d[,3],d[,4],d[,5],d[,6]) >>>res1<- do.call(rbind,lapply(vec1,function(m1) do.call(rbind,lapply(0:(as.numeric(substr(m1,1,1))),function(x1) do.call(rbind,lapply(0:(as.numeric(substr(m1,3,3))),function(y1) do.call(rbind,lapply((as.numeric(substr(m1,1,1))+2):(7-as.numeric(substr(m1,3,3))),function(m) do.call(rbind,lapply((as.numeric(substr(m1,3,3))+2):(9-m),function(n) >>> >>>?do.call(rbind,lapply(x1:(x1+m-as.numeric(substr(m1,1,1))), function(x) >>>?do.call(rbind,lapply(y1:(y1+n-as.numeric(substr(m1,3,3))), function(y) >>>?expand.grid(m1,x1,y1,m,n,x,y)) ))))))))))))) >>> >>names(res1)<- c("group","x1","y1","m","n","x","y") >>>?res1$m1<- NA; res1$n1<- NA; res1$A<- NA; res1$B<- NA; res1$C<- NA;res1$D <- NA >>>res1[,8:13]<-do.call(rbind,lapply(strsplit(as.character(res1$group)," "),as.numeric)) >>>res2<- res1[,c(8:9,2:7,10:13)] >>> >>> >>>?head(res2) >>>#? m1 n1 x1 y1 m n x y????? A??? B????? C??? D >>>#1? 2? 2? 0? 0 4 4 0 0 0.9025 0.64 0.9025 0.64 >>>#2? 2? 2? 0? 0 4 4 0 1 0.9025 0.64 0.9025 0.64 >>>#3? 2? 2? 0? 0 4 4 0 2 0.9025 0.64 0.9025 0.64 >>>#4? 2? 2? 0? 0 4 4 1 0 0.9025 0.64 0.9025 0.64 >>>#5? 2? 2? 0? 0 4 4 1 1 0.9025 0.64 0.9025 0.64 >>>#6? 2? 2? 0? 0 4 4 1 2 0.9025 0.64 0.9025 0.64 >>> >>> >>> >>> >>> >>> >>>________________________________ >>>From: Joanna Zhang <zjoanna2013 at gmail.com> >>>To: arun <smartpink111 at yahoo.com> >>>Sent: Tuesday, February 19, 2013 11:43 AM >>> >>>Subject: Re: [R] cumulative sum by group and under some criteria >>> >>> >>>Thanks. I can get the data I expected (get rid of the m1=3, n1=3) using the join and 'inner' code, but just curious about the way to expand the data. There should be a way to expand the data based on?each row (combination of the variables), unique(d3$m1 & d3$n1) ?. >>> >>>or is there a way to use 'data.frame' and 'for' loop to expand directly from the data? like res1<-data.frame (d3) for () {.... >>> >>> >>>On Tue, Feb 19, 2013 at 9:55 AM, arun <smartpink111 at yahoo.com> wrote: >>> >>>If you can provide me the output that you expect with all the rows of the combination in the res2, I can take a look. >>>>? >>>> >>>> >>>> >>>> >>>> >>>>________________________________ >>>> >>>>From: Joanna Zhang <zjoanna2013 at gmail.com> >>>>To: arun <smartpink111 at yahoo.com> >>>> >>>>Sent: Tuesday, February 19, 2013 10:42 AM >>>> >>>>Subject: Re: [R] cumulative sum by group and under some criteria >>>> >>>> >>>>Thanks. But I thougth the expanded dataset 'res1' should not have combination of m1=3 and n1=3 because it is based on dataset 'd3' which doesn't have m1=3 and n1=3, right?> >>>>>In the example that you provided: >>>>>?(m1+2):(maxN-(n1+2)) >>>>>#[1] 5? >>>>>?(n1+2):(maxN-5) >>>>>#[1] 4? >>>>>#Suppose >>>>>?x1<- 4? >>>>>?y1<- 2? >>>>>?x1:(x1+5-m1) >>>>>#[1] 4 5 6 >>>>>?y1:(y1+4-n1) >>>>>#[1] 2 3 4 >>>>> >>>>>?datnew<-expand.grid(5,4,4:6,2:4) >>>>>?colnames(datnew)<- c("m","n","x","y") >>>>>datnew<-within(datnew,{p1<- x/m;p2<-y/n}) >>>>>res<-cbind(datnew,d2[rep(1:nrow(d2),nrow(datnew)),]) >>>>>?row.names(res)<- 1:nrow(res) >>>>>?res >>>>># ?m n x y ? p2 ?p1 m1 n1 cterm1_P1L cterm1_P0H >>>>>#1 5 4 4 2 0.50 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>>>>#2 5 4 5 2 0.50 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>>>>#3 5 4 6 2 0.50 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>>>>#4 5 4 4 3 0.75 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>>>>#5 5 4 5 3 0.75 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>>>>#6 5 4 6 3 0.75 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>>>>#7 5 4 4 4 1.00 0.8 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>>>>#8 5 4 5 4 1.00 1.0 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>>>>#9 5 4 6 4 1.00 1.2 ?3 ?2 ? ?0.00032 ? ? 0.0025 >>>>> >>>>>A.K. >>>>> >>>>> >>>>> >>>>> >>>>> >>>>>----- Original Message ----- >>>>>From: Zjoanna <Zjoanna2013 at gmail.com> >>>>>To: r-help at r-project.org >>>>>Cc: >>>>> >>>>>Sent: Sunday, February 10, 2013 6:04 PM >>>>>Subject: Re: [R] cumulative sum by group and under some criteria >>>>> >>>>> >>>>>Hi, >>>>>How to expand or loop for one variable n based on another variable? for >>>>>example, I want to add m (from m1 to maxN- n1-2) and for each m, I want to >>>>>add n (n1+2 to maxN-m), and similarly add x and y, then I need to do some >>>>>calculations. >>>>> >>>>>d3<-data.frame(d2) >>>>>? ? for (m in (m1+2):(maxN-(n1+2)){ >>>>>? ? ? ?for (n in (n1+2):(maxN-m)){ >>>>>? ? ? ? ? ? ?for (x in x1:(x1+m-m1)){ >>>>>? ? ? ? ? ? ? ? ? for (y in y1:(y1+n-n1)){ >>>>>? ? ? ? ? ? ? ? ? ? ? ?p1<- x/m >>>>>? ? ? ? ? ? ? ? ? ? ? ?p2<- y/n >>>>>}}}} >>>>> >>>>>On Thu, Feb 7, 2013 at 12:16 AM, arun kirshna [via R] < >>>>>ml-node+s789695n4657773h74 at n4.nabble.com> wrote: >>>>> >>>>>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=0>> >>>>>> >>>>>> To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=1>> >>>>> >>>>>> >>>>>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=2>> >>>>> >>>>>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=3>> >>>>>> >>>>>> >To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=4>> >>>>> >>>>>> >>>>>> >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]<http://user/SendEmail.jtp?type=node&node=4657773&i=5>> >>>>> >>>>>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=6>> >>>>>> >>>>>> >>To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=7>> >>>>> >>>>>> >>>>>> >>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=8>> >>>>> >>>>>> 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]<http://user/SendEmail.jtp?type=node&node=4657773&i=9>";;;;;; >>>>>> <[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=10>> >>>>>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=11> >>>>>>? >>>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=12>";;;;;; >>>>>> <[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=13>> >>>>>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=14> >>>>>>? >>>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]<http://user/SendEmail.jtp?type=node&node=4657773&i=15>> >>>>>> >>>>>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4657773&i=16> >>>>> >>>>>> >>>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] <http://user/SendEmail.jtp?type=node&node=4657773&i=17>> >>>>> >>>>>> 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] <http://user/SendEmail.jtp?type=node&node=4657773&i=18>mailing list >>>>> >>>>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>>>> >>>PLEASE do read the posting guide >>>>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>>> >>>>>> >>>and provide commented, minimal, self-contained, reproducible code. >>>>>> >>> >>>>>> >>> >>>>>> >>>______________________________________________ >>>>>> >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4657773&i=19>mailing list >>>>> >>>>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>>>> >>>PLEASE do read the posting guide >>>>>> http://www.R-project.org/posting-guide.html<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] <http://user/SendEmail.jtp?type=node&node=4657773&i=20>mailing list >>>>> >>>>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>>>> >>>PLEASE do read the posting guide >>>>>> http://www.R-project.org/posting-guide.html<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] <http://user/SendEmail.jtp?type=node&node=4657773&i=21>mailing list >>>>> >>>>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>>>> PLEASE do read the posting guide >>>>>> http://www.R-project.org/posting-guide.html<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-tp4657074p4657773.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-tp4657074p4658133.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. >>>>> >>>>>???? >>>>??? >>> >> >???
res2<- do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){x[,11:14]<-NA;x[,11:12][x$Qm<=c11 & x$x1>2,]<- cumsum(x[,9:10][x$Qm<=c11 & x$x1>2,]); x[,13:14][x$Qn<=c12 & x$y1>2,]<-cumsum(x[,9:10][x$Qn<=c12 & x$y1>2,]); 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) ?tail(res2) #?? m1 n1 x1 y1? Fmm? Fnn??? Qm??? Qn???? term1_p0 term1_p1 cterm1_P0L #28? 2? 3? 1? 2 0.74 0.62 0.320 0.320 0.0006768750? 0.03072????????? 0 #29? 2? 3? 1? 3 0.68 1.00 0.320 0.000 0.0000118750? 0.00256????????? 0 #30? 2? 3? 2? 0 1.00 0.00 0.500 0.500 0.0021434375? 0.02048????????? 0 #31? 2? 3? 2? 1 1.00 0.63 0.185 0.185 0.0003384375? 0.01536????????? 0 #32? 2? 3? 2? 2 1.00 0.73 0.135 0.135 0.0000178125? 0.00384????????? 0 #33? 2? 3? 2? 3 1.00 1.00 0.000 0.000 0.0000003125? 0.00032????????? 0 #?? cterm1_P1L?? cterm1_P0H cterm1_P1H #28????????? 0 0.0001128125??? 0.00512 #29????????? 0 0.0001246875??? 0.00768 #30????????? 0 0.0001246875??? 0.00768 #31????????? 0 0.0001246875??? 0.00768 #32????????? 0 0.0001246875??? 0.00768 #33????????? 0 0.0001250000??? 0.00800 A.K. ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Monday, February 25, 2013 8:14 AM Subject: Re: [R] cumulative sum by group and under some criteria c11<-0.5 c12<-0.5 On Mon, Feb 25, 2013 at 1:20 AM, arun <smartpink111 at yahoo.com> wrote: Hello,>?head(d,2) > >#? m1 n1 x1 y1 Fmm? Fnn Qm?? Qn? term1_p0 term1_p1 >#1? 2? 2? 0? 0?? 0 0.00? 1 1.00 0.8145062?? 0.4096 >#2? 2? 2? 0? 1?? 0 0.64? 1 0.36 0.0857375?? 0.2048 > > >Assuming that `x` and `y` are 'x1` and `y1`, where is 'c11` in the dataset? > >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})) >Error in `[.data.frame`(x[, 9:10], x$Qm <= c11, ) : >? object 'c11' not found > > > >A.K. > > > > >----- Original Message ----- > >From: Zjoanna <Zjoanna2013 at gmail.com> >To: r-help at R-project.org >Cc: >Sent: Sunday, February 24, 2013 4:48 PM >Subject: Re: [R] cumulative sum by group and under some criteria > > >Thanks! Solved. I have another question. > >This is your code for calculated the cumulative sum. how to modify the code >if I want to add another criterion for calculating the cumulative sum: >x[,11:12][x$Qm<=c11,]<-cumsum(x[,9:10][x$Qm<=c11,]);? ?# cumsum if >x$Qm<=c11 & x>2 >x[,13:14][x$Qn<=c12,]<-cumsum(x[,9:10][x$Qn<=c12,]);? ? #cumsum if x$Qn<=12 >& y>2 > > >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,]);? ?# cumsum if >x$Qm<=c11 & x>2 >x[,13:14][x$Qn<=c12,]<-cumsum(x[,9:10][x$Qn<=c12,]);? ? #cumsum if x$Qn<=12 >& y>2 >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 > > >On Sat, Feb 23, 2013 at 10:56 PM, arun kirshna [via R] < >ml-node+s789695n4659515h76 at n4.nabble.com> wrote: > >> Hi, >> You can also use ?rowMins() or ?rowMaxs() from library(matrixStats) >> >> >> library(plyr) >> res2<- join(res1,d3,by=c("m1","n1"),type="inner") >> >> p0L<-0.05 >> p0H<-0.05 >> p1L<-0.20 >> p1H<-0.20 >> >> res2<- within(res2,{p1<- x/m; p2<- y/n;term2_p0<-dbinom(x1,m1, p0L, >> log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* >> dbinom(y-y1,n-n1,p0H, log=FALSE);term2_p1<- dbinom(x1,m1, p1L, log=FALSE)* >> dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* >> dbinom(y-y1,n-n1,p1H, log=FALSE)}) >> >> Pm2<-rbeta(1000, 0.2+res2$x, 0.8+res2$m-res2$x) >> Pn2<-rbeta(1000, 0.2+res2$y, 0.8+res2$n-res2$y) >> Fm2<- ecdf(Pm2) >> Fn2<- ecdf(Pn2) >> library(matrixStats) >>? res3<- within(res2,{Fmm2<-Fm2(p1);Fnn2<- Fn2(p2);R2<- >> (Fmm2+Fnn2)/2;Fmm_f2<-rowMins(cbind(R2,Fmm2));Fnn_f2<-rowMaxs(cbind(R2,Fnn2));Qm2<- >> 1-Fmm_f2;Qn2<- 1-Fnn_f2}) >> head(res3) >> #? m1 n1 x1 y1 m n x y cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H >> term2_p1 >> #1? 2? 2? 0? 0 4 4 0 0? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >> 0.16777216 >> #2? 2? 2? 0? 0 4 4 0 1? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >> 0.08388608 >> #3? 2? 2? 0? 0 4 4 0 2? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >> 0.01048576 >> #4? 2? 2? 0? 0 4 4 1 0? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >> 0.08388608 >> #5? 2? 2? 0? 0 4 4 1 1? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >> 0.04194304 >> #6? 2? 2? 0? 0 4 4 1 2? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >> 0.00524288 >> #? ? ? term2_p0? ?p2? ?p1? ?Qn2 Qm2 Fnn_f2 Fmm_f2? ? ?R2? Fnn2 Fmm2 >> #1 0.6634204313 0.00 0.00 1.000 1.0? 0.000? ? 0.0 0.0000 0.000? 0.0 >> #2 0.0698337296 0.25 0.00 0.593 1.0? 0.407? ? 0.0 0.2035 0.407? 0.0 >> #3 0.0018377297 0.50 0.00 0.302 1.0? 0.698? ? 0.0 0.3490 0.698? 0.0 >> #4 0.0698337296 0.00 0.25 0.800 0.8? 0.200? ? 0.2 0.2000 0.000? 0.4 >> #5 0.0073509189 0.25 0.25 0.593 0.6? 0.407? ? 0.4 0.4035 0.407? 0.4 >> # 0.0001934452 0.50 0.25 0.302 0.6? 0.698? ? 0.4 0.5490 0.698? 0.4 >> >> >> >> >> >> >> ________________________________ >> From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659515&i=0>> >> >> To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659515&i=1>> > >> >> Sent: Saturday, February 23, 2013 5:35 PM >> Subject: Re: [R] cumulative sum by group and under some criteria >> >> >> the row wise min or max of R2, Rmm2 , for example, for the first row, >> suppose R2 = 0.63 and Fmm2 = 0.56, then Fmm_f2 = 0.63. >> >> means draw 1000 samples for each row from a beta distribution. >> >> >> On Sat, Feb 23, 2013 at 3:49 PM, arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659515&i=2>> > >> wrote: >> >> Hi, >> >> > >> >I have a doubt: >> >When you say that min(R2,Fmm2) or max(R2,Fnn2), do you mean the >> corresponding row wise min or max. for these two columns or the maximum >> from the entire two columns. >> > >> >Also, why do you need rbeta(1000,...), should it be rbeta(240,..) because >> if you use the former the nrow from res2 is around 240 and in the formula, >> you are also using x, m , from res2, which makes no sense to me. >> >Arun >> > >> > >> > >> > >> > >> > >> > >> > >> >________________________________ >> >From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659515&i=3>> >> >> >To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659515&i=4>> > >> >> >Sent: Saturday, February 23, 2013 4:12 PM >> > >> >Subject: Re: [R] cumulative sum by group and under some criteria >> > >> > >> >sorry, these are >> >p0L<-0.05 >> >p0H<-0.05 >> >p1L<-0.20 >> >p1H<-0.20 >> > >> > >> > >> >On Sat, Feb 23, 2013 at 2:48 PM, arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659515&i=5>> > >> wrote: >> > >> >Hi, >> >> >> >>If you give only half the information, it will be difficult to solve. >> >> >> >> >> >>d3<-structure(list(m1 = c(2, 3, 2), n1 = c(2, 2, 3), cterm1_P0L >> c(0.9025, >> >>0.857375, 0.9025), cterm1_P1L = c(0.64, 0.512, 0.64), cterm1_P0H >> c(0.9025, >> >>0.9025, 0.857375), cterm1_P1H = c(0.64, 0.64, 0.512)), .Names = c("m1", >> >>"n1", "cterm1_P0L", "cterm1_P1L", "cterm1_P0H", "cterm1_P1H"), row.names >> = c(NA, >> >>3L), class = "data.frame") >> >>d2<- data.frame() >> >> >> >>for (m1 in 2:3) { >> >>? ? for (n1 in 2:3) { >> >>? ? ? ? for (x1 in 0:(m1-1)) { >> >>? ? ? ? ? ? for (y1 in 0:(n1-1)) { >> >>? ? ? ? for (m in (m1+2): (7-n1)){ >> >>? ? ? ? ? ? ? ?for (n in (n1+2):(9-m)){ >> >> >> >>? ? ? ? ? ? ? ?for (x in x1:(x1+m-m1)){ >> >> >> >>? ? ? ? ? ? ?for(y in y1:(y1+n-n1)){ >> >> d2<- rbind(d2,c(m1,n1,x1,y1,m,n,x,y)) >> >> }}}}}}}} >> >>colnames(d2)<-c("m1","n1","x1","y1","m","n","x","y") >> >> >> >> >> >> >> >>res1<-do.call(rbind,lapply(unique(d3$m1),function(m1) >> >> do.call(rbind,lapply(unique(d3$n1),function(n1) >> >> do.call(rbind,lapply(0:(m1-1),function(x1) >> >> do.call(rbind,lapply(0:(n1-1),function(y1) >> >> do.call(rbind,lapply((m1+2):(7-n1),function(m) >> >> do.call(rbind,lapply((n1+2):(9-m),function(n) >> >> do.call(rbind,lapply(x1:(x1+m-m1), function(x) >> >> do.call(rbind,lapply(y1:(y1+n-n1), function(y) >> >> expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) >> >> names(res1)<- c("m1","n1","x1","y1","m","n","x","y") >> >> attr(res1,"out.attrs")<-NULL >> >>res1[]<- sapply(res1,as.integer) >> >> >> >> identical(d2,res1) >> >>#[1] TRUE >> >> >> >>library(plyr) >> >>res2<- join(res1,d3,by=c("m1","n1"),type="inner") >> >>res2$p1<- x/m >> >> res2$p2<- y/n >> >> >> >> >> >> >> >>dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, >> log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, >> log=FALSE) >> >>#Error in dbinom(x1, m1, p0L, log = FALSE) : object 'p0L' not found >> >>A.K. >> >> >> >> >> >> >> >>________________________________ >> >> >> >>From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659515&i=6>> >> >> >>To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659515&i=7>> > >> >> >>Sent: Friday, February 22, 2013 11:02 AM >> >> >> >>Subject: Re: [R] cumulative sum by group and under some criteria >> >> >> >> >> >>Thanks!? Then I need to create new variables based on the res2.? I can't >> find Fmm_f1, Fnn_f2, R2, Qm2, Qn2 until? running the code several times and >> the values of Fnn_f2, Fmm_f2 are correct. >> >> >> >>attach(res2) >> >>res2$p1<-x/m >> >>res2$p2<-y/n >> >>res2$term2_p0 <- dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, >> log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, >> log=FALSE) >> >>res2$term2_p1 <- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, >> log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, >> log=FALSE) >> >>Pm2<-rbeta(1000, 0.2+x, 0.8+m-x) >> >>Fm2<-ecdf(Pm2) >> >>res2$Fmm2<-Fm2(x/m)? #not correct, it comes out after running code two >> times >> >>Pn2<-rbeta(1000, 0.2+y, 0.8+n-y) >> >>Fn2<-ecdf(Pn2) >> >>res2$Fnn2<-Fn2(y/n) >> >>res2$R2<-(Fmm2+Fnn2)/2 >> >>res2$Fmm_f2<-min(R2,Fmm2)? # not correct >> >>res2$Fnn_f2<-max(R2,Fnn2) >> >>res2$Qm2<-(1-Fmm_f2) >> >>res2$Qn2<-(1-Fnn_f2) >> >>detach(res2) >> >>res2 >> >>head(res2) >> >> >> >> >> >> >> >>On Tue, Feb 19, 2013 at 4:09 PM, arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659515&i=8>> > >> wrote: >> >> >>? >>Hi, >> >>> >> >>>""suppose that I have a dataset 'd' >> >>>? ?m1? n1? ? A? ? ? ? ? ? ?B? ? ?C? ? ? ? ?D >> >>>1? 2? 2? ?0.902500? ? 0.640? ?0.9025? ? 0.64 >> >>>2? 3? 2? ?0.857375? ? 0.512? ?0.9025? ? 0.64 >> >>>I want to add? x1 (from 0 to m1), y1(from 0 to n1), m (range from >> >>>m1+2 to 7-n1), n(from n1+2 to 9-m), x (x1 to x1+m-m1), y(y1 to >> y1+n-n1), expanding to another dataset 'd2' based on each row (combination >> of m1 >> >>>and n1)"" >> >>> >> >>> >> >>>Try: >> >>> >> >>> >> >>> d<-read.table(text=" >> >>> >> >>>m1? n1? ? A? ? ? ? ? ? ?B? ? ?C? ? ? ? ?D >> >>>1? 2? 2? ?0.902500? ? 0.640? ?0.9025? ? 0.64 >> >>>2? 3? 2? ?0.857375? ? 0.512? ?0.9025? ? 0.64 >> >>>",sep="",header=TRUE) >> >>> >> >>>vec1<- paste(d[,1],d[,2],d[,3],d[,4],d[,5],d[,6]) >> >>>res1<- do.call(rbind,lapply(vec1,function(m1) >> do.call(rbind,lapply(0:(as.numeric(substr(m1,1,1))),function(x1) >> do.call(rbind,lapply(0:(as.numeric(substr(m1,3,3))),function(y1) >> do.call(rbind,lapply((as.numeric(substr(m1,1,1))+2):(7-as.numeric(substr(m1,3,3))),function(m) >> do.call(rbind,lapply((as.numeric(substr(m1,3,3))+2):(9-m),function(n) >> >>> >> >>> do.call(rbind,lapply(x1:(x1+m-as.numeric(substr(m1,1,1))), function(x) >> >>> do.call(rbind,lapply(y1:(y1+n-as.numeric(substr(m1,3,3))), function(y) >> >>> expand.grid(m1,x1,y1,m,n,x,y)) ))))))))))))) >> >>> >> >>names(res1)<- c("group","x1","y1","m","n","x","y") >> >>> res1$m1<- NA; res1$n1<- NA; res1$A<- NA; res1$B<- NA; res1$C<- >> NA;res1$D <- NA >> >>>res1[,8:13]<-do.call(rbind,lapply(strsplit(as.character(res1$group)," >> "),as.numeric)) >> >>>res2<- res1[,c(8:9,2:7,10:13)] >> >>> >> >>> >> >>> head(res2) >> >>>#? m1 n1 x1 y1 m n x y? ? ? A? ? B? ? ? C? ? D >> >>>#1? 2? 2? 0? 0 4 4 0 0 0.9025 0.64 0.9025 0.64 >> >>>#2? 2? 2? 0? 0 4 4 0 1 0.9025 0.64 0.9025 0.64 >> >>>#3? 2? 2? 0? 0 4 4 0 2 0.9025 0.64 0.9025 0.64 >> >>>#4? 2? 2? 0? 0 4 4 1 0 0.9025 0.64 0.9025 0.64 >> >>>#5? 2? 2? 0? 0 4 4 1 1 0.9025 0.64 0.9025 0.64 >> >>>#6? 2? 2? 0? 0 4 4 1 2 0.9025 0.64 0.9025 0.64 >> >>> >> >>> >> >>> >> >>> >> >>> >> >>> >> >>>________________________________ >> >>>From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659515&i=9>> >> >> >>>To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659515&i=10>> > >> >> >>>Sent: Tuesday, February 19, 2013 11:43 AM >> >>> >> >>>Subject: Re: [R] cumulative sum by group and under some criteria >> >>> >> >>> >> >>>Thanks. I can get the data I expected (get rid of the m1=3, n1=3) using >> the join and 'inner' code, but just curious about the way to expand the >> data. There should be a way to expand the data based on each row >> (combination of the variables), unique(d3$m1 & d3$n1) ?. >> >>> >> >>>or is there a way to use 'data.frame' and 'for' loop to expand directly >> from the data? like res1<-data.frame (d3) for () {.... >> >>> >> >>> >> >>>On Tue, Feb 19, 2013 at 9:55 AM, arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659515&i=11>> > >> wrote: >> >>> >> >>>If you can provide me the output that you expect with all the rows of >> the combination in the res2, I can take a look. >> >>>> >> >>>> >> >>>> >> >>>> >> >>>> >> >>>> >> >>>>________________________________ >> >>>> >> >>>>From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659515&i=12>> >> >> >>>>To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659515&i=13>> > >> >> >>>> >>? >>>>Sent: Tuesday, February 19, 2013 10:42 AM >> >>>> >> >>>>Subject: Re: [R] cumulative sum by group and under some criteria >> >>>> >> >>>> >> >>>>Thanks. But I thougth the expanded dataset 'res1' should not have >> combination of m1=3 and n1=3 because it is based on dataset 'd3' which >> doesn't have m1=3 and n1=3, right?> >> >>>>>In the example that you provided: >> >>>>> (m1+2):(maxN-(n1+2)) >> >>>>>#[1] 5 >> >>>>> (n1+2):(maxN-5) >> >>>>>#[1] 4 >> >>>>>#Suppose >> >>>>> x1<- 4 >> >>>>> y1<- 2 >> >>>>> x1:(x1+5-m1) >> >>>>>#[1] 4 5 6 >> >>>>> y1:(y1+4-n1) >> >>>>>#[1] 2 3 4 >> >>>>> >> >>>>> datnew<-expand.grid(5,4,4:6,2:4) >> >>>>> colnames(datnew)<- c("m","n","x","y") >> >>>>>datnew<-within(datnew,{p1<- x/m;p2<-y/n}) >> >>>>>res<-cbind(datnew,d2[rep(1:nrow(d2),nrow(datnew)),]) >> >>>>> row.names(res)<- 1:nrow(res) >> >>>>> res >> >>>>>#? m n x y? ?p2? p1 m1 n1 cterm1_P1L cterm1_P0H >> >>>>>#1 5 4 4 2 0.50 0.8? 3? 2? ? 0.00032? ? ?0.0025 >> >>>>>#2 5 4 5 2 0.50 1.0? 3? 2? ? 0.00032? ? ?0.0025 >> >>>>>#3 5 4 6 2 0.50 1.2? 3? 2? ? 0.00032? ? ?0.0025 >> >>>>>#4 5 4 4 3 0.75 0.8? 3? 2? ? 0.00032? ? ?0.0025 >> >>>>>#5 5 4 5 3 0.75 1.0? 3? 2? ? 0.00032? ? ?0.0025 >> >>>>>#6 5 4 6 3 0.75 1.2? 3? 2? ? 0.00032? ? ?0.0025 >> >>>>>#7 5 4 4 4 1.00 0.8? 3? 2? ? 0.00032? ? ?0.0025 >> >>>>>#8 5 4 5 4 1.00 1.0? 3? 2? ? 0.00032? ? ?0.0025 >> >>>>>#9 5 4 6 4 1.00 1.2? 3? 2? ? 0.00032? ? ?0.0025 >> >>>>> >> >>>>>A.K. >> >>>>> >> >>>>> >> >>>>> >> >>>>> >> >>>>> >> >>>>>----- Original Message ----- >> >>>>>From: Zjoanna <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659515&i=14>> >> >> >>>>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4659515&i=15> > >> >>>>>Cc: >> >>>>> >> >>>>>Sent: Sunday, February 10, 2013 6:04 PM >> >>>>>Subject: Re: [R] cumulative sum by group and under some criteria >> >>>>> >> >>>>> >> >>>>>Hi, >> >>>>>How to expand or loop for one variable n based on another variable? >> for >> >>>>>example, I want to add m (from m1 to maxN- n1-2) and for each m, I >> want to >> >>>>>add n (n1+2 to maxN-m), and similarly add x and y, then I need to do >> some >> >>>>>calculations. >> >>>>> >> >>>>>d3<-data.frame(d2) >> >>>>>? ? for (m in (m1+2):(maxN-(n1+2)){ >> >>>>>? ? ? ?for (n in (n1+2):(maxN-m)){ >> >>>>>? ? ? ? ? ? ?for (x in x1:(x1+m-m1)){ >> >>>>>? ? ? ? ? ? ? ? ? for (y in y1:(y1+n-n1)){ >> >>>>>? ? ? ? ? ? ? ? ? ? ? ?p1<- x/m >> >>>>>? ? ? ? ? ? ? ? ? ? ? ?p2<- y/n >> >>>>>}}}} >> >>>>> >> >>>>>On Thu, Feb 7, 2013 at 12:16 AM, arun kirshna [via R] < >>? >>>>>[hidden email]<http://user/SendEmail.jtp?type=node&node=4659515&i=16>> > >> wrote: >> >>>>> >> >>>>>> 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]< >> http://user/SendEmail.jtp?type=node&node=4657773&i=0>> >> >>>>>> >> >>>>>> To: arun <[hidden email]< >> http://user/SendEmail.jtp?type=node&node=4657773&i=1>> >> >>>>> >> >>>>>> >> >>>>>> 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]< >> http://user/SendEmail.jtp?type=node&node=4657773&i=2>> >> >>>>> >> >>>>>> 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]< >> http://user/SendEmail.jtp?type=node&node=4657773&i=3>> >> >>>>>> >> >>>>>> >To: arun <[hidden email]< >> http://user/SendEmail.jtp?type=node&node=4657773&i=4>> >> >>>>> >> >>>>>> >> >>>>>> >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]< >> http://user/SendEmail.jtp?type=node&node=4657773&i=5>> >> >>>>> >> >>>>>> 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]< >> http://user/SendEmail.jtp?type=node&node=4657773&i=6>> >> >>>>>> >> >>>>>> >>To: arun <[hidden email]< >> http://user/SendEmail.jtp?type=node&node=4657773&i=7>> >> >>>>> >> >>>>>> >> >>>>>> >>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]< >> http://user/SendEmail.jtp?type=node&node=4657773&i=8>> >> >>>>> >> >>>>>> 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]< >> http://user/SendEmail.jtp?type=node&node=4657773&i=9>";;;;;;; >> >>>>>> <[hidden email] < >> http://user/SendEmail.jtp?type=node&node=4657773&i=10>> >> >>>>>> >>>To: [hidden email]< >> http://user/SendEmail.jtp?type=node&node=4657773&i=11> >> >>>>>>? >>>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]< >> http://user/SendEmail.jtp?type=node&node=4657773&i=12>";;;;;;; >> >>>>>> <[hidden email] < >> http://user/SendEmail.jtp?type=node&node=4657773&i=13>> >> >>>>>> >>>To: [hidden email]< >> http://user/SendEmail.jtp?type=node&node=4657773&i=14> >> >>>>>>? >>>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]< >> http://user/SendEmail.jtp?type=node&node=4657773&i=15>> >> >>>>>> >> >>>>>> >>>To: [hidden email]< >> http://user/SendEmail.jtp?type=node&node=4657773&i=16> >> >>>>> >> >>>>>> >>>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] < >> http://user/SendEmail.jtp?type=node&node=4657773&i=17>> >> >>>>> >> >>>>>> 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] < >> http://user/SendEmail.jtp?type=node&node=4657773&i=18>mailing list >> >>>>> >> >>>>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >> >>>>>> >>>PLEASE do read the posting guide >> >>>>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >> <http://www.r-project.org/posting-guide.html> >> >>>>> >> >>>>>> >>>and provide commented, minimal, self-contained, reproducible >> code. >> >>>>>> >>> >> >>>>>> >>> >> >>>>>> >>>______________________________________________ >> >>>>>> >>>[hidden email] < >> http://user/SendEmail.jtp?type=node&node=4657773&i=19>mailing list >> >>>>> >> >>>>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >> >>>>>> >>>PLEASE do read the posting guide >> >>>>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >> <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] < >> http://user/SendEmail.jtp?type=node&node=4657773&i=20>mailing list >> >>>>> >> >>>>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >> >>>>>> >>>PLEASE do read the posting guide >> >>>>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >> <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] < >> http://user/SendEmail.jtp?type=node&node=4657773&i=21>mailing list >> >>>>> >> >>>>>> https://stat.ethz.ch/mailman/listinfo/r-help >> >>>>>> PLEASE do read the posting guide >> >>>>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >> <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-tp4657074p4657773.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-tp4657074p4658133.html >> >>>>> >> >>>>>Sent from the R help mailing list archive at Nabble.com. >> >>>>>? ? [[alternative HTML version deleted]] >> >>>>> >> >>>>>______________________________________________ >> >>>>>[hidden email]<http://user/SendEmail.jtp?type=node&node=4659515&i=17>mailing list > >> >>>>> >> >>>>>https://stat.ethz.ch/mailman/listinfo/r-help >> >>>>>PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >> >>>>>and provide commented, minimal, self-contained, reproducible code. >> >>>>> >> >>>>> >> >>>> >> >>> >> >> >> > >> >> ______________________________________________ >> [hidden email] <http://user/SendEmail.jtp?type=node&node=4659515&i=18>mailing list > >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<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-tp4657074p4659515.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-tp4657074p4659547.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. > >
The values of 'x' an 'm' from res2.? For more information, you can check ?rbeta. I also don't get what you are up to. If you want to create 1000 random samples for each combinations of x, m ?lapply(lapply(unique(apply(cbind(res2$x,res2$m),1,paste,collapse="")),function(i) as.numeric(unlist(strsplit(i,"")))),function(x) rbeta(1000,0.2+x[1],0.8+x[2]-x[1])) A.K. ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Tuesday, February 26, 2013 10:37 PM Subject: Re: [R] cumulative sum by group and under some criteria I don't get it? What values of x and m are used here? I thought it should create 1000?random?samples from the beta distribution?for each combination of x,m in the data and this is what I wanted. In the code, it is creating 1000 values in total from the combination of values from x, m, ?Pm2<-rbeta(1000, 0.2+res2$x, 0.8+res2$m-res2$x) ?length(Pm2) #[1] 1000 On Tue, Feb 26, 2013 at 8:56 PM, arun <smartpink111 at yahoo.com> wrote: ??> > > >________________________________ > From: Joanna Zhang <zjoanna2013 at gmail.com> >To: arun <smartpink111 at yahoo.com> >Sent: Tuesday, February 26, 2013 9:51 PM > >Subject: Re: [R] cumulative sum by group and under some criteria > > > >Hi, > ># >Pm2<-rbeta(1000, 0.2+1, 0.8+3) #obs4? >this is for x=1, m=2 > >?length(Pm2) >>#[1] 1000 >> >> >>Pn2<-rbeta(1000, 0.2, 0.8+4) >>?length(Pn2) >>#[1] 1000 >>Here, you are creating Pm2 or Pn2 from a single observation. >> >>In the code, it is creating 1000 values in total from the combination of values from x, m, >>?Pm2<-rbeta(1000, 0.2+res2$x, 0.8+res2$m-res2$x) >>?length(Pm2) >>#[1] 1000 >> >>I don't get it here. What values of x and m are used here? I thought it should create 1000 observations for each combination of x,m in the data and this is what I want. >> >? >A.K. >> >> >> >>----- Original Message ----- >> >>From: Zjoanna <Zjoanna2013 at gmail.com> >>To: r-help at r-project.org >>Cc: >> >>Sent: Tuesday, February 26, 2013 3:13 PM >>Subject: Re: [R] cumulative sum by group and under some criteria >> >> >>Hi Arun >> >>I noticed that the values of Fmm, Fnn, and other corresponding variables >>are not correct, for example,? for the 4th obs after you run this code, the >>Fmm is 0.40,? but if you use the x, m, y, n in the 4th row to calculate >>them, the results are not consistent, same for the 5th obs. >> >>#check >># >>Pm2<-rbeta(1000, 0.2+1, 0.8+3) #obs4 >>Pn2<-rbeta(1000, 0.2, 0.8+4) >>Fm2<- ecdf(Pm2) >>Fn2<- ecdf(Pn2) >>Fmm2<-Fm2(1/4) >>Fnn2<-Fn2(0) >>Fmm2? #0.582 >>Fnn2? ?#0 >> >> >>Pm2<-rbeta(1000, 0.2+1, 0.8+3) #obs5 >>Pn2<-rbeta(1000, 0.2+1, 0.8+3) >>Fm2<- ecdf(Pm2) >>Fn2<- ecdf(Pn2) >>Fmm2<-Fm2(1/4) >>Fnn2<-Fn2(1/4) >>Fmm2 #0.404 >>Fnn2? #0.416 >> >> >> >>On Sat, Feb 23, 2013 at 10:53 PM, arun kirshna [via R] < >>ml-node+s789695n4659514h45 at n4.nabble.com> wrote: >> >>> Hi, >>> d3<-structure(list(m1 = c(2, 3, 2), n1 = c(2, 2, 3), cterm1_P0L >>> c(0.9025, >>> 0.857375, 0.9025), cterm1_P1L = c(0.64, 0.512, 0.64), cterm1_P0H >>> c(0.9025, >>> 0.9025, 0.857375), cterm1_P1H = c(0.64, 0.64, 0.512)), .Names = c("m1", >>> "n1", "cterm1_P0L", "cterm1_P1L", "cterm1_P0H", "cterm1_P1H"), row.names >>> c(NA, >>> 3L), class = "data.frame") >>> d2<- data.frame() >>> for (m1 in 2:3) { >>>? ? ?for (n1 in 2:3) { >>>? ? ? ? ?for (x1 in 0:(m1-1)) { >>>? ? ? ? ? ? ?for (y1 in 0:(n1-1)) { >>>? ? ? ? ?for (m in (m1+2): (7-n1)){ >>>? ? ? ? ? ? ? ? for (n in (n1+2):(9-m)){ >>>? ? ? ? ? ? ? ? for (x in x1:(x1+m-m1)){ >>>? ? ? ? ? ? ? for(y in y1:(y1+n-n1)){ >>>? d2<- rbind(d2,c(m1,n1,x1,y1,m,n,x,y)) >>>? }}}}}}}} >>> colnames(d2)<-c("m1","n1","x1","y1","m","n","x","y") >>>? #or >>> >>> res1<-do.call(rbind,lapply(unique(d3$m1),function(m1) >>>? do.call(rbind,lapply(unique(d3$n1),function(n1) >>>? do.call(rbind,lapply(0:(m1-1),function(x1) >>>? do.call(rbind,lapply(0:(n1-1),function(y1) >>>? do.call(rbind,lapply((m1+2):(7-n1),function(m) >>>? do.call(rbind,lapply((n1+2):(9-m),function(n) >>>? do.call(rbind,lapply(x1:(x1+m-m1), function(x) >>>? do.call(rbind,lapply(y1:(y1+n-n1), function(y) >>>? expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) >>>? names(res1)<- c("m1","n1","x1","y1","m","n","x","y") >>>? attr(res1,"out.attrs")<-NULL >>> res1[]<- sapply(res1,as.integer) >>> >>> library(plyr) >>> res2<- join(res1,d3,by=c("m1","n1"),type="inner") >>> >>> #Assuming that these are the values you used: >>> >>> p0L<-0.05 >>> p0H<-0.05 >>> p1L<-0.20 >>> p1H<-0.20 >>> res2<- within(res2,{p1<- x/m; p2<- y/n;term2_p0<-dbinom(x1,m1, p0L, >>> log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* >>> dbinom(y-y1,n-n1,p0H, log=FALSE);term2_p1<- dbinom(x1,m1, p1L, log=FALSE)* >>> dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* >>> dbinom(y-y1,n-n1,p1H, log=FALSE);Pm2<-rbeta(240, 0.2+x, >>> 0.8+m-x);Pn2<-rbeta(240, 0.2+y, 0.8+n-y)}) >>> Fm2<- ecdf(res2$Pm2) >>> Fn2<- ecdf(res2$Pn2) >>> >>> res3<- within(res2,{Fmm2<-Fm2(p1);Fnn2<- Fn2(p2);R2<- (Fmm2+Fnn2)/2}) #not >>> sure about this step especially the Fm2() or Fn2() >>> res3$Fmm_f2<-apply(res3[,c("R2","Fmm2")],1,min) >>>? res3$Fnn_f2<-apply(res3[,c("R2","Fnn2")],1,max) >>> res3<- within(res3,{Qm2<- 1-Fmm_f2;Qn2<- 1-Fnn_f2}) >>> head(res3) >>> #? m1 n1 x1 y1 m n x y cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H >>> Pn2 >>> #1? 2? 2? 0? 0 4 4 0 0? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>> 0.001084648 >>> #2? 2? 2? 0? 0 4 4 0 1? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>> 0.504593909 >>> #3? 2? 2? 0? 0 4 4 0 2? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>> 0.541379357 >>> #4? 2? 2? 0? 0 4 4 1 0? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>> 0.138947785 >>> #5? 2? 2? 0? 0 4 4 1 1? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>> 0.272364957 >>> #6? 2? 2? 0? 0 4 4 1 2? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>> 0.761635059 >>> #? ? ? ? ? ?Pm2? ?term2_p1? ? ?term2_p0? ?p2? ?p1? ? ? ? R2? ? ? Fnn2 Fmm2 >>> #1 1.212348e-05 0.16777216 0.6634204313 0.00 0.00 0.0000000 0.0000000? 0.0 >>> #2 1.007697e-03 0.08388608 0.0698337296 0.25 0.00 0.1791667 0.3583333? 0.0 >>> #3 1.106946e-05 0.01048576 0.0018377297 0.50 0.00 0.3479167 0.6958333? 0.0 >>> # 2.086758e-01 0.08388608 0.0698337296 0.00 0.25 0.2000000 0.0000000? 0.4 >>> #5 2.382179e-01 0.04194304 0.0073509189 0.25 0.25 0.3791667 0.3583333? 0.4 >>> #6 4.494673e-01 0.00524288 0.0001934452 0.50 0.25 0.5479167 0.6958333? 0.4 >>> #? ? ?Fmm_f2? ? Fnn_f2? ? ? ?Qn2? ? ? ?Qm2 >>> #1 0.0000000 0.0000000 1.0000000 1.0000000 >>> #2 0.0000000 0.3583333 0.6416667 1.0000000 >>> #3 0.0000000 0.6958333 0.3041667 1.0000000 >>> #4 0.2000000 0.2000000 0.8000000 0.8000000 >>> #5 0.3791667 0.3791667 0.6208333 0.6208333 >>> #6 0.4000000 0.6958333 0.3041667 0.6000000 >>> >>> >>> A.K. >>> >>> >>> >>> >>> >>> >>> >>> ________________________________ >>> From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=0>> >>> >>> To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=1>> >> >>> >>> Sent: Friday, February 22, 2013 11:02 AM >>> Subject: Re: [R] cumulative sum by group and under some criteria >>> >>> >>> Thanks!? Then I need to create new variables based on the res2.? I can't >>> find Fmm_f1, Fnn_f2, R2, Qm2, Qn2 until? running the code several times and >>> the values of Fnn_f2, Fmm_f2 are correct. >>> >>> attach(res2) >>> res2$p1<-x/m >>> res2$p2<-y/n >>> res2$term2_p0 <- dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, >>> log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, >>> log=FALSE) >>> res2$term2_p1 <- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, >>> log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, >>> log=FALSE) >>> Pm2<-rbeta(1000, 0.2+x, 0.8+m-x) >>> Fm2<-ecdf(Pm2) >>> res2$Fmm2<-Fm2(x/m)? #not correct, it comes out after running code two >>> times >>> Pn2<-rbeta(1000, 0.2+y, 0.8+n-y) >>> Fn2<-ecdf(Pn2) >>> res2$Fnn2<-Fn2(y/n) >>> res2$R2<-(Fmm2+Fnn2)/2 >>> res2$Fmm_f2<-min(R2,Fmm2)? # not correct >>> res2$Fnn_f2<-max(R2,Fnn2) >>> res2$Qm2<-(1-Fmm_f2) >>> res2$Qn2<-(1-Fnn_f2) >>> detach(res2) >>> res2 >>> head(res2) >>> >>> >>> >>> On Tue, Feb 19, 2013 at 4:09 PM, arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=2>> >> >>> wrote: >>> >>> Hi, >>> >>> > >>> >""suppose that I have a dataset 'd' >>> >? ?m1? n1? ? A? ? ? ? ? ? ?B? ? ?C? ? ? ? ?D >>> >1? 2? 2? ?0.902500? ? 0.640? ?0.9025? ? 0.64 >>> >2? 3? 2? ?0.857375? ? 0.512? ?0.9025? ? 0.64 >>> >I want to add? x1 (from 0 to m1), y1(from 0 to n1), m (range from >>> >m1+2 to 7-n1), n(from n1+2 to 9-m), x (x1 to x1+m-m1), y(y1 to y1+n-n1), >>> expanding to another dataset 'd2' based on each row (combination of m1 >>> >and n1)"" >>> > >>> > >>> >Try: >>> > >>> > >>> > d<-read.table(text=" >>> > >>> >m1? n1? ? A? ? ? ? ? ? ?B? ? ?C? ? ? ? ?D >>> >1? 2? 2? ?0.902500? ? 0.640? ?0.9025? ? 0.64 >>> >2? 3? 2? ?0.857375? ? 0.512? ?0.9025? ? 0.64 >>> >",sep="",header=TRUE) >>> > >>> >vec1<- paste(d[,1],d[,2],d[,3],d[,4],d[,5],d[,6]) >>> >res1<- do.call(rbind,lapply(vec1,function(m1) >>> do.call(rbind,lapply(0:(as.numeric(substr(m1,1,1))),function(x1) >>> do.call(rbind,lapply(0:(as.numeric(substr(m1,3,3))),function(y1) >>> do.call(rbind,lapply((as.numeric(substr(m1,1,1))+2):(7-as.numeric(substr(m1,3,3))),function(m) >>> do.call(rbind,lapply((as.numeric(substr(m1,3,3))+2):(9-m),function(n) >>> > >>> > do.call(rbind,lapply(x1:(x1+m-as.numeric(substr(m1,1,1))), function(x) >>> > do.call(rbind,lapply(y1:(y1+n-as.numeric(substr(m1,3,3))), function(y) >>> > expand.grid(m1,x1,y1,m,n,x,y)) ))))))))))))) >>> > >>> names(res1)<- c("group","x1","y1","m","n","x","y") >>> >>> > res1$m1<- NA; res1$n1<- NA; res1$A<- NA; res1$B<- NA; res1$C<- NA;res1$D >>> <- NA >>> >res1[,8:13]<-do.call(rbind,lapply(strsplit(as.character(res1$group)," >>> "),as.numeric)) >>> >res2<- res1[,c(8:9,2:7,10:13)] >>> > >>> > >>> > head(res2) >>> >#? m1 n1 x1 y1 m n x y? ? ? A? ? B? ? ? C? ? D >>> >#1? 2? 2? 0? 0 4 4 0 0 0.9025 0.64 0.9025 0.64 >>> >#2? 2? 2? 0? 0 4 4 0 1 0.9025 0.64 0.9025 0.64 >>> >#3? 2? 2? 0? 0 4 4 0 2 0.9025 0.64 0.9025 0.64 >>> >#4? 2? 2? 0? 0 4 4 1 0 0.9025 0.64 0.9025 0.64 >>> >#5? 2? 2? 0? 0 4 4 1 1 0.9025 0.64 0.9025 0.64 >>> >#6? 2? 2? 0? 0 4 4 1 2 0.9025 0.64 0.9025 0.64 >>> > >>> > >>> > >>> > >>> > >>> > >>> >________________________________ >>> >From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=3>> >>> >>> >To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=4>> >> >>> >>> >Sent: Tuesday, February 19, 2013 11:43 AM >>> > >>> >Subject: Re: [R] cumulative sum by group and under some criteria >>> > >>> > >>> >Thanks. I can get the data I expected (get rid of the m1=3, n1=3) using >>> the join and 'inner' code, but just curious about the way to expand the >>> data. There should be a way to expand the data based on each row >>> (combination of the variables), unique(d3$m1 & d3$n1) ?. >>> > >>> >or is there a way to use 'data.frame' and 'for' loop to expand directly >>> from the data? like res1<-data.frame (d3) for () {.... >>> > >>> > >>> >On Tue, Feb 19, 2013 at 9:55 AM, arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=5>> >> >>> wrote: >>> > >>> >If you can provide me the output that you expect with all the rows of the >>> combination in the res2, I can take a look. >>> >> >>> >> >>> >> >>> >> >>> >> >>> >> >>> >>________________________________ >>> >> >>> >>From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=6>> >>> >>> >>To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=7>> >> >>> >>> >> >>>? >>Sent: Tuesday, February 19, 2013 10:42 AM >>> >> >>> >>Subject: Re: [R] cumulative sum by group and under some criteria >>> >> >>> >> >>> >>Thanks. But I thougth the expanded dataset 'res1' should not have >>> combination of m1=3 and n1=3 because it is based on dataset 'd3' which >>> doesn't have m1=3 and n1=3, right?> >>> >>>In the example that you provided: >>> >>> (m1+2):(maxN-(n1+2)) >>> >>>#[1] 5 >>> >>> (n1+2):(maxN-5) >>> >>>#[1] 4 >>> >>>#Suppose >>> >>> x1<- 4 >>> >>> y1<- 2 >>> >>> x1:(x1+5-m1) >>> >>>#[1] 4 5 6 >>> >>> y1:(y1+4-n1) >>> >>>#[1] 2 3 4 >>> >>> >>> >>> datnew<-expand.grid(5,4,4:6,2:4) >>> >>> colnames(datnew)<- c("m","n","x","y") >>> >>>datnew<-within(datnew,{p1<- x/m;p2<-y/n}) >>> >>>res<-cbind(datnew,d2[rep(1:nrow(d2),nrow(datnew)),]) >>> >>> row.names(res)<- 1:nrow(res) >>> >>> res >>> >>>#? m n x y? ?p2? p1 m1 n1 cterm1_P1L cterm1_P0H >>> >>>#1 5 4 4 2 0.50 0.8? 3? 2? ? 0.00032? ? ?0.0025 >>> >>>#2 5 4 5 2 0.50 1.0? 3? 2? ? 0.00032? ? ?0.0025 >>> >>>#3 5 4 6 2 0.50 1.2? 3? 2? ? 0.00032? ? ?0.0025 >>> >>>#4 5 4 4 3 0.75 0.8? 3? 2? ? 0.00032? ? ?0.0025 >>> >>>#5 5 4 5 3 0.75 1.0? 3? 2? ? 0.00032? ? ?0.0025 >>> >>>#6 5 4 6 3 0.75 1.2? 3? 2? ? 0.00032? ? ?0.0025 >>> >>>#7 5 4 4 4 1.00 0.8? 3? 2? ? 0.00032? ? ?0.0025 >>> >>>#8 5 4 5 4 1.00 1.0? 3? 2? ? 0.00032? ? ?0.0025 >>> >>>#9 5 4 6 4 1.00 1.2? 3? 2? ? 0.00032? ? ?0.0025 >>> >>> >>> >>>A.K. >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>>----- Original Message ----- >>> >>>From: Zjoanna <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=8>> >>> >>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=9> >> >>> >>>Cc: >>> >>> >>> >>>Sent: Sunday, February 10, 2013 6:04 PM >>> >>>Subject: Re: [R] cumulative sum by group and under some criteria >>> >>> >>> >>> >>> >>>Hi, >>> >>>How to expand or loop for one variable n based on another variable? for >>> >>>example, I want to add m (from m1 to maxN- n1-2) and for each m, I want >>> to >>> >>>add n (n1+2 to maxN-m), and similarly add x and y, then I need to do >>> some >>> >>>calculations. >>> >>> >>> >>>d3<-data.frame(d2) >>> >>>? ? for (m in (m1+2):(maxN-(n1+2)){ >>> >>>? ? ? ?for (n in (n1+2):(maxN-m)){ >>> >>>? ? ? ? ? ? ?for (x in x1:(x1+m-m1)){ >>> >>>? ? ? ? ? ? ? ? ? for (y in y1:(y1+n-n1)){ >>> >>>? ? ? ? ? ? ? ? ? ? ? ?p1<- x/m >>> >>>? ? ? ? ? ? ? ? ? ? ? ?p2<- y/n >>> >>>}}}} >>> >>> >>> >>>On Thu, Feb 7, 2013 at 12:16 AM, arun kirshna [via R] < >>>? >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4659514&i=10>> >> >>> wrote: >>> >>> >>> >>>> 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]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=0>> >>> >>>> >>> >>>> To: arun <[hidden email]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=1>> >>> >>> >>> >>>> >>> >>>> 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]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=2>> >>> >>> >>> >>>> 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]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=3>> >>> >>>> >>> >>>> >To: arun <[hidden email]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=4>> >>> >>> >>> >>>> >>> >>>> >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]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=5>> >>> >>> >>> >>>> 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]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=6>> >>> >>>> >>> >>>> >>To: arun <[hidden email]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=7>> >>> >>> >>> >>>> >>> >>>> >>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]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=8>> >>> >>> >>> >>>> 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]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=9>";;;;; >>> >>>> <[hidden email] < >>> http://user/SendEmail.jtp?type=node&node=4657773&i=10>> >>> >>>> >>>To: [hidden email]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=11> >>> >>>>? >>>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]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=12>";;;;; >>> >>>> <[hidden email] < >>> http://user/SendEmail.jtp?type=node&node=4657773&i=13>> >>> >>>> >>>To: [hidden email]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=14> >>> >>>>? >>>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]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=15>> >>> >>>> >>> >>>> >>>To: [hidden email]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=16> >>> >>> >>> >>>> >>>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] < >>> http://user/SendEmail.jtp?type=node&node=4657773&i=17>> >>> >>> >>> >>>> 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] < >>> http://user/SendEmail.jtp?type=node&node=4657773&i=18>mailing list >>> >>> >>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>> >>>PLEASE do read the posting guide >>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>> <http://www.r-project.org/posting-guide.html> >>> >>> >>> >>>> >>>and provide commented, minimal, self-contained, reproducible code. >>> >>>> >>> >>> >>>> >>> >>> >>>> >>>______________________________________________ >>> >>>> >>>[hidden email] < >>> http://user/SendEmail.jtp?type=node&node=4657773&i=19>mailing list >>> >>> >>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>> >>>PLEASE do read the posting guide >>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>> <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] < >>> http://user/SendEmail.jtp?type=node&node=4657773&i=20>mailing list >>> >>> >>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>> >>>PLEASE do read the posting guide >>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>> <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] <http://user/SendEmail.jtp?type=node&node=4657773&i=21>mailing >>> list >>> >>> >>> >>>> https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>> PLEASE do read the posting guide >>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>> <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-tp4657074p4657773.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-tp4657074p4658133.html >>> >>> >>> >>>Sent from the R help mailing list archive at Nabble.com. >>> >>>? ? [[alternative HTML version deleted]] >>> >>> >>> >>>______________________________________________ >>> >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4659514&i=11>mailing list >> >>> >>> >>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>> >>>and provide commented, minimal, self-contained, reproducible code. >>> >>> >>> >>> >>> >> >>> > >>> >>> ______________________________________________ >>> [hidden email] <http://user/SendEmail.jtp?type=node&node=4659514&i=12>mailing list >> >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html<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-tp4657074p4659514.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-tp4657074p4659717.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. >> >> > > >
res2<- join(res1,d3,by=c("m1","n1"),type="inner") p0L<-0.05 p0H<-0.05 p1L<-0.20 p1H<-0.20 ? res2<- within(res2,{p1<- x/m; p2<- y/n;term2_p0<-dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, log=FALSE);term2_p1<- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, log=FALSE)})? res4<-do.call(rbind,lapply(seq_len(nrow(res2)),function(i) {Pm2<-rbeta(1000,0.2+res2[i,"x"],0.8+res2[i,"m"]-res2[i,"x"]);Pn2<- rbeta(1000,0.2+res2[i,"y"],0.8+res2[i,"n"]-res2[i,"y"]); Fm2<- ecdf(Pm2); Fn2<- ecdf(Pn2); Fmm2<- Fm2(res2[i,"p1"]); Fnn2<- Fn2(res2[i,"p2"]);R2<- (Fmm2+Fnn2)/2; Fmm_f2<- min(R2, Fmm2); Fnn_f2<- max(R2, Fnn2); Qm2<- 1-Fmm_f2; Qn2<- 1-Fnn_f2;data.frame(Fmm2,Fnn2,R2,Fmm_f2,Fnn_f2,Qm2,Qn2)})) res5<-cbind(res2,res4) ?head(res5,5) #? m1 n1 x1 y1 m n x y cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H?? term2_p1 #1? 2? 2? 0? 0 4 4 0 0???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.16777216 #2? 2? 2? 0? 0 4 4 0 1???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.08388608 #3? 2? 2? 0? 0 4 4 0 2???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.01048576 #4? 2? 2? 0? 0 4 4 1 0???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.08388608 #5? 2? 2? 0? 0 4 4 1 1???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.04194304 #???? term2_p0?? p2?? p1 Fmm2? Fnn2???? R2 Fmm_f2 Fnn_f2?? Qm2?? Qn2 #1 0.663420431 0.00 0.00 0.00 0.000 0.0000? 0.000? 0.000 1.000 1.000 #2 0.069833730 0.25 0.00 0.00 0.601 0.3005? 0.000? 0.601 1.000 0.399 #3 0.001837730 0.50 0.00 0.00 0.612 0.3060? 0.000? 0.612 1.000 0.388 #4 0.069833730 0.00 0.25 0.59 0.000 0.2950? 0.295? 0.295 0.705 0.705 #5 0.007350919 0.25 0.25 0.60 0.566 0.5830? 0.583? 0.583 0.417 0.417 A.K. ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Tuesday, February 26, 2013 11:32 PM Subject: Re: [R] cumulative sum by group and under some criteria ok. 1) for each row in the data, I want to simulate a sample of 1000?observations(Pm2) for?the combination of x, m and another sample of 1000 observations(Pn2) for?the combination of y, n and 2) get the cumulative distribution of these two samples Fm, Fn, respectively and 3) calculate the percentage of obs? )that are less than the x/m (Fmm2<-Fm2(x/m)and percentage of obs that are less than the y/n ?(Fnn2<-Fn2(y/n) 4) I just want to keep Fmm2 and Fnn2 for each row in the final data Thanks very much for your help. for example, if I simulate a sample of 10 instead of 1000:> Pm2<-rbeta(10, 0.2+1, 0.8+3)? #x=1, m=4 > Pn2<-rbeta(10, 0.2, 0.8+4)???? #y=0, n=4 > Pm2?[1] 0.19567380 0.10242121 0.21295666 0.23824629 0.52519487 0.10825192 0.49724191 0.02098218 0.04740662 0.26410004> Pn2?[1] 6.857148e-05 1.631983e-01 1.340303e-08 1.309932e-01 2.944966e-03 1.133654e-01 9.623050e-02 4.091554e-01 1.103247e-01 5.657689e-04> > Fm2<- ecdf(Pm2) > Fn2<- ecdf(Pn2) > > Fmm2<-Fm2(1/4) > Fnn2<-Fn2(0) > Fmm2?[1] 0.7? # this is the percentage of observation that <= 1/4, I want to keep this vaule for each row> Fnn2[1] 0? # this is the percentage of observation that <= 0 , I want to keep this value for each row>On Tue, Feb 26, 2013 at 10:01 PM, arun <smartpink111 at yahoo.com> wrote: The values of 'x' an 'm' from res2.? For more information, you can check ?rbeta.>I also don't get what you are up to. >If you want to create 1000 random samples for each combinations of x, m >?lapply(lapply(unique(apply(cbind(res2$x,res2$m),1,paste,collapse="")),function(i) as.numeric(unlist(strsplit(i,"")))),function(x) rbeta(1000,0.2+x[1],0.8+x[2]-x[1])) > >A.K. > > > > > > >________________________________ > >From: Joanna Zhang <zjoanna2013 at gmail.com> >To: arun <smartpink111 at yahoo.com> >Sent: Tuesday, February 26, 2013 10:37 PM > >Subject: Re: [R] cumulative sum by group and under some criteria > > >I don't get it? What values of x and m are used here? I thought it should create 1000?random?samples from the beta distribution?for each combination of x,m in the data and this is what I wanted. > > >In the code, it is creating 1000 values in total from the combination of values from x, m, >?Pm2<-rbeta(1000, 0.2+res2$x, 0.8+res2$m-res2$x) >?length(Pm2) >#[1] 1000 > >On Tue, Feb 26, 2013 at 8:56 PM, arun <smartpink111 at yahoo.com> wrote: > >?? >> >> >> >>________________________________ >> From: Joanna Zhang <zjoanna2013 at gmail.com> >>To: arun <smartpink111 at yahoo.com> >>Sent: Tuesday, February 26, 2013 9:51 PM >> >>Subject: Re: [R] cumulative sum by group and under some criteria >> >> >> >>Hi, >> >># >>Pm2<-rbeta(1000, 0.2+1, 0.8+3) #obs4? >>this is for x=1, m=2 >> >>?length(Pm2) >>>#[1] 1000 >>> >>> >>>Pn2<-rbeta(1000, 0.2, 0.8+4) >>>?length(Pn2) >>>#[1] 1000 >>>Here, you are creating Pm2 or Pn2 from a single observation. >>> >>>In the code, it is creating 1000 values in total from the combination of values from x, m, >>>?Pm2<-rbeta(1000, 0.2+res2$x, 0.8+res2$m-res2$x) >>>?length(Pm2) >>>#[1] 1000 >>> >>>I don't get it here. What values of x and m are used here? I thought it should create 1000 observations for each combination of x,m in the data and this is what I want. >>> >>? >>A.K. >>> >>> >>> >>>----- Original Message ----- >>> >>>From: Zjoanna <Zjoanna2013 at gmail.com> >>>To: r-help at r-project.org >>>Cc: >>> >>>Sent: Tuesday, February 26, 2013 3:13 PM >>>Subject: Re: [R] cumulative sum by group and under some criteria >>> >>> >>>Hi Arun >>> >>>I noticed that the values of Fmm, Fnn, and other corresponding variables >>>are not correct, for example,? for the 4th obs after you run this code, the >>>Fmm is 0.40,? but if you use the x, m, y, n in the 4th row to calculate >>>them, the results are not consistent, same for the 5th obs. >>> >>>#check >>># >>>Pm2<-rbeta(1000, 0.2+1, 0.8+3) #obs4 >>>Pn2<-rbeta(1000, 0.2, 0.8+4) >>>Fm2<- ecdf(Pm2) >>>Fn2<- ecdf(Pn2) >>>Fmm2<-Fm2(1/4) >>>Fnn2<-Fn2(0) >>>Fmm2? #0.582 >>>Fnn2? ?#0 >>> >>> >>>Pm2<-rbeta(1000, 0.2+1, 0.8+3) #obs5 >>>Pn2<-rbeta(1000, 0.2+1, 0.8+3) >>>Fm2<- ecdf(Pm2) >>>Fn2<- ecdf(Pn2) >>>Fmm2<-Fm2(1/4) >>>Fnn2<-Fn2(1/4) >>>Fmm2 #0.404 >>>Fnn2? #0.416 >>> >>> >>> >>>On Sat, Feb 23, 2013 at 10:53 PM, arun kirshna [via R] < >>>ml-node+s789695n4659514h45 at n4.nabble.com> wrote: >>> >>>> Hi, >>>> d3<-structure(list(m1 = c(2, 3, 2), n1 = c(2, 2, 3), cterm1_P0L >>>> c(0.9025, >>>> 0.857375, 0.9025), cterm1_P1L = c(0.64, 0.512, 0.64), cterm1_P0H >>>> c(0.9025, >>>> 0.9025, 0.857375), cterm1_P1H = c(0.64, 0.64, 0.512)), .Names = c("m1", >>>> "n1", "cterm1_P0L", "cterm1_P1L", "cterm1_P0H", "cterm1_P1H"), row.names >>>> c(NA, >>>> 3L), class = "data.frame") >>>> d2<- data.frame() >>>> for (m1 in 2:3) { >>>>? ? ?for (n1 in 2:3) { >>>>? ? ? ? ?for (x1 in 0:(m1-1)) { >>>>? ? ? ? ? ? ?for (y1 in 0:(n1-1)) { >>>>? ? ? ? ?for (m in (m1+2): (7-n1)){ >>>>? ? ? ? ? ? ? ? for (n in (n1+2):(9-m)){ >>>>? ? ? ? ? ? ? ? for (x in x1:(x1+m-m1)){ >>>>? ? ? ? ? ? ? for(y in y1:(y1+n-n1)){ >>>>? d2<- rbind(d2,c(m1,n1,x1,y1,m,n,x,y)) >>>>? }}}}}}}} >>>> colnames(d2)<-c("m1","n1","x1","y1","m","n","x","y") >>>>? #or >>>> >>>> res1<-do.call(rbind,lapply(unique(d3$m1),function(m1) >>>>? do.call(rbind,lapply(unique(d3$n1),function(n1) >>>>? do.call(rbind,lapply(0:(m1-1),function(x1) >>>>? do.call(rbind,lapply(0:(n1-1),function(y1) >>>>? do.call(rbind,lapply((m1+2):(7-n1),function(m) >>>>? do.call(rbind,lapply((n1+2):(9-m),function(n) >>>>? do.call(rbind,lapply(x1:(x1+m-m1), function(x) >>>>? do.call(rbind,lapply(y1:(y1+n-n1), function(y) >>>>? expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) >>>>? names(res1)<- c("m1","n1","x1","y1","m","n","x","y") >>>>? attr(res1,"out.attrs")<-NULL >>>> res1[]<- sapply(res1,as.integer) >>>> >>>> library(plyr) >>>> res2<- join(res1,d3,by=c("m1","n1"),type="inner") >>>> >>>> #Assuming that these are the values you used: >>>> >>>> p0L<-0.05 >>>> p0H<-0.05 >>>> p1L<-0.20 >>>> p1H<-0.20 >>>> res2<- within(res2,{p1<- x/m; p2<- y/n;term2_p0<-dbinom(x1,m1, p0L, >>>> log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* >>>> dbinom(y-y1,n-n1,p0H, log=FALSE);term2_p1<- dbinom(x1,m1, p1L, log=FALSE)* >>>> dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* >>>> dbinom(y-y1,n-n1,p1H, log=FALSE);Pm2<-rbeta(240, 0.2+x, >>>> 0.8+m-x);Pn2<-rbeta(240, 0.2+y, 0.8+n-y)}) >>>> Fm2<- ecdf(res2$Pm2) >>>> Fn2<- ecdf(res2$Pn2) >>>> >>>> res3<- within(res2,{Fmm2<-Fm2(p1);Fnn2<- Fn2(p2);R2<- (Fmm2+Fnn2)/2}) #not >>>> sure about this step especially the Fm2() or Fn2() >>>> res3$Fmm_f2<-apply(res3[,c("R2","Fmm2")],1,min) >>>>? res3$Fnn_f2<-apply(res3[,c("R2","Fnn2")],1,max) >>>> res3<- within(res3,{Qm2<- 1-Fmm_f2;Qn2<- 1-Fnn_f2}) >>>> head(res3) >>>> #? m1 n1 x1 y1 m n x y cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H >>>> Pn2 >>>> #1? 2? 2? 0? 0 4 4 0 0? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>> 0.001084648 >>>> #2? 2? 2? 0? 0 4 4 0 1? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>> 0.504593909 >>>> #3? 2? 2? 0? 0 4 4 0 2? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>> 0.541379357 >>>> #4? 2? 2? 0? 0 4 4 1 0? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>> 0.138947785 >>>> #5? 2? 2? 0? 0 4 4 1 1? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>> 0.272364957 >>>> #6? 2? 2? 0? 0 4 4 1 2? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>> 0.761635059 >>>> #? ? ? ? ? ?Pm2? ?term2_p1? ? ?term2_p0? ?p2? ?p1? ? ? ? R2? ? ? Fnn2 Fmm2 >>>> #1 1.212348e-05 0.16777216 0.6634204313 0.00 0.00 0.0000000 0.0000000? 0.0 >>>> #2 1.007697e-03 0.08388608 0.0698337296 0.25 0.00 0.1791667 0.3583333? 0.0 >>>> #3 1.106946e-05 0.01048576 0.0018377297 0.50 0.00 0.3479167 0.6958333? 0.0 >>>> # 2.086758e-01 0.08388608 0.0698337296 0.00 0.25 0.2000000 0.0000000? 0.4 >>>> #5 2.382179e-01 0.04194304 0.0073509189 0.25 0.25 0.3791667 0.3583333? 0.4 >>>> #6 4.494673e-01 0.00524288 0.0001934452 0.50 0.25 0.5479167 0.6958333? 0.4 >>>> #? ? ?Fmm_f2? ? Fnn_f2? ? ? ?Qn2? ? ? ?Qm2 >>>> #1 0.0000000 0.0000000 1.0000000 1.0000000 >>>> #2 0.0000000 0.3583333 0.6416667 1.0000000 >>>> #3 0.0000000 0.6958333 0.3041667 1.0000000 >>>> #4 0.2000000 0.2000000 0.8000000 0.8000000 >>>> #5 0.3791667 0.3791667 0.6208333 0.6208333 >>>> #6 0.4000000 0.6958333 0.3041667 0.6000000 >>>> >>>> >>>> A.K. >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> ________________________________ >>>> From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=0>> >>>> >>>> To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=1>> >>> >>>> >>>> Sent: Friday, February 22, 2013 11:02 AM >>>> Subject: Re: [R] cumulative sum by group and under some criteria >>>> >>>> >>>> Thanks!? Then I need to create new variables based on the res2.? I can't >>>> find Fmm_f1, Fnn_f2, R2, Qm2, Qn2 until? running the code several times and >>>> the values of Fnn_f2, Fmm_f2 are correct. >>>> >>>> attach(res2) >>>> res2$p1<-x/m >>>> res2$p2<-y/n >>>> res2$term2_p0 <- dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, >>>> log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, >>>> log=FALSE) >>>> res2$term2_p1 <- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, >>>> log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, >>>> log=FALSE) >>>> Pm2<-rbeta(1000, 0.2+x, 0.8+m-x) >>>> Fm2<-ecdf(Pm2) >>>> res2$Fmm2<-Fm2(x/m)? #not correct, it comes out after running code two >>>> times >>>> Pn2<-rbeta(1000, 0.2+y, 0.8+n-y) >>>> Fn2<-ecdf(Pn2) >>>> res2$Fnn2<-Fn2(y/n) >>>> res2$R2<-(Fmm2+Fnn2)/2 >>>> res2$Fmm_f2<-min(R2,Fmm2)? # not correct >>>> res2$Fnn_f2<-max(R2,Fnn2) >>>> res2$Qm2<-(1-Fmm_f2) >>>> res2$Qn2<-(1-Fnn_f2) >>>> detach(res2) >>>> res2 >>>> head(res2) >>>> >>>> >>>> >>>> On Tue, Feb 19, 2013 at 4:09 PM, arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=2>> >>> >>>> wrote: >>>> >>>> Hi, >>>> >>>> > >>>> >""suppose that I have a dataset 'd' >>>> >? ?m1? n1? ? A? ? ? ? ? ? ?B? ? ?C? ? ? ? ?D >>>> >1? 2? 2? ?0.902500? ? 0.640? ?0.9025? ? 0.64 >>>> >2? 3? 2? ?0.857375? ? 0.512? ?0.9025? ? 0.64 >>>> >I want to add? x1 (from 0 to m1), y1(from 0 to n1), m (range from >>>> >m1+2 to 7-n1), n(from n1+2 to 9-m), x (x1 to x1+m-m1), y(y1 to y1+n-n1), >>>> expanding to another dataset 'd2' based on each row (combination of m1 >>>> >and n1)"" >>>> > >>>> > >>>> >Try: >>>> > >>>> > >>>> > d<-read.table(text=" >>>> > >>>> >m1? n1? ? A? ? ? ? ? ? ?B? ? ?C? ? ? ? ?D >>>> >1? 2? 2? ?0.902500? ? 0.640? ?0.9025? ? 0.64 >>>> >2? 3? 2? ?0.857375? ? 0.512? ?0.9025? ? 0.64 >>>> >",sep="",header=TRUE) >>>> > >>>> >vec1<- paste(d[,1],d[,2],d[,3],d[,4],d[,5],d[,6]) >>>> >res1<- do.call(rbind,lapply(vec1,function(m1) >>>> do.call(rbind,lapply(0:(as.numeric(substr(m1,1,1))),function(x1) >>>> do.call(rbind,lapply(0:(as.numeric(substr(m1,3,3))),function(y1) >>>> do.call(rbind,lapply((as.numeric(substr(m1,1,1))+2):(7-as.numeric(substr(m1,3,3))),function(m) >>>> do.call(rbind,lapply((as.numeric(substr(m1,3,3))+2):(9-m),function(n) >>>> > >>>> > do.call(rbind,lapply(x1:(x1+m-as.numeric(substr(m1,1,1))), function(x) >>>> > do.call(rbind,lapply(y1:(y1+n-as.numeric(substr(m1,3,3))), function(y) >>>> > expand.grid(m1,x1,y1,m,n,x,y)) ))))))))))))) >>>> > >>>> names(res1)<- c("group","x1","y1","m","n","x","y") >>>> >>>> > res1$m1<- NA; res1$n1<- NA; res1$A<- NA; res1$B<- NA; res1$C<- NA;res1$D >>>> <- NA >>>> >res1[,8:13]<-do.call(rbind,lapply(strsplit(as.character(res1$group)," >>>> "),as.numeric)) >>>> >res2<- res1[,c(8:9,2:7,10:13)] >>>> > >>>> > >>>> > head(res2) >>>> >#? m1 n1 x1 y1 m n x y? ? ? A? ? B? ? ? C? ? D >>>> >#1? 2? 2? 0? 0 4 4 0 0 0.9025 0.64 0.9025 0.64 >>>> >#2? 2? 2? 0? 0 4 4 0 1 0.9025 0.64 0.9025 0.64 >>>> >#3? 2? 2? 0? 0 4 4 0 2 0.9025 0.64 0.9025 0.64 >>>> >#4? 2? 2? 0? 0 4 4 1 0 0.9025 0.64 0.9025 0.64 >>>> >#5? 2? 2? 0? 0 4 4 1 1 0.9025 0.64 0.9025 0.64 >>>> >#6? 2? 2? 0? 0 4 4 1 2 0.9025 0.64 0.9025 0.64 >>>> > >>>> > >>>> > >>>> > >>>> > >>>> > >>>> >________________________________ >>>> >From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=3>> >>>> >>>> >To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=4>> >>> >>>> >>>> >Sent: Tuesday, February 19, 2013 11:43 AM >>>> > >>>> >Subject: Re: [R] cumulative sum by group and under some criteria >>>> > >>>> > >>>> >Thanks. I can get the data I expected (get rid of the m1=3, n1=3) using >>>> the join and 'inner' code, but just curious about the way to expand the >>>> data. There should be a way to expand the data based on each row >>>> (combination of the variables), unique(d3$m1 & d3$n1) ?. >>>> > >>>> >or is there a way to use 'data.frame' and 'for' loop to expand directly >>>> from the data? like res1<-data.frame (d3) for () {.... >>>> > >>>> > >>>> >On Tue, Feb 19, 2013 at 9:55 AM, arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=5>> >>> >>>> wrote: >>>> > >>>> >If you can provide me the output that you expect with all the rows of the >>>> combination in the res2, I can take a look. >>>> >> >>>> >> >>>> >> >>>> >> >>>> >> >>>> >> >>>> >>________________________________ >>>> >> >>>> >>From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=6>> >>>> >>>> >>To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=7>> >>> >>>> >>>> >> >>>>? >>Sent: Tuesday, February 19, 2013 10:42 AM >>>> >> >>>> >>Subject: Re: [R] cumulative sum by group and under some criteria >>>> >> >>>> >> >>>> >>Thanks. But I thougth the expanded dataset 'res1' should not have >>>> combination of m1=3 and n1=3 because it is based on dataset 'd3' which >>>> doesn't have m1=3 and n1=3, right?> >>>> >>>In the example that you provided: >>>> >>> (m1+2):(maxN-(n1+2)) >>>> >>>#[1] 5 >>>> >>> (n1+2):(maxN-5) >>>> >>>#[1] 4 >>>> >>>#Suppose >>>> >>> x1<- 4 >>>> >>> y1<- 2 >>>> >>> x1:(x1+5-m1) >>>> >>>#[1] 4 5 6 >>>> >>> y1:(y1+4-n1) >>>> >>>#[1] 2 3 4 >>>> >>> >>>> >>> datnew<-expand.grid(5,4,4:6,2:4) >>>> >>> colnames(datnew)<- c("m","n","x","y") >>>> >>>datnew<-within(datnew,{p1<- x/m;p2<-y/n}) >>>> >>>res<-cbind(datnew,d2[rep(1:nrow(d2),nrow(datnew)),]) >>>> >>> row.names(res)<- 1:nrow(res) >>>> >>> res >>>> >>>#? m n x y? ?p2? p1 m1 n1 cterm1_P1L cterm1_P0H >>>> >>>#1 5 4 4 2 0.50 0.8? 3? 2? ? 0.00032? ? ?0.0025 >>>> >>>#2 5 4 5 2 0.50 1.0? 3? 2? ? 0.00032? ? ?0.0025 >>>> >>>#3 5 4 6 2 0.50 1.2? 3? 2? ? 0.00032? ? ?0.0025 >>>> >>>#4 5 4 4 3 0.75 0.8? 3? 2? ? 0.00032? ? ?0.0025 >>>> >>>#5 5 4 5 3 0.75 1.0? 3? 2? ? 0.00032? ? ?0.0025 >>>> >>>#6 5 4 6 3 0.75 1.2? 3? 2? ? 0.00032? ? ?0.0025 >>>> >>>#7 5 4 4 4 1.00 0.8? 3? 2? ? 0.00032? ? ?0.0025 >>>> >>>#8 5 4 5 4 1.00 1.0? 3? 2? ? 0.00032? ? ?0.0025 >>>> >>>#9 5 4 6 4 1.00 1.2? 3? 2? ? 0.00032? ? ?0.0025 >>>> >>> >>>> >>>A.K. >>>> >>> >>>> >>> >>>> >>> >>>> >>> >>>> >>> >>>> >>>----- Original Message ----- >>>> >>>From: Zjoanna <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=8>> >>>> >>>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=9> >>> >>>> >>>Cc: >>>> >>> >>>> >>>Sent: Sunday, February 10, 2013 6:04 PM >>>> >>>Subject: Re: [R] cumulative sum by group and under some criteria >>>> >>> >>>> >>> >>>> >>>Hi, >>>> >>>How to expand or loop for one variable n based on another variable? for >>>> >>>example, I want to add m (from m1 to maxN- n1-2) and for each m, I want >>>> to >>>> >>>add n (n1+2 to maxN-m), and similarly add x and y, then I need to do >>>> some >>>> >>>calculations. >>>> >>> >>>> >>>d3<-data.frame(d2) >>>> >>>? ? for (m in (m1+2):(maxN-(n1+2)){ >>>> >>>? ? ? ?for (n in (n1+2):(maxN-m)){ >>>> >>>? ? ? ? ? ? ?for (x in x1:(x1+m-m1)){ >>>> >>>? ? ? ? ? ? ? ? ? for (y in y1:(y1+n-n1)){ >>>> >>>? ? ? ? ? ? ? ? ? ? ? ?p1<- x/m >>>> >>>? ? ? ? ? ? ? ? ? ? ? ?p2<- y/n >>>> >>>}}}} >>>> >>> >>>> >>>On Thu, Feb 7, 2013 at 12:16 AM, arun kirshna [via R] < >>>>? >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4659514&i=10>> >>> >>>> wrote: >>>> >>> >>>> >>>> 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]< >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=0>> >>>> >>>> >>>> >>>> To: arun <[hidden email]< >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=1>> >>>> >>> >>>> >>>> >>>> >>>> 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]< >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=2>> >>>> >>> >>>> >>>> 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]< >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=3>> >>>> >>>> >>>> >>>> >To: arun <[hidden email]< >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=4>> >>>> >>> >>>> >>>> >>>> >>>> >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]< >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=5>> >>>> >>> >>>> >>>> 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]< >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=6>> >>>> >>>> >>>> >>>> >>To: arun <[hidden email]< >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=7>> >>>> >>> >>>> >>>> >>>> >>>> >>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]< >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=8>> >>>> >>> >>>> >>>> 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]< >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=9>";;;;;; >>>> >>>> <[hidden email] < >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=10>> >>>> >>>> >>>To: [hidden email]< >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=11> >>>> >>>>? >>>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]< >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=12>";;;;;; >>>> >>>> <[hidden email] < >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=13>> >>>> >>>> >>>To: [hidden email]< >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=14> >>>> >>>>? >>>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]< >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=15>> >>>> >>>> >>>> >>>> >>>To: [hidden email]< >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=16> >>>> >>> >>>> >>>> >>>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] < >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=17>> >>>> >>> >>>> >>>> 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] < >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=18>mailing list >>>> >>> >>>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>> >>>> >>>PLEASE do read the posting guide >>>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>> <http://www.r-project.org/posting-guide.html> >>>> >>> >>>> >>>> >>>and provide commented, minimal, self-contained, reproducible code. >>>> >>>> >>> >>>> >>>> >>> >>>> >>>> >>>______________________________________________ >>>> >>>> >>>[hidden email] < >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=19>mailing list >>>> >>> >>>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>> >>>> >>>PLEASE do read the posting guide >>>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>> <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] < >>>> http://user/SendEmail.jtp?type=node&node=4657773&i=20>mailing list >>>> >>> >>>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>> >>>> >>>PLEASE do read the posting guide >>>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>> <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] <http://user/SendEmail.jtp?type=node&node=4657773&i=21>mailing >>>> list >>>> >>> >>>> >>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>> >>>> PLEASE do read the posting guide >>>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>> <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-tp4657074p4657773.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-tp4657074p4658133.html >>>> >>> >>>> >>>Sent from the R help mailing list archive at Nabble.com. >>>> >>>? ? [[alternative HTML version deleted]] >>>> >>> >>>> >>>______________________________________________ >>>> >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4659514&i=11>mailing list >>> >>>> >>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>> >>>PLEASE do read the posting guide >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>> >>>and provide commented, minimal, self-contained, reproducible code. >>>> >>> >>>> >>> >>>> >> >>>> > >>>> >>>> ______________________________________________ >>>> [hidden email] <http://user/SendEmail.jtp?type=node&node=4659514&i=12>mailing list >>> >>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>> PLEASE do read the posting guide >>>> http://www.R-project.org/posting-guide.html<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-tp4657074p4659514.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-tp4657074p4659717.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. >>> >>> >> >> >> >?????
dat1<- read.table(text=" m1 n1 m n??? A????? B??? C?? D 2??? 2??? 4? 5? 0.1??? 0.2? 0.2 0.3 2?? 2???? 4? 4? 0.2?? 0.1?? 0.3? 0.4 2?? 3???? 4? 5? 0.5?? 0.6?? 0.2? 0.2 2? 4????? 3? 4? 0.2?? 0.8?? 0.5? 0.3 ",sep="",header=TRUE) dat1[which.max(dat1$D),] #? m1 n1 m n?? A?? B?? C?? D #2? 2? 2 4 4 0.2 0.1 0.3 0.4 A.K. ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Wednesday, February 27, 2013 1:25 AM Subject: Re: [R] cumulative sum by group and under some criteria Thanks very much! it works! suppose that I have a data: m1 n1 m n??? A???? ?B?? ?C? ?D 2??? 2??? 4? 5? 0.1??? 0.2? 0.2 0.3 2?? 2???? 4??4? 0.2?? 0.1?? 0.3? 0.4 I want to identify the max value of D and extract the row , in this example, it is the 2nd row. On Tue, Feb 26, 2013 at 11:13 PM, arun <smartpink111 at yahoo.com> wrote: res2<- join(res1,d3,by=c("m1","n1"),type="inner")> >p0L<-0.05 >p0H<-0.05 >p1L<-0.20 >p1H<-0.20 >? >res2<- within(res2,{p1<- x/m; p2<- y/n;term2_p0<-dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, log=FALSE);term2_p1<- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, log=FALSE)})? > >res4<-do.call(rbind,lapply(seq_len(nrow(res2)),function(i) {Pm2<-rbeta(1000,0.2+res2[i,"x"],0.8+res2[i,"m"]-res2[i,"x"]);Pn2<- rbeta(1000,0.2+res2[i,"y"],0.8+res2[i,"n"]-res2[i,"y"]); Fm2<- ecdf(Pm2); Fn2<- ecdf(Pn2); Fmm2<- Fm2(res2[i,"p1"]); Fnn2<- Fn2(res2[i,"p2"]);R2<- (Fmm2+Fnn2)/2; Fmm_f2<- min(R2, Fmm2); Fnn_f2<- max(R2, Fnn2); Qm2<- 1-Fmm_f2; Qn2<- 1-Fnn_f2;data.frame(Fmm2,Fnn2,R2,Fmm_f2,Fnn_f2,Qm2,Qn2)})) > >res5<-cbind(res2,res4) >?head(res5,5) >#? m1 n1 x1 y1 m n x y cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H?? term2_p1 >#1? 2? 2? 0? 0 4 4 0 0???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.16777216 >#2? 2? 2? 0? 0 4 4 0 1???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.08388608 >#3? 2? 2? 0? 0 4 4 0 2???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.01048576 >#4? 2? 2? 0? 0 4 4 1 0???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.08388608 >#5? 2? 2? 0? 0 4 4 1 1???? 0.9025?????? 0.64???? 0.9025?????? 0.64 0.04194304 >#???? term2_p0?? p2?? p1 Fmm2? Fnn2???? R2 Fmm_f2 Fnn_f2?? Qm2?? Qn2 >#1 0.663420431 0.00 0.00 0.00 0.000 0.0000? 0.000? 0.000 1.000 1.000 >#2 0.069833730 0.25 0.00 0.00 0.601 0.3005? 0.000? 0.601 1.000 0.399 >#3 0.001837730 0.50 0.00 0.00 0.612 0.3060? 0.000? 0.612 1.000 0.388 >#4 0.069833730 0.00 0.25 0.59 0.000 0.2950? 0.295? 0.295 0.705 0.705 >#5 0.007350919 0.25 0.25 0.60 0.566 0.5830? 0.583? 0.583 0.417 0.417 > > >A.K. > > > > >>R-help at r-project.org mailing list >> >>https://stat.ethz.ch/mailman/listinfo/r-help >>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >>and provide commented, minimal, self-contained, reproducible code. >
Hi, It seems like you haven't even looked at the output of d2, (first d2) d2<- cbind(d,d1) head(d2,3) #? m1 n1 x1 y1 p11 p12?? term1_p0?? term1_p1??? Qm??? Qn #1? 2? 2? 0? 0?? 0 0.0 0.81450625 0.31640625 1.000 1.000 #2? 2? 2? 0? 1?? 0 0.5 0.08573750 0.21093750 0.666 0.666 #3? 2? 2? 0? 2?? 0 1.0 0.00225625 0.03515625 0.500 0.500 ?ncol(d2)? # you have only 10 columns in d2 #[1] 10 #2nd problem: You are splitting using d ###############Your code library(zoo) lst1<- split(d,list(d$m1,d$n1)) # should split byd2, because `d` doesn't have Qm or Qn columns? d2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){ x[,13:18]<-NA; #### this code was created for another dataset which obviously had 12 columns x[,13:14][x$Qm<=c11,]<-cumsum(x[,11:12][x$Qm<=c11,]); #### here your term1_p0 and term1_p1 are columns 7 and 8. x[,15:16][x$Qn<=c12,]<-cumsum(x[,11:12][x$Qn<=c12,]); x[,17:18]<-cumsum(x[,11:12]); colnames(x)[13:18]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1"); x1<-na.locf(x); x1[,13:18][is.na(x1[,13:18])]<-0; x1} )) ########################################## #corrected codes: lst1<- split(d2,list(d2$m1,d2$n1)) dNew<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){ x[,11:16]<-NA; x[,11:12][x$Qm<=c11,]<-cumsum(x[,7:8][x$Qm<=c11,]); x[,13:14][x$Qn<=c12,]<-cumsum(x[,7:8][x$Qn<=c12,]); x[,15:16]<-cumsum(x[,7:8]); colnames(x)[11:16]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1"); x1<-na.locf(x); x1[,11:16][is.na(x1[,11:16])]<-0; x1} )) ?row.names(dNew)<- 1:nrow(dNew) ?head(dNew,3) #? m1 n1 x1 y1 p11 p12?? term1_p0?? term1_p1??? Qm??? Qn cterm1_P0L cterm1_P1L #1? 2? 2? 0? 0?? 0 0.0 0.81450625 0.31640625 1.000 1.000????????? 0????????? 0 #2? 2? 2? 0? 1?? 0 0.5 0.08573750 0.21093750 0.666 0.666????????? 0????????? 0 #3? 2? 2? 0? 2?? 0 1.0 0.00225625 0.03515625 0.500 0.500????????? 0????????? 0 #? cterm1_P0H cterm1_P1H sumTerm1_p0 sumTerm1_p1 #1????????? 0????????? 0?? 0.8145062?? 0.3164062 #2????????? 0????????? 0?? 0.9002438?? 0.5273438 #3????????? 0????????? 0?? 0.9025000?? 0.5625000 A.K. ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Friday, March 1, 2013 11:40 AM Subject: Re: [R] cumulative sum by group and under some criteria Hi, why there is an error when I run the cumulative sum code below? Error in `[<-.data.frame`(`*tmp*`, , 16:21, value = NA) : ? new columns would leave holes after existing columns maxN<-9 c11<-0.4 c12<-0.4 c1<-0.5 c2<-0.5 p0L<-0.05 p0H<-0.05 p1L<-0.25 p1H<-0.25 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)???????????????????????????? ?????????????????????? 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,p11,p12,term1_p0,term1_p1)) }} }} colnames(d)<-c("m1","n1","x1","y1","p11","p12","term1_p0","term1_p1") d tail(d) set.seed(8) d1<-do.call(rbind,lapply(seq_len(nrow(d)),function(i){ Pm<- rbeta(1000,0.2+d[i,"x1"],0.8+d[i,"m1"]-d[i,"x1"]); Pn<- rbeta(1000,0.2+d[i,"y1"],0.8+d[i,"n1"]-d[i,"y1"]); Fm<- ecdf(Pm); Fn<- ecdf(Pn); Fmm<- Fm(d[i,"p11"]); Fnn<- Fn(d[i,"p12"]); R<- (Fmm+Fnn)/2; Fmm_f<- max(R, Fmm); Fnn_f<- min(R, Fnn); Qm<- 1-Fmm_f; Qn<- 1-Fnn_f; data.frame(Qm,Qn)})) d2<-cbind(d,d1) d2 library(zoo) lst1<- split(d,list(d$m1,d$n1)) d2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){ x[,13:18]<-NA; x[,13:14][x$Qm<=c11,]<-cumsum(x[,11:12][x$Qm<=c11,]); x[,15:16][x$Qn<=c12,]<-cumsum(x[,11:12][x$Qn<=c12,]); x[,17:18]<-cumsum(x[,11:12]); colnames(x)[13:18]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1"); x1<-na.locf(x); x1[,13:18][is.na(x1[,13:18])]<-0; x1} )) On Tue, Feb 26, 2013 at 8:56 PM, arun <smartpink111 at yahoo.com> wrote: ??> > > >________________________________ > From: Joanna Zhang <zjoanna2013 at gmail.com> >To: arun <smartpink111 at yahoo.com> >Sent: Tuesday, February 26, 2013 9:51 PM > >Subject: Re: [R] cumulative sum by group and under some criteria > > > >Hi, > ># >Pm2<-rbeta(1000, 0.2+1, 0.8+3) #obs4? >this is for x=1, m=2 > >?length(Pm2) >>#[1] 1000 >> >> >>Pn2<-rbeta(1000, 0.2, 0.8+4) >>?length(Pn2) >>#[1] 1000 >>Here, you are creating Pm2 or Pn2 from a single observation. >> >>In the code, it is creating 1000 values in total from the combination of values from x, m, >>?Pm2<-rbeta(1000, 0.2+res2$x, 0.8+res2$m-res2$x) >>?length(Pm2) >>#[1] 1000 >> >>I don't get it here. What values of x and m are used here? I thought it should create 1000 observations for each combination of x,m in the data and this is what I want. >> >? >A.K. >> >> >> >>----- Original Message ----- >> >>From: Zjoanna <Zjoanna2013 at gmail.com> >>To: r-help at r-project.org >>Cc: >> >>Sent: Tuesday, February 26, 2013 3:13 PM >>Subject: Re: [R] cumulative sum by group and under some criteria >> >> >>Hi Arun >> >>I noticed that the values of Fmm, Fnn, and other corresponding variables >>are not correct, for example,? for the 4th obs after you run this code, the >>Fmm is 0.40,? but if you use the x, m, y, n in the 4th row to calculate >>them, the results are not consistent, same for the 5th obs. >> >>#check >># >>Pm2<-rbeta(1000, 0.2+1, 0.8+3) #obs4 >>Pn2<-rbeta(1000, 0.2, 0.8+4) >>Fm2<- ecdf(Pm2) >>Fn2<- ecdf(Pn2) >>Fmm2<-Fm2(1/4) >>Fnn2<-Fn2(0) >>Fmm2? #0.582 >>Fnn2? ?#0 >> >> >>Pm2<-rbeta(1000, 0.2+1, 0.8+3) #obs5 >>Pn2<-rbeta(1000, 0.2+1, 0.8+3) >>Fm2<- ecdf(Pm2) >>Fn2<- ecdf(Pn2) >>Fmm2<-Fm2(1/4) >>Fnn2<-Fn2(1/4) >>Fmm2 #0.404 >>Fnn2? #0.416 >> >> >> >>On Sat, Feb 23, 2013 at 10:53 PM, arun kirshna [via R] < >>ml-node+s789695n4659514h45 at n4.nabble.com> wrote: >> >>> Hi, >>> d3<-structure(list(m1 = c(2, 3, 2), n1 = c(2, 2, 3), cterm1_P0L >>> c(0.9025, >>> 0.857375, 0.9025), cterm1_P1L = c(0.64, 0.512, 0.64), cterm1_P0H >>> c(0.9025, >>> 0.9025, 0.857375), cterm1_P1H = c(0.64, 0.64, 0.512)), .Names = c("m1", >>> "n1", "cterm1_P0L", "cterm1_P1L", "cterm1_P0H", "cterm1_P1H"), row.names >>> c(NA, >>> 3L), class = "data.frame") >>> d2<- data.frame() >>> for (m1 in 2:3) { >>>? ? ?for (n1 in 2:3) { >>>? ? ? ? ?for (x1 in 0:(m1-1)) { >>>? ? ? ? ? ? ?for (y1 in 0:(n1-1)) { >>>? ? ? ? ?for (m in (m1+2): (7-n1)){ >>>? ? ? ? ? ? ? ? for (n in (n1+2):(9-m)){ >>>? ? ? ? ? ? ? ? for (x in x1:(x1+m-m1)){ >>>? ? ? ? ? ? ? for(y in y1:(y1+n-n1)){ >>>? d2<- rbind(d2,c(m1,n1,x1,y1,m,n,x,y)) >>>? }}}}}}}} >>> colnames(d2)<-c("m1","n1","x1","y1","m","n","x","y") >>>? #or >>> >>> res1<-do.call(rbind,lapply(unique(d3$m1),function(m1) >>>? do.call(rbind,lapply(unique(d3$n1),function(n1) >>>? do.call(rbind,lapply(0:(m1-1),function(x1) >>>? do.call(rbind,lapply(0:(n1-1),function(y1) >>>? do.call(rbind,lapply((m1+2):(7-n1),function(m) >>>? do.call(rbind,lapply((n1+2):(9-m),function(n) >>>? do.call(rbind,lapply(x1:(x1+m-m1), function(x) >>>? do.call(rbind,lapply(y1:(y1+n-n1), function(y) >>>? expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) >>>? names(res1)<- c("m1","n1","x1","y1","m","n","x","y") >>>? attr(res1,"out.attrs")<-NULL >>> res1[]<- sapply(res1,as.integer) >>> >>> library(plyr) >>> res2<- join(res1,d3,by=c("m1","n1"),type="inner") >>> >>> #Assuming that these are the values you used: >>> >>> p0L<-0.05 >>> p0H<-0.05 >>> p1L<-0.20 >>> p1H<-0.20 >>> res2<- within(res2,{p1<- x/m; p2<- y/n;term2_p0<-dbinom(x1,m1, p0L, >>> log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* >>> dbinom(y-y1,n-n1,p0H, log=FALSE);term2_p1<- dbinom(x1,m1, p1L, log=FALSE)* >>> dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* >>> dbinom(y-y1,n-n1,p1H, log=FALSE);Pm2<-rbeta(240, 0.2+x, >>> 0.8+m-x);Pn2<-rbeta(240, 0.2+y, 0.8+n-y)}) >>> Fm2<- ecdf(res2$Pm2) >>> Fn2<- ecdf(res2$Pn2) >>> >>> res3<- within(res2,{Fmm2<-Fm2(p1);Fnn2<- Fn2(p2);R2<- (Fmm2+Fnn2)/2}) #not >>> sure about this step especially the Fm2() or Fn2() >>> res3$Fmm_f2<-apply(res3[,c("R2","Fmm2")],1,min) >>>? res3$Fnn_f2<-apply(res3[,c("R2","Fnn2")],1,max) >>> res3<- within(res3,{Qm2<- 1-Fmm_f2;Qn2<- 1-Fnn_f2}) >>> head(res3) >>> #? m1 n1 x1 y1 m n x y cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H >>> Pn2 >>> #1? 2? 2? 0? 0 4 4 0 0? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>> 0.001084648 >>> #2? 2? 2? 0? 0 4 4 0 1? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>> 0.504593909 >>> #3? 2? 2? 0? 0 4 4 0 2? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>> 0.541379357 >>> #4? 2? 2? 0? 0 4 4 1 0? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>> 0.138947785 >>> #5? 2? 2? 0? 0 4 4 1 1? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>> 0.272364957 >>> #6? 2? 2? 0? 0 4 4 1 2? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>> 0.761635059 >>> #? ? ? ? ? ?Pm2? ?term2_p1? ? ?term2_p0? ?p2? ?p1? ? ? ? R2? ? ? Fnn2 Fmm2 >>> #1 1.212348e-05 0.16777216 0.6634204313 0.00 0.00 0.0000000 0.0000000? 0.0 >>> #2 1.007697e-03 0.08388608 0.0698337296 0.25 0.00 0.1791667 0.3583333? 0.0 >>> #3 1.106946e-05 0.01048576 0.0018377297 0.50 0.00 0.3479167 0.6958333? 0.0 >>> # 2.086758e-01 0.08388608 0.0698337296 0.00 0.25 0.2000000 0.0000000? 0.4 >>> #5 2.382179e-01 0.04194304 0.0073509189 0.25 0.25 0.3791667 0.3583333? 0.4 >>> #6 4.494673e-01 0.00524288 0.0001934452 0.50 0.25 0.5479167 0.6958333? 0.4 >>> #? ? ?Fmm_f2? ? Fnn_f2? ? ? ?Qn2? ? ? ?Qm2 >>> #1 0.0000000 0.0000000 1.0000000 1.0000000 >>> #2 0.0000000 0.3583333 0.6416667 1.0000000 >>> #3 0.0000000 0.6958333 0.3041667 1.0000000 >>> #4 0.2000000 0.2000000 0.8000000 0.8000000 >>> #5 0.3791667 0.3791667 0.6208333 0.6208333 >>> #6 0.4000000 0.6958333 0.3041667 0.6000000 >>> >>> >>> A.K. >>> >>> >>> >>> >>> >>> >>> >>> ________________________________ >>> From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=0>> >>> >>> To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=1>> >> >>> >>> Sent: Friday, February 22, 2013 11:02 AM >>> Subject: Re: [R] cumulative sum by group and under some criteria >>> >>> >>> Thanks!? Then I need to create new variables based on the res2.? I can't >>> find Fmm_f1, Fnn_f2, R2, Qm2, Qn2 until? running the code several times and >>> the values of Fnn_f2, Fmm_f2 are correct. >>> >>> attach(res2) >>> res2$p1<-x/m >>> res2$p2<-y/n >>> res2$term2_p0 <- dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, >>> log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, >>> log=FALSE) >>> res2$term2_p1 <- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, >>> log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, >>> log=FALSE) >>> Pm2<-rbeta(1000, 0.2+x, 0.8+m-x) >>> Fm2<-ecdf(Pm2) >>> res2$Fmm2<-Fm2(x/m)? #not correct, it comes out after running code two >>> times >>> Pn2<-rbeta(1000, 0.2+y, 0.8+n-y) >>> Fn2<-ecdf(Pn2) >>> res2$Fnn2<-Fn2(y/n) >>> res2$R2<-(Fmm2+Fnn2)/2 >>> res2$Fmm_f2<-min(R2,Fmm2)? # not correct >>> res2$Fnn_f2<-max(R2,Fnn2) >>> res2$Qm2<-(1-Fmm_f2) >>> res2$Qn2<-(1-Fnn_f2) >>> detach(res2) >>> res2 >>> head(res2) >>> >>> >>> >>> On Tue, Feb 19, 2013 at 4:09 PM, arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=2>> >> >>> wrote: >>> >>> Hi, >>> >>> > >>> >""suppose that I have a dataset 'd' >>> >? ?m1? n1? ? A? ? ? ? ? ? ?B? ? ?C? ? ? ? ?D >>> >1? 2? 2? ?0.902500? ? 0.640? ?0.9025? ? 0.64 >>> >2? 3? 2? ?0.857375? ? 0.512? ?0.9025? ? 0.64 >>> >I want to add? x1 (from 0 to m1), y1(from 0 to n1), m (range from >>> >m1+2 to 7-n1), n(from n1+2 to 9-m), x (x1 to x1+m-m1), y(y1 to y1+n-n1), >>> expanding to another dataset 'd2' based on each row (combination of m1 >>> >and n1)"" >>> > >>> > >>> >Try: >>> > >>> > >>> > d<-read.table(text=" >>> > >>> >m1? n1? ? A? ? ? ? ? ? ?B? ? ?C? ? ? ? ?D >>> >1? 2? 2? ?0.902500? ? 0.640? ?0.9025? ? 0.64 >>> >2? 3? 2? ?0.857375? ? 0.512? ?0.9025? ? 0.64 >>> >",sep="",header=TRUE) >>> > >>> >vec1<- paste(d[,1],d[,2],d[,3],d[,4],d[,5],d[,6]) >>> >res1<- do.call(rbind,lapply(vec1,function(m1) >>> do.call(rbind,lapply(0:(as.numeric(substr(m1,1,1))),function(x1) >>> do.call(rbind,lapply(0:(as.numeric(substr(m1,3,3))),function(y1) >>> do.call(rbind,lapply((as.numeric(substr(m1,1,1))+2):(7-as.numeric(substr(m1,3,3))),function(m) >>> do.call(rbind,lapply((as.numeric(substr(m1,3,3))+2):(9-m),function(n) >>> > >>> > do.call(rbind,lapply(x1:(x1+m-as.numeric(substr(m1,1,1))), function(x) >>> > do.call(rbind,lapply(y1:(y1+n-as.numeric(substr(m1,3,3))), function(y) >>> > expand.grid(m1,x1,y1,m,n,x,y)) ))))))))))))) >>> > >>> names(res1)<- c("group","x1","y1","m","n","x","y") >>> >>> > res1$m1<- NA; res1$n1<- NA; res1$A<- NA; res1$B<- NA; res1$C<- NA;res1$D >>> <- NA >>> >res1[,8:13]<-do.call(rbind,lapply(strsplit(as.character(res1$group)," >>> "),as.numeric)) >>> >res2<- res1[,c(8:9,2:7,10:13)] >>> > >>> > >>> > head(res2) >>> >#? m1 n1 x1 y1 m n x y? ? ? A? ? B? ? ? C? ? D >>> >#1? 2? 2? 0? 0 4 4 0 0 0.9025 0.64 0.9025 0.64 >>> >#2? 2? 2? 0? 0 4 4 0 1 0.9025 0.64 0.9025 0.64 >>> >#3? 2? 2? 0? 0 4 4 0 2 0.9025 0.64 0.9025 0.64 >>> >#4? 2? 2? 0? 0 4 4 1 0 0.9025 0.64 0.9025 0.64 >>> >#5? 2? 2? 0? 0 4 4 1 1 0.9025 0.64 0.9025 0.64 >>> >#6? 2? 2? 0? 0 4 4 1 2 0.9025 0.64 0.9025 0.64 >>> > >>> > >>> > >>> > >>> > >>> > >>> >________________________________ >>> >From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=3>> >>> >>> >To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=4>> >> >>> >>> >Sent: Tuesday, February 19, 2013 11:43 AM >>> > >>> >Subject: Re: [R] cumulative sum by group and under some criteria >>> > >>> > >>> >Thanks. I can get the data I expected (get rid of the m1=3, n1=3) using >>> the join and 'inner' code, but just curious about the way to expand the >>> data. There should be a way to expand the data based on each row >>> (combination of the variables), unique(d3$m1 & d3$n1) ?. >>> > >>> >or is there a way to use 'data.frame' and 'for' loop to expand directly >>> from the data? like res1<-data.frame (d3) for () {.... >>> > >>> > >>> >On Tue, Feb 19, 2013 at 9:55 AM, arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=5>> >> >>> wrote: >>> > >>> >If you can provide me the output that you expect with all the rows of the >>> combination in the res2, I can take a look. >>> >> >>> >> >>> >> >>> >> >>> >> >>> >> >>> >>________________________________ >>> >> >>> >>From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=6>> >>> >>> >>To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=7>> >> >>> >>> >> >>>? >>Sent: Tuesday, February 19, 2013 10:42 AM >>> >> >>> >>Subject: Re: [R] cumulative sum by group and under some criteria >>> >> >>> >> >>> >>Thanks. But I thougth the expanded dataset 'res1' should not have >>> combination of m1=3 and n1=3 because it is based on dataset 'd3' which >>> doesn't have m1=3 and n1=3, right?> >>> >>>In the example that you provided: >>> >>> (m1+2):(maxN-(n1+2)) >>> >>>#[1] 5 >>> >>> (n1+2):(maxN-5) >>> >>>#[1] 4 >>> >>>#Suppose >>> >>> x1<- 4 >>> >>> y1<- 2 >>> >>> x1:(x1+5-m1) >>> >>>#[1] 4 5 6 >>> >>> y1:(y1+4-n1) >>> >>>#[1] 2 3 4 >>> >>> >>> >>> datnew<-expand.grid(5,4,4:6,2:4) >>> >>> colnames(datnew)<- c("m","n","x","y") >>> >>>datnew<-within(datnew,{p1<- x/m;p2<-y/n}) >>> >>>res<-cbind(datnew,d2[rep(1:nrow(d2),nrow(datnew)),]) >>> >>> row.names(res)<- 1:nrow(res) >>> >>> res >>> >>>#? m n x y? ?p2? p1 m1 n1 cterm1_P1L cterm1_P0H >>> >>>#1 5 4 4 2 0.50 0.8? 3? 2? ? 0.00032? ? ?0.0025 >>> >>>#2 5 4 5 2 0.50 1.0? 3? 2? ? 0.00032? ? ?0.0025 >>> >>>#3 5 4 6 2 0.50 1.2? 3? 2? ? 0.00032? ? ?0.0025 >>> >>>#4 5 4 4 3 0.75 0.8? 3? 2? ? 0.00032? ? ?0.0025 >>> >>>#5 5 4 5 3 0.75 1.0? 3? 2? ? 0.00032? ? ?0.0025 >>> >>>#6 5 4 6 3 0.75 1.2? 3? 2? ? 0.00032? ? ?0.0025 >>> >>>#7 5 4 4 4 1.00 0.8? 3? 2? ? 0.00032? ? ?0.0025 >>> >>>#8 5 4 5 4 1.00 1.0? 3? 2? ? 0.00032? ? ?0.0025 >>> >>>#9 5 4 6 4 1.00 1.2? 3? 2? ? 0.00032? ? ?0.0025 >>> >>> >>> >>>A.K. >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>>----- Original Message ----- >>> >>>From: Zjoanna <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=8>> >>> >>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=9> >> >>> >>>Cc: >>> >>> >>> >>>Sent: Sunday, February 10, 2013 6:04 PM >>> >>>Subject: Re: [R] cumulative sum by group and under some criteria >>> >>> >>> >>> >>> >>>Hi, >>> >>>How to expand or loop for one variable n based on another variable? for >>> >>>example, I want to add m (from m1 to maxN- n1-2) and for each m, I want >>> to >>> >>>add n (n1+2 to maxN-m), and similarly add x and y, then I need to do >>> some >>> >>>calculations. >>> >>> >>> >>>d3<-data.frame(d2) >>> >>>? ? for (m in (m1+2):(maxN-(n1+2)){ >>> >>>? ? ? ?for (n in (n1+2):(maxN-m)){ >>> >>>? ? ? ? ? ? ?for (x in x1:(x1+m-m1)){ >>> >>>? ? ? ? ? ? ? ? ? for (y in y1:(y1+n-n1)){ >>> >>>? ? ? ? ? ? ? ? ? ? ? ?p1<- x/m >>> >>>? ? ? ? ? ? ? ? ? ? ? ?p2<- y/n >>> >>>}}}} >>> >>> >>> >>>On Thu, Feb 7, 2013 at 12:16 AM, arun kirshna [via R] < >>>? >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4659514&i=10>> >> >>> wrote: >>> >>> >>> >>>> 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]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=0>> >>> >>>> >>> >>>> To: arun <[hidden email]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=1>> >>> >>> >>> >>>> >>> >>>> 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]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=2>> >>> >>> >>> >>>> 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]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=3>> >>> >>>> >>> >>>> >To: arun <[hidden email]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=4>> >>> >>> >>> >>>> >>> >>>> >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]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=5>> >>> >>> >>> >>>> 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]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=6>> >>> >>>> >>> >>>> >>To: arun <[hidden email]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=7>> >>> >>> >>> >>>> >>> >>>> >>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]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=8>> >>> >>> >>> >>>> 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]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=9>";;;;; >>> >>>> <[hidden email] < >>> http://user/SendEmail.jtp?type=node&node=4657773&i=10>> >>> >>>> >>>To: [hidden email]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=11> >>> >>>>? >>>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]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=12>";;;;; >>> >>>> <[hidden email] < >>> http://user/SendEmail.jtp?type=node&node=4657773&i=13>> >>> >>>> >>>To: [hidden email]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=14> >>> >>>>? >>>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]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=15>> >>> >>>> >>> >>>> >>>To: [hidden email]< >>> http://user/SendEmail.jtp?type=node&node=4657773&i=16> >>> >>> >>> >>>> >>>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] < >>> http://user/SendEmail.jtp?type=node&node=4657773&i=17>> >>> >>> >>> >>>> 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] < >>> http://user/SendEmail.jtp?type=node&node=4657773&i=18>mailing list >>> >>> >>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>> >>>PLEASE do read the posting guide >>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>> <http://www.r-project.org/posting-guide.html> >>> >>> >>> >>>> >>>and provide commented, minimal, self-contained, reproducible code. >>> >>>> >>> >>> >>>> >>> >>> >>>> >>>______________________________________________ >>> >>>> >>>[hidden email] < >>> http://user/SendEmail.jtp?type=node&node=4657773&i=19>mailing list >>> >>> >>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>> >>>PLEASE do read the posting guide >>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>> <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] < >>> http://user/SendEmail.jtp?type=node&node=4657773&i=20>mailing list >>> >>> >>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>> >>>PLEASE do read the posting guide >>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>> <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] <http://user/SendEmail.jtp?type=node&node=4657773&i=21>mailing >>> list >>> >>> >>> >>>> https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>> PLEASE do read the posting guide >>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>> <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-tp4657074p4657773.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-tp4657074p4658133.html >>> >>> >>> >>>Sent from the R help mailing list archive at Nabble.com. >>> >>>? ? [[alternative HTML version deleted]] >>> >>> >>> >>>______________________________________________ >>> >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4659514&i=11>mailing list >> >>> >>> >>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>> >>>PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>> >>>and provide commented, minimal, self-contained, reproducible code. >>> >>> >>> >>> >>> >> >>> > >>> >>> ______________________________________________ >>> [hidden email] <http://user/SendEmail.jtp?type=node&node=4659514&i=12>mailing list >> >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html<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-tp4657074p4659514.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-tp4657074p4659717.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. >> >> > > >??????????????????????????????????????????? ?????????????????????? ?
HI, d #?? m1 n1 x1 y1???? term1_p0???? term1_p1 #1?? 3? 2? 0? 0 0.7737809375 0.2373046875 #2?? 3? 2? 0? 1 0.0814506250 0.1582031250 #3?? 3? 2? 0? 2 0.0021434375 0.0263671875 #4?? 3? 2? 1? 0 0.1221759375 0.2373046875 #5?? 3? 2? 1? 1 0.0128606250 0.1582031250 #6?? 3? 2? 1? 2 0.0003384375 0.0263671875 #7?? 3? 2? 2? 0 0.0064303125 0.0791015625 #8?? 3? 2? 2? 1 0.0006768750 0.0527343750 #9?? 3? 2? 2? 2 0.0000178125 0.0087890625 #10? 3? 2? 3? 0 0.0001128125 0.0087890625 #11? 3? 2? 3? 1 0.0000118750 0.0058593750 #12? 3? 2? 3? 2 0.0000003125 0.0009765625 d2 #?? m1 n1 x1 y1???? term1_p0???? term1_p1???? Qm???? Qn #1?? 3? 2? 0? 0 0.7737809375 0.2373046875 0.0500 0.0810 #2?? 3? 2? 0? 1 0.0814506250 0.1582031250 0.0560 0.6690 #3?? 3? 2? 0? 2 0.0021434375 0.0263671875 0.0410 0.9680 #4?? 3? 2? 1? 0 0.1221759375 0.2373046875 0.3150 0.3150 #5?? 3? 2? 1? 1 0.0128606250 0.1582031250 0.5200 0.6580 #6?? 3? 2? 1? 2 0.0003384375 0.0263671875 0.5360 0.9640 #7?? 3? 2? 2? 0 0.0064303125 0.0791015625 0.4945 0.4945 #8?? 3? 2? 2? 1 0.0006768750 0.0527343750 0.7815 0.7815 #9?? 3? 2? 2? 2 0.0000178125 0.0087890625 0.9030 0.9650 #10? 3? 2? 3? 0 0.0001128125 0.0087890625 0.5350 0.5350 #11? 3? 2? 3? 1 0.0000118750 0.0058593750 0.8195 0.8195 #12? 3? 2? 3? 2 0.0000003125 0.0009765625 0.9835 0.9835 ?lst1<- split(d2,list(d2$m1,d2$n1)) d3<- do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){ x[,9:14]<-NA; x[,9:10][x$Qm<=c11,]<-cumsum(x[,5:6][x$Qm<=c11,]); x[,11:12][x$Qn<=c12,]<-cumsum(x[,5:6][x$Qn<=c12,]); x[,13:14]<-cumsum(x[,5:6]); colnames(x)[9:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1"); x1<-na.locf(x); x1[,9:14][is.na(x1[,9:14])]<-0; x1} )) ?row.names(d3)<-1:nrow(d3) res1<-aggregate(.~m1+n1,data=d3[,c(1:2,9:12)],max) res1 # m1 n1 cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H #1? 3? 2? 0.9795509? 0.6591797? 0.8959569? 0.4746094 ?d3New<- res1[res1[,4]<=0.60 & res1[,6]<0.40,] d3New #[1] m1???????? n1???????? cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H? # zero 0 rows #<0 rows> (or 0-length row.names) ?library(plyr) ?d4<-join(d3New,d,by=c("m1","n1"),type="inner") ?d4 # [1] m1???????? n1???????? cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H #zero 0 rows # [7] x1???????? y1???????? term1_p0?? term1_p1? #<0 rows> (or 0-length row.names) do.call(rbind,lapply(unique(d4$m1),function(m1) ?do.call(rbind,lapply(unique(d4$n1),function(n1) ?do.call(rbind,lapply(unique(d4$x1),function(x1) ?do.call(rbind,lapply(unique(d4$y1),function(y1) ? ?#do.call(rbind,lapply(0:m1,function(x1) ?#do.call(rbind,lapply(0:n1,function(y1) ?do.call(rbind,lapply((m1+2):(maxN-2-n1),function(m) ?do.call(rbind,lapply((n1+2):(maxN-m),function(n) ?do.call(rbind,lapply(x1:(x1+m-m1), function(x) ?do.call(rbind,lapply(y1:(y1+n-n1), function(y) ?expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) #NULL A.K. ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Saturday, March 2, 2013 3:59 PM Subject: Re: [R] cumulative sum by group and under some criteria I can't figure out why there is an error for names (): Error in names(res2) <- c("m1", "n1", "x1", "y1", "m", "n", "x", "y") :? ? attempt to set an attribute on NULL maxN<-9 c11<-0.4 c12<-0.4 c1<-0.5 c2<-0.5 p0L<-0.05 p0H<-0.05 p1L<-0.25 p1H<-0.25 alpha<-0.20 beta<-0.80 result <- vector("list",5) result for (a in 2: (maxN-6)) { d <- data.frame () for ( m1 in a:a) { ? ? ? ?for (n1 in 2: (maxN-m1-4)){ ? ? ? ? ? for (x1 in 0: m1) { ? ? ? ? ? ? ? ?for (y1 in 0: n1) { ? ? ? ? ? ? ? ? ? ? ? ?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,term1_p0,term1_p1)) }} }} colnames(d)<-c("m1","n1","x1","y1","term1_p0","term1_p1")? tail(d) set.seed(8) d1<-do.call(rbind,lapply(seq_len(nrow(d)),function(i){ Pm<- rbeta(1000,0.2+d[i,"x1"],0.8+d[i,"m1"]-d[i,"x1"]); Pn<- rbeta(1000,0.2+d[i,"y1"],0.8+d[i,"n1"]-d[i,"y1"]); Fm<- ecdf(Pm); Fn<- ecdf(Pn); #Fmm<- Fm(d[i,"p11"]); #Fnn<- Fn(d[i,"p12"]); Fmm<- Fm(p1L); Fnn<- Fn(p1H); R<- (Fmm+Fnn)/2;? Fmm_f<- max(R, Fmm); Fnn_f<- min(R, Fnn); Qm<- 1-Fmm_f; Qn<- 1-Fnn_f; data.frame(Qm,Qn)})) d2<-cbind(d,d1) head(d2) library(zoo) lst1<- split(d2,list(d$m1,d$n1))? d2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){? x[,9:14]<-NA;? x[,9:10][x$Qm<=c11,]<-cumsum(x[,5:6][x$Qm<=c11,]);? x[,11:12][x$Qn<=c12,]<-cumsum(x[,5:6][x$Qn<=c12,]); x[,13:14]<-cumsum(x[,5:6]);? colnames(x)[9:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1");? x1<-na.locf(x);? x1[,9:14][is.na(x1[,9:14])]<-0;? x1} ))? row.names(d2)<-1:nrow(d2)? tail(d2) res1<-aggregate(.~m1+n1,data=d2[,c(1:2,9:12)],max) head(res1) d3<-res1[res1[,4]<=0.60 & res1[,6]<0.40,]? tail(d3) library(plyr)? d4<- join(d3,d,by=c("m1","n1"),type="inner") head(d4)? tail(d4) res2<-do.call(rbind,lapply(unique(d4$m1),function(m1)? do.call(rbind,lapply(unique(d4$n1),function(n1) do.call(rbind,lapply(unique(d4$x1),function(x1) do.call(rbind,lapply(unique(d4$y1),function(y1) #do.call(rbind,lapply(0:m1,function(x1)? #do.call(rbind,lapply(0:n1,function(y1)? do.call(rbind,lapply((m1+2):(maxN-2-n1),function(m)? do.call(rbind,lapply((n1+2):(maxN-m),function(n)? do.call(rbind,lapply(x1:(x1+m-m1), function(x)? do.call(rbind,lapply(y1:(y1+n-n1), function(y) expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) names(res2)<- c("m1","n1","x1","y1","m","n","x","y") attr(res2,"out.attrs")<-NULL? tail(res2) result[[a]]<-res2 } result On Sat, Mar 2, 2013 at 1:05 PM, arun <smartpink111 at yahoo.com> wrote: Alright, then go ahead and use a loop.> >A.K. > > > > > > >________________________________ >From: Joanna Zhang <zjoanna2013 at gmail.com> >To: arun <smartpink111 at yahoo.com> >Sent: Saturday, March 2, 2013 1:53 PM > >Subject: Re: [R] cumulative sum by group and under some criteria > > >Thank you! When I run my real data, there was a warning massage 'lack of memory'. I am thinking that I can use a loop to run the same code for each part of the data. But I need to keep each output data "d2" and combine them.? > >for example: > >maxN<-9 >c11<-0.4 >c12<-0.4 > >p0L<-0.05 >p0H<-0.05 >p1L<-0.25 >p1H<-0.25 > > >for (a in 2: (maxN-6)) { >d <- data.frame () >for ( m1 in a:a) { >? ? ?for (n1 in 2: (maxN-m1-4)){ >? ? ? ? ? for (x1 in 0: m1) { >? ? ? ? ? ? ? ?for (y1 in 0: n1) { >? ? ? ? ? ? ? ? ? ? ? ?p11<- (x1/m1) >? ? ? ? ? ? ? ? ? ? ? ?p12<- (y1/n1) ? ? ? ? ? ? ? ? ? ? ? ? ? ?? > >? ? ? ? ? ? ? ? ? ? ? ?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,p11,p12,term1_p0,term1_p1)) >}} >}} >colnames(d)<-c("m1","n1","x1","y1","p11","p12","term1_p0","term1_p1")? >tail(d) > >set.seed(8) >d1<-do.call(rbind,lapply(seq_len(nrow(d)),function(i){ >Pm<- rbeta(1000,0.2+d[i,"x1"],0.8+d[i,"m1"]-d[i,"x1"]); >Pn<- rbeta(1000,0.2+d[i,"y1"],0.8+d[i,"n1"]-d[i,"y1"]); >Fm<- ecdf(Pm); >Fn<- ecdf(Pn); > >Fmm<- Fm(p1L); >Fnn<- Fn(p1H); > >R<- (Fmm+Fnn)/2;? >Fmm_f<- max(R, Fmm); >Fnn_f<- min(R, Fnn); >Qm<- 1-Fmm_f; >Qn<- 1-Fnn_f; >data.frame(Qm,Qn)})) >d2<-cbind(d,d1) >head(d2) ? ? ? ? ? ? ? ? ? ? ? ?# need to name "d2" using the value of a (or another way) to?distinguish from each other?? >} > ># combine all "d2" here > > > >On Fri, Mar 1, 2013 at 12:51 PM, arun <smartpink111 at yahoo.com> wrote: > > >> >>Hi, >> >>It seems like you haven't even looked at the output of d2, (first d2) >>d2<- cbind(d,d1) >>head(d2,3) >>#? m1 n1 x1 y1 p11 p12?? term1_p0?? term1_p1??? Qm??? Qn >>#1? 2? 2? 0? 0?? 0 0.0 0.81450625 0.31640625 1.000 1.000 >>#2? 2? 2? 0? 1?? 0 0.5 0.08573750 0.21093750 0.666 0.666 >>#3? 2? 2? 0? 2?? 0 1.0 0.00225625 0.03515625 0.500 0.500 >>?ncol(d2)? # you have only 10 columns in d2 >>#[1] 10 >> >> >>#2nd problem: >>You are splitting using d >> >>###############Your code >> >>library(zoo) >>lst1<- split(d,list(d$m1,d$n1)) # should split byd2, because `d` doesn't have Qm or Qn columns? >>d2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){ >>x[,13:18]<-NA; #### this code was created for another dataset which obviously had 12 columns >>x[,13:14][x$Qm<=c11,]<-cumsum(x[,11:12][x$Qm<=c11,]); #### here your term1_p0 and term1_p1 are columns 7 and 8. >> >>x[,15:16][x$Qn<=c12,]<-cumsum(x[,11:12][x$Qn<=c12,]); >>x[,17:18]<-cumsum(x[,11:12]); >>colnames(x)[13:18]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1"); >>x1<-na.locf(x); >>x1[,13:18][is.na(x1[,13:18])]<-0; >>x1} >>)) >>########################################## >> >> >>#corrected codes: >> >>lst1<- split(d2,list(d2$m1,d2$n1)) >> >>dNew<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){ >>x[,11:16]<-NA; >>x[,11:12][x$Qm<=c11,]<-cumsum(x[,7:8][x$Qm<=c11,]); >>x[,13:14][x$Qn<=c12,]<-cumsum(x[,7:8][x$Qn<=c12,]); >>x[,15:16]<-cumsum(x[,7:8]); >>colnames(x)[11:16]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1"); >>x1<-na.locf(x); >>x1[,11:16][is.na(x1[,11:16])]<-0; >>x1} >>)) >>?row.names(dNew)<- 1:nrow(dNew) >>?head(dNew,3) >>#? m1 n1 x1 y1 p11 p12?? term1_p0?? term1_p1??? Qm??? Qn cterm1_P0L cterm1_P1L >>#1? 2? 2? 0? 0?? 0 0.0 0.81450625 0.31640625 1.000 1.000????????? 0????????? 0 >>#2? 2? 2? 0? 1?? 0 0.5 0.08573750 0.21093750 0.666 0.666????????? 0????????? 0 >>#3? 2? 2? 0? 2?? 0 1.0 0.00225625 0.03515625 0.500 0.500????????? 0????????? 0 >>#? cterm1_P0H cterm1_P1H sumTerm1_p0 sumTerm1_p1 >>#1????????? 0????????? 0?? 0.8145062?? 0.3164062 >>#2????????? 0????????? 0?? 0.9002438?? 0.5273438 >>#3????????? 0????????? 0?? 0.9025000?? 0.5625000 >> >> >>A.K. >> >> >>________________________________ >> >>From: Joanna Zhang <zjoanna2013 at gmail.com> >>To: arun <smartpink111 at yahoo.com> >>Sent: Friday, March 1, 2013 11:40 AM >> >>Subject: Re: [R] cumulative sum by group and under some criteria >> >> >>Hi, why there is an error when I run the cumulative sum code below? >> >>Error in `[<-.data.frame`(`*tmp*`, , 16:21, value = NA) : >>? new columns would leave holes after existing columns >> >> >>maxN<-9 >>c11<-0.4 >>c12<-0.4 >>c1<-0.5 >>c2<-0.5 >>p0L<-0.05 >>p0H<-0.05 >>p1L<-0.25 >>p1H<-0.25 >> >>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)???????????????????????????? >> >>?????????????????????? 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,p11,p12,term1_p0,term1_p1)) >>}} >>}} >>colnames(d)<-c("m1","n1","x1","y1","p11","p12","term1_p0","term1_p1") >>d >>tail(d) >>set.seed(8) >>d1<-do.call(rbind,lapply(seq_len(nrow(d)),function(i){ >>Pm<- rbeta(1000,0.2+d[i,"x1"],0.8+d[i,"m1"]-d[i,"x1"]); >>Pn<- rbeta(1000,0.2+d[i,"y1"],0.8+d[i,"n1"]-d[i,"y1"]); >>Fm<- ecdf(Pm); >>Fn<- ecdf(Pn); >>Fmm<- Fm(d[i,"p11"]); >>Fnn<- Fn(d[i,"p12"]); >>R<- (Fmm+Fnn)/2; >>Fmm_f<- max(R, Fmm); >>Fnn_f<- min(R, Fnn); >>Qm<- 1-Fmm_f; >>Qn<- 1-Fnn_f; >>data.frame(Qm,Qn)})) >>d2<-cbind(d,d1) >>d2 >> >>library(zoo) >>lst1<- split(d,list(d$m1,d$n1)) >>d2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){ >>x[,13:18]<-NA; >>x[,13:14][x$Qm<=c11,]<-cumsum(x[,11:12][x$Qm<=c11,]); >>x[,15:16][x$Qn<=c12,]<-cumsum(x[,11:12][x$Qn<=c12,]); >>x[,17:18]<-cumsum(x[,11:12]); >>colnames(x)[13:18]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1"); >>x1<-na.locf(x); >>x1[,13:18][is.na(x1[,13:18])]<-0; >>x1} >>)) >> >> >> >> >>On Tue, Feb 26, 2013 at 8:56 PM, arun <smartpink111 at yahoo.com> wrote: >> >>?? >>> >>> >>> >>>________________________________ >>> From: Joanna Zhang <zjoanna2013 at gmail.com> >>>To: arun <smartpink111 at yahoo.com> >>>Sent: Tuesday, February 26, 2013 9:51 PM >>> >>>Subject: Re: [R] cumulative sum by group and under some criteria >>> >>> >>> >>>Hi, >>> >>># >>>Pm2<-rbeta(1000, 0.2+1, 0.8+3) #obs4? >>>this is for x=1, m=2 >>> >>>?length(Pm2) >>>>#[1] 1000 >>>> >>>> >>>>Pn2<-rbeta(1000, 0.2, 0.8+4) >>>>?length(Pn2) >>>>#[1] 1000 >>>>Here, you are creating Pm2 or Pn2 from a single observation. >>>> >>>>In the code, it is creating 1000 values in total from the combination of values from x, m, >>>>?Pm2<-rbeta(1000, 0.2+res2$x, 0.8+res2$m-res2$x) >>>>?length(Pm2) >>>>#[1] 1000 >>>> >>>>I don't get it here. What values of x and m are used here? I thought it should create 1000 observations for each combination of x,m in the data and this is what I want. >>>> >>>? >>>A.K. >>>> >>>> >>>> >>>>----- Original Message ----- >>>> >>>>From: Zjoanna <Zjoanna2013 at gmail.com> >>>>To: r-help at r-project.org >>>>Cc: >>>> >>>>Sent: Tuesday, February 26, 2013 3:13 PM >>>>Subject: Re: [R] cumulative sum by group and under some criteria >>>> >>>> >>>>Hi Arun >>>> >>>>I noticed that the values of Fmm, Fnn, and other corresponding variables >>>>are not correct, for example,? for the 4th obs after you run this code, the >>>>Fmm is 0.40,? but if you use the x, m, y, n in the 4th row to calculate >>>>them, the results are not consistent, same for the 5th obs. >>>> >>>>#check >>>># >>>>Pm2<-rbeta(1000, 0.2+1, 0.8+3) #obs4 >>>>Pn2<-rbeta(1000, 0.2, 0.8+4) >>>>Fm2<- ecdf(Pm2) >>>>Fn2<- ecdf(Pn2) >>>>Fmm2<-Fm2(1/4) >>>>Fnn2<-Fn2(0) >>>>Fmm2? #0.582 >>>>Fnn2? ?#0 >>>> >>>> >>>>Pm2<-rbeta(1000, 0.2+1, 0.8+3) #obs5 >>>>Pn2<-rbeta(1000, 0.2+1, 0.8+3) >>>>Fm2<- ecdf(Pm2) >>>>Fn2<- ecdf(Pn2) >>>>Fmm2<-Fm2(1/4) >>>>Fnn2<-Fn2(1/4) >>>>Fmm2 #0.404 >>>>Fnn2? #0.416 >>>> >>>> >>>> >>>>On Sat, Feb 23, 2013 at 10:53 PM, arun kirshna [via R] < >>>>ml-node+s789695n4659514h45 at n4.nabble.com> wrote: >>>> >>>>> Hi, >>>>> d3<-structure(list(m1 = c(2, 3, 2), n1 = c(2, 2, 3), cterm1_P0L >>>>> c(0.9025, >>>>> 0.857375, 0.9025), cterm1_P1L = c(0.64, 0.512, 0.64), cterm1_P0H >>>>> c(0.9025, >>>>> 0.9025, 0.857375), cterm1_P1H = c(0.64, 0.64, 0.512)), .Names = c("m1", >>>>> "n1", "cterm1_P0L", "cterm1_P1L", "cterm1_P0H", "cterm1_P1H"), row.names >>>>> c(NA, >>>>> 3L), class = "data.frame") >>>>> d2<- data.frame() >>>>> for (m1 in 2:3) { >>>>>? ? ?for (n1 in 2:3) { >>>>>? ? ? ? ?for (x1 in 0:(m1-1)) { >>>>>? ? ? ? ? ? ?for (y1 in 0:(n1-1)) { >>>>>? ? ? ? ?for (m in (m1+2): (7-n1)){ >>>>>? ? ? ? ? ? ? ? for (n in (n1+2):(9-m)){ >>>>>? ? ? ? ? ? ? ? for (x in x1:(x1+m-m1)){ >>>>>? ? ? ? ? ? ? for(y in y1:(y1+n-n1)){ >>>>>? d2<- rbind(d2,c(m1,n1,x1,y1,m,n,x,y)) >>>>>? }}}}}}}} >>>>> colnames(d2)<-c("m1","n1","x1","y1","m","n","x","y") >>>>>? #or >>>>> >>>>> res1<-do.call(rbind,lapply(unique(d3$m1),function(m1) >>>>>? do.call(rbind,lapply(unique(d3$n1),function(n1) >>>>>? do.call(rbind,lapply(0:(m1-1),function(x1) >>>>>? do.call(rbind,lapply(0:(n1-1),function(y1) >>>>>? do.call(rbind,lapply((m1+2):(7-n1),function(m) >>>>>? do.call(rbind,lapply((n1+2):(9-m),function(n) >>>>>? do.call(rbind,lapply(x1:(x1+m-m1), function(x) >>>>>? do.call(rbind,lapply(y1:(y1+n-n1), function(y) >>>>>? expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) >>>>>? names(res1)<- c("m1","n1","x1","y1","m","n","x","y") >>>>>? attr(res1,"out.attrs")<-NULL >>>>> res1[]<- sapply(res1,as.integer) >>>>> >>>>> library(plyr) >>>>> res2<- join(res1,d3,by=c("m1","n1"),type="inner") >>>>> >>>>> #Assuming that these are the values you used: >>>>> >>>>> p0L<-0.05 >>>>> p0H<-0.05 >>>>> p1L<-0.20 >>>>> p1H<-0.20 >>>>> res2<- within(res2,{p1<- x/m; p2<- y/n;term2_p0<-dbinom(x1,m1, p0L, >>>>> log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* >>>>> dbinom(y-y1,n-n1,p0H, log=FALSE);term2_p1<- dbinom(x1,m1, p1L, log=FALSE)* >>>>> dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* >>>>> dbinom(y-y1,n-n1,p1H, log=FALSE);Pm2<-rbeta(240, 0.2+x, >>>>> 0.8+m-x);Pn2<-rbeta(240, 0.2+y, 0.8+n-y)}) >>>>> Fm2<- ecdf(res2$Pm2) >>>>> Fn2<- ecdf(res2$Pn2) >>>>> >>>>> res3<- within(res2,{Fmm2<-Fm2(p1);Fnn2<- Fn2(p2);R2<- (Fmm2+Fnn2)/2}) #not >>>>> sure about this step especially the Fm2() or Fn2() >>>>> res3$Fmm_f2<-apply(res3[,c("R2","Fmm2")],1,min) >>>>>? res3$Fnn_f2<-apply(res3[,c("R2","Fnn2")],1,max) >>>>> res3<- within(res3,{Qm2<- 1-Fmm_f2;Qn2<- 1-Fnn_f2}) >>>>> head(res3) >>>>> #? m1 n1 x1 y1 m n x y cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H >>>>> Pn2 >>>>> #1? 2? 2? 0? 0 4 4 0 0? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>>> 0.001084648 >>>>> #2? 2? 2? 0? 0 4 4 0 1? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>>> 0.504593909 >>>>> #3? 2? 2? 0? 0 4 4 0 2? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>>> 0.541379357 >>>>> #4? 2? 2? 0? 0 4 4 1 0? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>>> 0.138947785 >>>>> #5? 2? 2? 0? 0 4 4 1 1? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>>> 0.272364957 >>>>> #6? 2? 2? 0? 0 4 4 1 2? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>>> 0.761635059 >>>>> #? ? ? ? ? ?Pm2? ?term2_p1? ? ?term2_p0? ?p2? ?p1? ? ? ? R2? ? ? Fnn2 Fmm2 >>>>> #1 1.212348e-05 0.16777216 0.6634204313 0.00 0.00 0.0000000 0.0000000? 0.0 >>>>> #2 1.007697e-03 0.08388608 0.0698337296 0.25 0.00 0.1791667 0.3583333? 0.0 >>>>> #3 1.106946e-05 0.01048576 0.0018377297 0.50 0.00 0.3479167 0.6958333? 0.0 >>>>> # 2.086758e-01 0.08388608 0.0698337296 0.00 0.25 0.2000000 0.0000000? 0.4 >>>>> #5 2.382179e-01 0.04194304 0.0073509189 0.25 0.25 0.3791667 0.3583333? 0.4 >>>>> #6 4.494673e-01 0.00524288 0.0001934452 0.50 0.25 0.5479167 0.6958333? 0.4 >>>>> #? ? ?Fmm_f2? ? Fnn_f2? ? ? ?Qn2? ? ? ?Qm2 >>>>> #1 0.0000000 0.0000000 1.0000000 1.0000000 >>>>> #2 0.0000000 0.3583333 0.6416667 1.0000000 >>>>> #3 0.0000000 0.6958333 0.3041667 1.0000000 >>>>> #4 0.2000000 0.2000000 0.8000000 0.8000000 >>>>> #5 0.3791667 0.3791667 0.6208333 0.6208333 >>>>> #6 0.4000000 0.6958333 0.3041667 0.6000000 >>>>> >>>>> >>>>> A.K. >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> ________________________________ >>>>> From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=0>> >>>>> >>>>> To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=1>> >>>> >>>>> >>>>> Sent: Friday, February 22, 2013 11:02 AM >>>>> Subject: Re: [R] cumulative sum by group and under some criteria >>>>> >>>>> >>>>> Thanks!? Then I need to create new variables based on the res2.? I can't >>>>> find Fmm_f1, Fnn_f2, R2, Qm2, Qn2 until? running the code several times and >>>>> the values of Fnn_f2, Fmm_f2 are correct. >>>>> >>>>> attach(res2) >>>>> res2$p1<-x/m >>>>> res2$p2<-y/n >>>>> res2$term2_p0 <- dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, >>>>> log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, >>>>> log=FALSE) >>>>> res2$term2_p1 <- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, >>>>> log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, >>>>> log=FALSE) >>>>> Pm2<-rbeta(1000, 0.2+x, 0.8+m-x) >>>>> Fm2<-ecdf(Pm2) >>>>> res2$Fmm2<-Fm2(x/m)? #not correct, it comes out after running code two >>>>> times >>>>> Pn2<-rbeta(1000, 0.2+y, 0.8+n-y) >>>>> Fn2<-ecdf(Pn2) >>>>> res2$Fnn2<-Fn2(y/n) >>>>> res2$R2<-(Fmm2+Fnn2)/2 >>>>> res2$Fmm_f2<-min(R2,Fmm2)? # not correct >>>>> res2$Fnn_f2<-max(R2,Fnn2) >>>>> res2$Qm2<-(1-Fmm_f2) >>>>> res2$Qn2<-(1-Fnn_f2) >>>>> detach(res2) >>>>> res2 >>>>> head(res2) >>>>> >>>>> >>>>> >>>>> On Tue, Feb 19, 2013 at 4:09 PM, arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=2>> >>>> >>>>> wrote: >>>>> >>>>> Hi, >>>>> >>>>> > >>>>> >""suppose that I have a dataset 'd' >>>>> >? ?m1? n1? ? A? ? ? ? ? ? ?B? ? ?C? ? ? ? ?D >>>>> >1? 2? 2? ?0.902500? ? 0.640? ?0.9025? ? 0.64 >>>>> >2? 3? 2? ?0.857375? ? 0.512? ?0.9025? ? 0.64 >>>>> >I want to add? x1 (from 0 to m1), y1(from 0 to n1), m (range from >>>>> >m1+2 to 7-n1), n(from n1+2 to 9-m), x (x1 to x1+m-m1), y(y1 to y1+n-n1), >>>>> expanding to another dataset 'd2' based on each row (combination of m1 >>>>> >and n1)"" >>>>> > >>>>> > >>>>> >Try: >>>>> > >>>>> > >>>>> > d<-read.table(text=" >>>>> > >>>>> >m1? n1? ? A? ? ? ? ? ? ?B? ? ?C? ? ? ? ?D >>>>> >1? 2? 2? ?0.902500? ? 0.640? ?0.9025? ? 0.64 >>>>> >2? 3? 2? ?0.857375? ? 0.512? ?0.9025? ? 0.64 >>>>> >",sep="",header=TRUE) >>>>> > >>>>> >vec1<- paste(d[,1],d[,2],d[,3],d[,4],d[,5],d[,6]) >>>>> >res1<- do.call(rbind,lapply(vec1,function(m1) >>>>> do.call(rbind,lapply(0:(as.numeric(substr(m1,1,1))),function(x1) >>>>> do.call(rbind,lapply(0:(as.numeric(substr(m1,3,3))),function(y1) >>>>> do.call(rbind,lapply((as.numeric(substr(m1,1,1))+2):(7-as.numeric(substr(m1,3,3))),function(m) >>>>> do.call(rbind,lapply((as.numeric(substr(m1,3,3))+2):(9-m),function(n) >>>>> > >>>>> > do.call(rbind,lapply(x1:(x1+m-as.numeric(substr(m1,1,1))), function(x) >>>>> > do.call(rbind,lapply(y1:(y1+n-as.numeric(substr(m1,3,3))), function(y) >>>>> > expand.grid(m1,x1,y1,m,n,x,y)) ))))))))))))) >>>>> > >>>>> names(res1)<- c("group","x1","y1","m","n","x","y") >>>>> >>>>> > res1$m1<- NA; res1$n1<- NA; res1$A<- NA; res1$B<- NA; res1$C<- NA;res1$D >>>>> <- NA >>>>> >res1[,8:13]<-do.call(rbind,lapply(strsplit(as.character(res1$group)," >>>>> "),as.numeric)) >>>>> >res2<- res1[,c(8:9,2:7,10:13)] >>>>> > >>>>> > >>>>> > head(res2) >>>>> >#? m1 n1 x1 y1 m n x y? ? ? A? ? B? ? ? C? ? D >>>>> >#1? 2? 2? 0? 0 4 4 0 0 0.9025 0.64 0.9025 0.64 >>>>> >#2? 2? 2? 0? 0 4 4 0 1 0.9025 0.64 0.9025 0.64 >>>>> >#3? 2? 2? 0? 0 4 4 0 2 0.9025 0.64 0.9025 0.64 >>>>> >#4? 2? 2? 0? 0 4 4 1 0 0.9025 0.64 0.9025 0.64 >>>>> >#5? 2? 2? 0? 0 4 4 1 1 0.9025 0.64 0.9025 0.64 >>>>> >#6? 2? 2? 0? 0 4 4 1 2 0.9025 0.64 0.9025 0.64 >>>>> > >>>>> > >>>>> > >>>>> > >>>>> > >>>>> > >>>>> >________________________________ >>>>> >From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=3>> >>>>> >>>>> >To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=4>> >>>> >>>>> >>>>> >Sent: Tuesday, February 19, 2013 11:43 AM >>>>> > >>>>> >Subject: Re: [R] cumulative sum by group and under some criteria >>>>> > >>>>> > >>>>> >Thanks. I can get the data I expected (get rid of the m1=3, n1=3) using >>>>> the join and 'inner' code, but just curious about the way to expand the >>>>> data. There should be a way to expand the data based on each row >>>>> (combination of the variables), unique(d3$m1 & d3$n1) ?. >>>>> > >>>>> >or is there a way to use 'data.frame' and 'for' loop to expand directly >>>>> from the data? like res1<-data.frame (d3) for () {.... >>>>> > >>>>> > >>>>> >On Tue, Feb 19, 2013 at 9:55 AM, arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=5>> >>>> >>>>> wrote: >>>>> > >>>>> >If you can provide me the output that you expect with all the rows of the >>>>> combination in the res2, I can take a look. >>>>> >> >>>>> >> >>>>> >> >>>>> >> >>>>> >> >>>>> >> >>>>> >>________________________________ >>>>> >> >>>>> >>From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=6>> >>>>> >>>>> >>To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=7>> >>>> >>>>> >>>>> >> >>>>>? >>Sent: Tuesday, February 19, 2013 10:42 AM >>>>> >> >>>>> >>Subject: Re: [R] cumulative sum by group and under some criteria >>>>> >> >>>>> >> >>>>> >>Thanks. But I thougth the expanded dataset 'res1' should not have >>>>> combination of m1=3 and n1=3 because it is based on dataset 'd3' which >>>>> doesn't have m1=3 and n1=3, right?> >>>>> >>>In the example that you provided: >>>>> >>> (m1+2):(maxN-(n1+2)) >>>>> >>>#[1] 5 >>>>> >>> (n1+2):(maxN-5) >>>>> >>>#[1] 4 >>>>> >>>#Suppose >>>>> >>> x1<- 4 >>>>> >>> y1<- 2 >>>>> >>> x1:(x1+5-m1) >>>>> >>>#[1] 4 5 6 >>>>> >>> y1:(y1+4-n1) >>>>> >>>#[1] 2 3 4 >>>>> >>> >>>>> >>> datnew<-expand.grid(5,4,4:6,2:4) >>>>> >>> colnames(datnew)<- c("m","n","x","y") >>>>> >>>datnew<-within(datnew,{p1<- x/m;p2<-y/n}) >>>>> >>>res<-cbind(datnew,d2[rep(1:nrow(d2),nrow(datnew)),]) >>>>> >>> row.names(res)<- 1:nrow(res) >>>>> >>> res >>>>> >>>#? m n x y? ?p2? p1 m1 n1 cterm1_P1L cterm1_P0H >>>>> >>>#1 5 4 4 2 0.50 0.8? 3? 2? ? 0.00032? ? ?0.0025 >>>>> >>>#2 5 4 5 2 0.50 1.0? 3? 2? ? 0.00032? ? ?0.0025 >>>>> >>>#3 5 4 6 2 0.50 1.2? 3? 2? ? 0.00032? ? ?0.0025 >>>>> >>>#4 5 4 4 3 0.75 0.8? 3? 2? ? 0.00032? ? ?0.0025 >>>>> >>>#5 5 4 5 3 0.75 1.0? 3? 2? ? 0.00032? ? ?0.0025 >>>>> >>>#6 5 4 6 3 0.75 1.2? 3? 2? ? 0.00032? ? ?0.0025 >>>>> >>>#7 5 4 4 4 1.00 0.8? 3? 2? ? 0.00032? ? ?0.0025 >>>>> >>>#8 5 4 5 4 1.00 1.0? 3? 2? ? 0.00032? ? ?0.0025 >>>>> >>>#9 5 4 6 4 1.00 1.2? 3? 2? ? 0.00032? ? ?0.0025 >>>>> >>> >>>>> >>>A.K. >>>>> >>> >>>>> >>> >>>>> >>> >>>>> >>> >>>>> >>> >>>>> >>>----- Original Message ----- >>>>> >>>From: Zjoanna <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=8>> >>>>> >>>>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=9> >>>> >>>>> >>>Cc: >>>>> >>> >>>>> >>>Sent: Sunday, February 10, 2013 6:04 PM >>>>> >>>Subject: Re: [R] cumulative sum by group and under some criteria >>>>> >>> >>>>> >>> >>>>> >>>Hi, >>>>> >>>How to expand or loop for one variable n based on another variable? for >>>>> >>>example, I want to add m (from m1 to maxN- n1-2) and for each m, I want >>>>> to >>>>> >>>add n (n1+2 to maxN-m), and similarly add x and y, then I need to do >>>>> some >>>>> >>>calculations. >>>>> >>> >>>>> >>>d3<-data.frame(d2) >>>>> >>>? ? for (m in (m1+2):(maxN-(n1+2)){ >>>>> >>>? ? ? ?for (n in (n1+2):(maxN-m)){ >>>>> >>>? ? ? ? ? ? ?for (x in x1:(x1+m-m1)){ >>>>> >>>? ? ? ? ? ? ? ? ? for (y in y1:(y1+n-n1)){ >>>>> >>>? ? ? ? ? ? ? ? ? ? ? ?p1<- x/m >>>>> >>>? ? ? ? ? ? ? ? ? ? ? ?p2<- y/n >>>>> >>>}}}} >>>>> >>> >>>>> >>>On Thu, Feb 7, 2013 at 12:16 AM, arun kirshna [via R] < >>>>>? >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4659514&i=10>> >>>> >>>>> wrote: >>>>> >>> >>>>> >>>> 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]< >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=0>> >>>>> >>>> >>>>> >>>> To: arun <[hidden email]< >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=1>> >>>>> >>> >>>>> >>>> >>>>> >>>> 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]< >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=2>> >>>>> >>> >>>>> >>>> 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]< >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=3>> >>>>> >>>> >>>>> >>>> >To: arun <[hidden email]< >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=4>> >>>>> >>> >>>>> >>>> >>>>> >>>> >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]< >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=5>> >>>>> >>> >>>>> >>>> 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]< >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=6>> >>>>> >>>> >>>>> >>>> >>To: arun <[hidden email]< >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=7>> >>>>> >>> >>>>> >>>> >>>>> >>>> >>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]< >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=8>> >>>>> >>> >>>>> >>>> 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]< >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=9>";;;;;;; >>>>> >>>> <[hidden email] < >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=10>> >>>>> >>>> >>>To: [hidden email]< >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=11> >>>>> >>>>? >>>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]< >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=12>";;;;;;; >>>>> >>>> <[hidden email] < >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=13>> >>>>> >>>> >>>To: [hidden email]< >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=14> >>>>> >>>>? >>>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]< >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=15>> >>>>> >>>> >>>>> >>>> >>>To: [hidden email]< >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=16> >>>>> >>> >>>>> >>>> >>>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] < >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=17>> >>>>> >>> >>>>> >>>> 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] < >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=18>mailing list >>>>> >>> >>>>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>>> >>>> >>>PLEASE do read the posting guide >>>>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>>> <http://www.r-project.org/posting-guide.html> >>>>> >>> >>>>> >>>> >>>and provide commented, minimal, self-contained, reproducible code. >>>>> >>>> >>> >>>>> >>>> >>> >>>>> >>>> >>>______________________________________________ >>>>> >>>> >>>[hidden email] < >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=19>mailing list >>>>> >>> >>>>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>>> >>>> >>>PLEASE do read the posting guide >>>>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>>> <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] < >>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=20>mailing list >>>>> >>> >>>>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>>> >>>> >>>PLEASE do read the posting guide >>>>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>>> <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] <http://user/SendEmail.jtp?type=node&node=4657773&i=21>mailing >>>>> list >>>>> >>> >>>>> >>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>>> >>>> PLEASE do read the posting guide >>>>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>>> <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-tp4657074p4657773.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-tp4657074p4658133.html >>>>> >>> >>>>> >>>Sent from the R help mailing list archive at Nabble.com. >>>>> >>>? ? [[alternative HTML version deleted]] >>>>> >>> >>>>> >>>______________________________________________ >>>>> >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4659514&i=11>mailing list >>>> >>>>> >>> >>>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>>> >>>PLEASE do read the posting guide >>>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>>> >>>and provide commented, minimal, self-contained, reproducible code. >>>>> >>> >>>>> >>> >>>>> >> >>>>> > >>>>> >>>>> ______________________________________________ >>>>> [hidden email] <http://user/SendEmail.jtp?type=node&node=4659514&i=12>mailing list >>>> >>>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>>> PLEASE do read the posting guide >>>>> http://www.R-project.org/posting-guide.html<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-tp4657074p4659514.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-tp4657074p4659717.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. >>>> >>>> >>> >>> >>>??????????????????????????????????????????? ?????????????????????? ? >>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? >?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ???
f1<- function(dat){ stopifnot(nrow(dat)!=0) do.call(rbind,lapply(unique(dat$m1),function(m1) ?do.call(rbind,lapply(unique(dat$n1),function(n1) ?do.call(rbind,lapply(unique(dat$x1),function(x1) ?do.call(rbind,lapply(unique(dat$y1),function(y1) ? ?#do.call(rbind,lapply(0:m1,function(x1) ?#do.call(rbind,lapply(0:n1,function(y1) ?do.call(rbind,lapply((m1+2):(maxN-2-n1),function(m) ?do.call(rbind,lapply((n1+2):(maxN-m),function(n) ?do.call(rbind,lapply(x1:(x1+m-m1), function(x) ?do.call(rbind,lapply(y1:(y1+n-n1), function(y) ?expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) } f1(d4) #Error: nrow(dat) != 0 is not TRUE ?head(f1(d3),2) #? Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 #1??? 3??? 2??? 0??? 0??? 5??? 4??? 0??? 0 #2??? 3??? 2??? 0??? 0??? 5??? 4??? 0??? 1 A.K. ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Saturday, March 2, 2013 4:47 PM Subject: Re: [R] cumulative sum by group and under some criteria got it. is there a way to exit the loop if there is no row (empty data) in d3new? On Sat, Mar 2, 2013 at 3:38 PM, arun <smartpink111 at yahoo.com> wrote: HI,>d > >#?? m1 n1 x1 y1???? term1_p0???? term1_p1 >#1?? 3? 2? 0? 0 0.7737809375 0.2373046875 >#2?? 3? 2? 0? 1 0.0814506250 0.1582031250 >#3?? 3? 2? 0? 2 0.0021434375 0.0263671875 >#4?? 3? 2? 1? 0 0.1221759375 0.2373046875 >#5?? 3? 2? 1? 1 0.0128606250 0.1582031250 >#6?? 3? 2? 1? 2 0.0003384375 0.0263671875 >#7?? 3? 2? 2? 0 0.0064303125 0.0791015625 >#8?? 3? 2? 2? 1 0.0006768750 0.0527343750 >#9?? 3? 2? 2? 2 0.0000178125 0.0087890625 >#10? 3? 2? 3? 0 0.0001128125 0.0087890625 >#11? 3? 2? 3? 1 0.0000118750 0.0058593750 >#12? 3? 2? 3? 2 0.0000003125 0.0009765625 > >d2 >#?? m1 n1 x1 y1???? term1_p0???? term1_p1???? Qm???? Qn >#1?? 3? 2? 0? 0 0.7737809375 0.2373046875 0.0500 0.0810 >#2?? 3? 2? 0? 1 0.0814506250 0.1582031250 0.0560 0.6690 >#3?? 3? 2? 0? 2 0.0021434375 0.0263671875 0.0410 0.9680 >#4?? 3? 2? 1? 0 0.1221759375 0.2373046875 0.3150 0.3150 >#5?? 3? 2? 1? 1 0.0128606250 0.1582031250 0.5200 0.6580 >#6?? 3? 2? 1? 2 0.0003384375 0.0263671875 0.5360 0.9640 >#7?? 3? 2? 2? 0 0.0064303125 0.0791015625 0.4945 0.4945 >#8?? 3? 2? 2? 1 0.0006768750 0.0527343750 0.7815 0.7815 >#9?? 3? 2? 2? 2 0.0000178125 0.0087890625 0.9030 0.9650 >#10? 3? 2? 3? 0 0.0001128125 0.0087890625 0.5350 0.5350 >#11? 3? 2? 3? 1 0.0000118750 0.0058593750 0.8195 0.8195 >#12? 3? 2? 3? 2 0.0000003125 0.0009765625 0.9835 0.9835 > >?lst1<- split(d2,list(d2$m1,d2$n1)) >d3<- do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){ > >x[,9:14]<-NA; >x[,9:10][x$Qm<=c11,]<-cumsum(x[,5:6][x$Qm<=c11,]); >x[,11:12][x$Qn<=c12,]<-cumsum(x[,5:6][x$Qn<=c12,]); >x[,13:14]<-cumsum(x[,5:6]); >colnames(x)[9:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1"); >x1<-na.locf(x); >x1[,9:14][is.na(x1[,9:14])]<-0; >x1} >)) >?row.names(d3)<-1:nrow(d3) > >res1<-aggregate(.~m1+n1,data=d3[,c(1:2,9:12)],max) >res1 ># m1 n1 cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H >#1? 3? 2? 0.9795509? 0.6591797? 0.8959569? 0.4746094 >?d3New<- res1[res1[,4]<=0.60 & res1[,6]<0.40,] >d3New >#[1] m1???????? n1???????? cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H? # zero 0 rows >#<0 rows> (or 0-length row.names) >?library(plyr) >?d4<-join(d3New,d,by=c("m1","n1"),type="inner") >?d4 ># [1] m1???????? n1???????? cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H #zero 0 rows ># [7] x1???????? y1???????? term1_p0?? term1_p1? >#<0 rows> (or 0-length row.names) > >do.call(rbind,lapply(unique(d4$m1),function(m1) > >?do.call(rbind,lapply(unique(d4$n1),function(n1) >?do.call(rbind,lapply(unique(d4$x1),function(x1) >?do.call(rbind,lapply(unique(d4$y1),function(y1) >? >?#do.call(rbind,lapply(0:m1,function(x1) >?#do.call(rbind,lapply(0:n1,function(y1) >?do.call(rbind,lapply((m1+2):(maxN-2-n1),function(m) >?do.call(rbind,lapply((n1+2):(maxN-m),function(n) >?do.call(rbind,lapply(x1:(x1+m-m1), function(x) >?do.call(rbind,lapply(y1:(y1+n-n1), function(y) >?expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) >#NULL > > > > > >A.K. >________________________________ >From: Joanna Zhang <zjoanna2013 at gmail.com> >To: arun <smartpink111 at yahoo.com> >Sent: Saturday, March 2, 2013 3:59 PM > >Subject: Re: [R] cumulative sum by group and under some criteria > > >I can't figure out why there is an error for names (): Error in names(res2) <- c("m1", "n1", "x1", "y1", "m", "n", "x", "y") :? >? attempt to set an attribute on NULL > > > > >maxN<-9 >c11<-0.4 >c12<-0.4 >c1<-0.5 >c2<-0.5 > >p0L<-0.05 >p0H<-0.05 >p1L<-0.25 >p1H<-0.25 > >alpha<-0.20 >beta<-0.80 > >result <- vector("list",5) >result > >for (a in 2: (maxN-6)) { >d <- data.frame () >for ( m1 in a:a) { >? ? ? ?for (n1 in 2: (maxN-m1-4)){ >? ? ? ? ? for (x1 in 0: m1) { >? ? ? ? ? ? ? ?for (y1 in 0: n1) { > > >? ? ? ? ? ? ? ? ? ? ? ?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,term1_p0,term1_p1)) >}} >}} >colnames(d)<-c("m1","n1","x1","y1","term1_p0","term1_p1")? >tail(d) > >set.seed(8) >d1<-do.call(rbind,lapply(seq_len(nrow(d)),function(i){ >Pm<- rbeta(1000,0.2+d[i,"x1"],0.8+d[i,"m1"]-d[i,"x1"]); >Pn<- rbeta(1000,0.2+d[i,"y1"],0.8+d[i,"n1"]-d[i,"y1"]); >Fm<- ecdf(Pm); >Fn<- ecdf(Pn); >#Fmm<- Fm(d[i,"p11"]); >#Fnn<- Fn(d[i,"p12"]); > >Fmm<- Fm(p1L); >Fnn<- Fn(p1H); > >R<- (Fmm+Fnn)/2;? >Fmm_f<- max(R, Fmm); >Fnn_f<- min(R, Fnn); >Qm<- 1-Fmm_f; >Qn<- 1-Fnn_f; >data.frame(Qm,Qn)})) >d2<-cbind(d,d1) >head(d2) > > >library(zoo) >lst1<- split(d2,list(d$m1,d$n1))? >d2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){? >x[,9:14]<-NA;? >x[,9:10][x$Qm<=c11,]<-cumsum(x[,5:6][x$Qm<=c11,]);? >x[,11:12][x$Qn<=c12,]<-cumsum(x[,5:6][x$Qn<=c12,]); >x[,13:14]<-cumsum(x[,5:6]);? >colnames(x)[9:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1");? >x1<-na.locf(x);? >x1[,9:14][is.na(x1[,9:14])]<-0;? >x1} >))? >row.names(d2)<-1:nrow(d2)? >tail(d2) > >res1<-aggregate(.~m1+n1,data=d2[,c(1:2,9:12)],max) >head(res1) > >d3<-res1[res1[,4]<=0.60 & res1[,6]<0.40,]? >tail(d3) > >library(plyr)? >d4<- join(d3,d,by=c("m1","n1"),type="inner") >head(d4)? >tail(d4) > >res2<-do.call(rbind,lapply(unique(d4$m1),function(m1)? >do.call(rbind,lapply(unique(d4$n1),function(n1) >do.call(rbind,lapply(unique(d4$x1),function(x1) >do.call(rbind,lapply(unique(d4$y1),function(y1) > >#do.call(rbind,lapply(0:m1,function(x1)? >#do.call(rbind,lapply(0:n1,function(y1)? >do.call(rbind,lapply((m1+2):(maxN-2-n1),function(m)? >do.call(rbind,lapply((n1+2):(maxN-m),function(n)? >do.call(rbind,lapply(x1:(x1+m-m1), function(x)? >do.call(rbind,lapply(y1:(y1+n-n1), function(y) >expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) > >names(res2)<- c("m1","n1","x1","y1","m","n","x","y") >attr(res2,"out.attrs")<-NULL? >tail(res2) >result[[a]]<-res2 >} >result > > > > >On Sat, Mar 2, 2013 at 1:05 PM, arun <smartpink111 at yahoo.com> wrote: > >Alright, then go ahead and use a loop. >> >>A.K. >> >> >> >> >> >> >>________________________________ >>From: Joanna Zhang <zjoanna2013 at gmail.com> >>To: arun <smartpink111 at yahoo.com> >>Sent: Saturday, March 2, 2013 1:53 PM >> >>Subject: Re: [R] cumulative sum by group and under some criteria >> >> >>Thank you! When I run my real data, there was a warning massage 'lack of memory'. I am thinking that I can use a loop to run the same code for each part of the data. But I need to keep each output data "d2" and combine them.? >> >>for example: >> >>maxN<-9 >>c11<-0.4 >>c12<-0.4 >> >>p0L<-0.05 >>p0H<-0.05 >>p1L<-0.25 >>p1H<-0.25 >> >> >>for (a in 2: (maxN-6)) { >>d <- data.frame () >>for ( m1 in a:a) { >>? ? ?for (n1 in 2: (maxN-m1-4)){ >>? ? ? ? ? for (x1 in 0: m1) { >>? ? ? ? ? ? ? ?for (y1 in 0: n1) { >>? ? ? ? ? ? ? ? ? ? ? ?p11<- (x1/m1) >>? ? ? ? ? ? ? ? ? ? ? ?p12<- (y1/n1) ? ? ? ? ? ? ? ? ? ? ? ? ? ?? >> >>? ? ? ? ? ? ? ? ? ? ? ?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,p11,p12,term1_p0,term1_p1)) >>}} >>}} >>colnames(d)<-c("m1","n1","x1","y1","p11","p12","term1_p0","term1_p1")? >>tail(d) >> >>set.seed(8) >>d1<-do.call(rbind,lapply(seq_len(nrow(d)),function(i){ >>Pm<- rbeta(1000,0.2+d[i,"x1"],0.8+d[i,"m1"]-d[i,"x1"]); >>Pn<- rbeta(1000,0.2+d[i,"y1"],0.8+d[i,"n1"]-d[i,"y1"]); >>Fm<- ecdf(Pm); >>Fn<- ecdf(Pn); >> >>Fmm<- Fm(p1L); >>Fnn<- Fn(p1H); >> >>R<- (Fmm+Fnn)/2;? >>Fmm_f<- max(R, Fmm); >>Fnn_f<- min(R, Fnn); >>Qm<- 1-Fmm_f; >>Qn<- 1-Fnn_f; >>data.frame(Qm,Qn)})) >>d2<-cbind(d,d1) >>head(d2) ? ? ? ? ? ? ? ? ? ? ? ?# need to name "d2" using the value of a (or another way) to?distinguish from each other?? >>} >> >># combine all "d2" here >> >> >> >>On Fri, Mar 1, 2013 at 12:51 PM, arun <smartpink111 at yahoo.com> wrote: >> >> >>> >>>Hi, >>> >>>It seems like you haven't even looked at the output of d2, (first d2) >>>d2<- cbind(d,d1) >>>head(d2,3) >>>#? m1 n1 x1 y1 p11 p12?? term1_p0?? term1_p1??? Qm??? Qn >>>#1? 2? 2? 0? 0?? 0 0.0 0.81450625 0.31640625 1.000 1.000 >>>#2? 2? 2? 0? 1?? 0 0.5 0.08573750 0.21093750 0.666 0.666 >>>#3? 2? 2? 0? 2?? 0 1.0 0.00225625 0.03515625 0.500 0.500 >>>?ncol(d2)? # you have only 10 columns in d2 >>>#[1] 10 >>> >>> >>>#2nd problem: >>>You are splitting using d >>> >>>###############Your code >>> >>>library(zoo) >>>lst1<- split(d,list(d$m1,d$n1)) # should split byd2, because `d` doesn't have Qm or Qn columns? >>>d2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){ >>>x[,13:18]<-NA; #### this code was created for another dataset which obviously had 12 columns >>>x[,13:14][x$Qm<=c11,]<-cumsum(x[,11:12][x$Qm<=c11,]); #### here your term1_p0 and term1_p1 are columns 7 and 8. >>> >>>x[,15:16][x$Qn<=c12,]<-cumsum(x[,11:12][x$Qn<=c12,]); >>>x[,17:18]<-cumsum(x[,11:12]); >>>colnames(x)[13:18]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1"); >>>x1<-na.locf(x); >>>x1[,13:18][is.na(x1[,13:18])]<-0; >>>x1} >>>)) >>>########################################## >>> >>> >>>#corrected codes: >>> >>>lst1<- split(d2,list(d2$m1,d2$n1)) >>> >>>dNew<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){ >>>x[,11:16]<-NA; >>>x[,11:12][x$Qm<=c11,]<-cumsum(x[,7:8][x$Qm<=c11,]); >>>x[,13:14][x$Qn<=c12,]<-cumsum(x[,7:8][x$Qn<=c12,]); >>>x[,15:16]<-cumsum(x[,7:8]); >>>colnames(x)[11:16]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1"); >>>x1<-na.locf(x); >>>x1[,11:16][is.na(x1[,11:16])]<-0; >>>x1} >>>)) >>>?row.names(dNew)<- 1:nrow(dNew) >>>?head(dNew,3) >>>#? m1 n1 x1 y1 p11 p12?? term1_p0?? term1_p1??? Qm??? Qn cterm1_P0L cterm1_P1L >>>#1? 2? 2? 0? 0?? 0 0.0 0.81450625 0.31640625 1.000 1.000????????? 0????????? 0 >>>#2? 2? 2? 0? 1?? 0 0.5 0.08573750 0.21093750 0.666 0.666????????? 0????????? 0 >>>#3? 2? 2? 0? 2?? 0 1.0 0.00225625 0.03515625 0.500 0.500????????? 0????????? 0 >>>#? cterm1_P0H cterm1_P1H sumTerm1_p0 sumTerm1_p1 >>>#1????????? 0????????? 0?? 0.8145062?? 0.3164062 >>>#2????????? 0????????? 0?? 0.9002438?? 0.5273438 >>>#3????????? 0????????? 0?? 0.9025000?? 0.5625000 >>> >>> >>>A.K. >>> >>> >>>________________________________ >>> >>>From: Joanna Zhang <zjoanna2013 at gmail.com> >>>To: arun <smartpink111 at yahoo.com> >>>Sent: Friday, March 1, 2013 11:40 AM >>> >>>Subject: Re: [R] cumulative sum by group and under some criteria >>> >>> >>>Hi, why there is an error when I run the cumulative sum code below? >>> >>>Error in `[<-.data.frame`(`*tmp*`, , 16:21, value = NA) : >>>? new columns would leave holes after existing columns >>> >>> >>>maxN<-9 >>>c11<-0.4 >>>c12<-0.4 >>>c1<-0.5 >>>c2<-0.5 >>>p0L<-0.05 >>>p0H<-0.05 >>>p1L<-0.25 >>>p1H<-0.25 >>> >>>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)???????????????????????????? >>> >>>?????????????????????? 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,p11,p12,term1_p0,term1_p1)) >>>}} >>>}} >>>colnames(d)<-c("m1","n1","x1","y1","p11","p12","term1_p0","term1_p1") >>>d >>>tail(d) >>>set.seed(8) >>>d1<-do.call(rbind,lapply(seq_len(nrow(d)),function(i){ >>>Pm<- rbeta(1000,0.2+d[i,"x1"],0.8+d[i,"m1"]-d[i,"x1"]); >>>Pn<- rbeta(1000,0.2+d[i,"y1"],0.8+d[i,"n1"]-d[i,"y1"]); >>>Fm<- ecdf(Pm); >>>Fn<- ecdf(Pn); >>>Fmm<- Fm(d[i,"p11"]); >>>Fnn<- Fn(d[i,"p12"]); >>>R<- (Fmm+Fnn)/2; >>>Fmm_f<- max(R, Fmm); >>>Fnn_f<- min(R, Fnn); >>>Qm<- 1-Fmm_f; >>>Qn<- 1-Fnn_f; >>>data.frame(Qm,Qn)})) >>>d2<-cbind(d,d1) >>>d2 >>> >>>library(zoo) >>>lst1<- split(d,list(d$m1,d$n1)) >>>d2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){ >>>x[,13:18]<-NA; >>>x[,13:14][x$Qm<=c11,]<-cumsum(x[,11:12][x$Qm<=c11,]); >>>x[,15:16][x$Qn<=c12,]<-cumsum(x[,11:12][x$Qn<=c12,]); >>>x[,17:18]<-cumsum(x[,11:12]); >>>colnames(x)[13:18]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1"); >>>x1<-na.locf(x); >>>x1[,13:18][is.na(x1[,13:18])]<-0; >>>x1} >>>)) >>> >>> >>> >>> >>>On Tue, Feb 26, 2013 at 8:56 PM, arun <smartpink111 at yahoo.com> wrote: >>> >>>?? >>>> >>>> >>>> >>>>________________________________ >>>> From: Joanna Zhang <zjoanna2013 at gmail.com> >>>>To: arun <smartpink111 at yahoo.com> >>>>Sent: Tuesday, February 26, 2013 9:51 PM >>>> >>>>Subject: Re: [R] cumulative sum by group and under some criteria >>>> >>>> >>>> >>>>Hi, >>>> >>>># >>>>Pm2<-rbeta(1000, 0.2+1, 0.8+3) #obs4? >>>>this is for x=1, m=2 >>>> >>>>?length(Pm2) >>>>>#[1] 1000 >>>>> >>>>> >>>>>Pn2<-rbeta(1000, 0.2, 0.8+4) >>>>>?length(Pn2) >>>>>#[1] 1000 >>>>>Here, you are creating Pm2 or Pn2 from a single observation. >>>>> >>>>>In the code, it is creating 1000 values in total from the combination of values from x, m, >>>>>?Pm2<-rbeta(1000, 0.2+res2$x, 0.8+res2$m-res2$x) >>>>>?length(Pm2) >>>>>#[1] 1000 >>>>> >>>>>I don't get it here. What values of x and m are used here? I thought it should create 1000 observations for each combination of x,m in the data and this is what I want. >>>>> >>>>? >>>>A.K. >>>>> >>>>> >>>>> >>>>>----- Original Message ----- >>>>> >>>>>From: Zjoanna <Zjoanna2013 at gmail.com> >>>>>To: r-help at r-project.org >>>>>Cc: >>>>> >>>>>Sent: Tuesday, February 26, 2013 3:13 PM >>>>>Subject: Re: [R] cumulative sum by group and under some criteria >>>>> >>>>> >>>>>Hi Arun >>>>> >>>>>I noticed that the values of Fmm, Fnn, and other corresponding variables >>>>>are not correct, for example,? for the 4th obs after you run this code, the >>>>>Fmm is 0.40,? but if you use the x, m, y, n in the 4th row to calculate >>>>>them, the results are not consistent, same for the 5th obs. >>>>> >>>>>#check >>>>># >>>>>Pm2<-rbeta(1000, 0.2+1, 0.8+3) #obs4 >>>>>Pn2<-rbeta(1000, 0.2, 0.8+4) >>>>>Fm2<- ecdf(Pm2) >>>>>Fn2<- ecdf(Pn2) >>>>>Fmm2<-Fm2(1/4) >>>>>Fnn2<-Fn2(0) >>>>>Fmm2? #0.582 >>>>>Fnn2? ?#0 >>>>> >>>>> >>>>>Pm2<-rbeta(1000, 0.2+1, 0.8+3) #obs5 >>>>>Pn2<-rbeta(1000, 0.2+1, 0.8+3) >>>>>Fm2<- ecdf(Pm2) >>>>>Fn2<- ecdf(Pn2) >>>>>Fmm2<-Fm2(1/4) >>>>>Fnn2<-Fn2(1/4) >>>>>Fmm2 #0.404 >>>>>Fnn2? #0.416 >>>>> >>>>> >>>>> >>>>>On Sat, Feb 23, 2013 at 10:53 PM, arun kirshna [via R] < >>>>>ml-node+s789695n4659514h45 at n4.nabble.com> wrote: >>>>> >>>>>> Hi, >>>>>> d3<-structure(list(m1 = c(2, 3, 2), n1 = c(2, 2, 3), cterm1_P0L >>>>>> c(0.9025, >>>>>> 0.857375, 0.9025), cterm1_P1L = c(0.64, 0.512, 0.64), cterm1_P0H >>>>>> c(0.9025, >>>>>> 0.9025, 0.857375), cterm1_P1H = c(0.64, 0.64, 0.512)), .Names = c("m1", >>>>>> "n1", "cterm1_P0L", "cterm1_P1L", "cterm1_P0H", "cterm1_P1H"), row.names >>>>>> c(NA, >>>>>> 3L), class = "data.frame") >>>>>> d2<- data.frame() >>>>>> for (m1 in 2:3) { >>>>>>? ? ?for (n1 in 2:3) { >>>>>>? ? ? ? ?for (x1 in 0:(m1-1)) { >>>>>>? ? ? ? ? ? ?for (y1 in 0:(n1-1)) { >>>>>>? ? ? ? ?for (m in (m1+2): (7-n1)){ >>>>>>? ? ? ? ? ? ? ? for (n in (n1+2):(9-m)){ >>>>>>? ? ? ? ? ? ? ? for (x in x1:(x1+m-m1)){ >>>>>>? ? ? ? ? ? ? for(y in y1:(y1+n-n1)){ >>>>>>? d2<- rbind(d2,c(m1,n1,x1,y1,m,n,x,y)) >>>>>>? }}}}}}}} >>>>>> colnames(d2)<-c("m1","n1","x1","y1","m","n","x","y") >>>>>>? #or >>>>>> >>>>>> res1<-do.call(rbind,lapply(unique(d3$m1),function(m1) >>>>>>? do.call(rbind,lapply(unique(d3$n1),function(n1) >>>>>>? do.call(rbind,lapply(0:(m1-1),function(x1) >>>>>>? do.call(rbind,lapply(0:(n1-1),function(y1) >>>>>>? do.call(rbind,lapply((m1+2):(7-n1),function(m) >>>>>>? do.call(rbind,lapply((n1+2):(9-m),function(n) >>>>>>? do.call(rbind,lapply(x1:(x1+m-m1), function(x) >>>>>>? do.call(rbind,lapply(y1:(y1+n-n1), function(y) >>>>>>? expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) >>>>>>? names(res1)<- c("m1","n1","x1","y1","m","n","x","y") >>>>>>? attr(res1,"out.attrs")<-NULL >>>>>> res1[]<- sapply(res1,as.integer) >>>>>> >>>>>> library(plyr) >>>>>> res2<- join(res1,d3,by=c("m1","n1"),type="inner") >>>>>> >>>>>> #Assuming that these are the values you used: >>>>>> >>>>>> p0L<-0.05 >>>>>> p0H<-0.05 >>>>>> p1L<-0.20 >>>>>> p1H<-0.20 >>>>>> res2<- within(res2,{p1<- x/m; p2<- y/n;term2_p0<-dbinom(x1,m1, p0L, >>>>>> log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* >>>>>> dbinom(y-y1,n-n1,p0H, log=FALSE);term2_p1<- dbinom(x1,m1, p1L, log=FALSE)* >>>>>> dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* >>>>>> dbinom(y-y1,n-n1,p1H, log=FALSE);Pm2<-rbeta(240, 0.2+x, >>>>>> 0.8+m-x);Pn2<-rbeta(240, 0.2+y, 0.8+n-y)}) >>>>>> Fm2<- ecdf(res2$Pm2) >>>>>> Fn2<- ecdf(res2$Pn2) >>>>>> >>>>>> res3<- within(res2,{Fmm2<-Fm2(p1);Fnn2<- Fn2(p2);R2<- (Fmm2+Fnn2)/2}) #not >>>>>> sure about this step especially the Fm2() or Fn2() >>>>>> res3$Fmm_f2<-apply(res3[,c("R2","Fmm2")],1,min) >>>>>>? res3$Fnn_f2<-apply(res3[,c("R2","Fnn2")],1,max) >>>>>> res3<- within(res3,{Qm2<- 1-Fmm_f2;Qn2<- 1-Fnn_f2}) >>>>>> head(res3) >>>>>> #? m1 n1 x1 y1 m n x y cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H >>>>>> Pn2 >>>>>> #1? 2? 2? 0? 0 4 4 0 0? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>>>> 0.001084648 >>>>>> #2? 2? 2? 0? 0 4 4 0 1? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>>>> 0.504593909 >>>>>> #3? 2? 2? 0? 0 4 4 0 2? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>>>> 0.541379357 >>>>>> #4? 2? 2? 0? 0 4 4 1 0? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>>>> 0.138947785 >>>>>> #5? 2? 2? 0? 0 4 4 1 1? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>>>> 0.272364957 >>>>>> #6? 2? 2? 0? 0 4 4 1 2? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>>>> 0.761635059 >>>>>> #? ? ? ? ? ?Pm2? ?term2_p1? ? ?term2_p0? ?p2? ?p1? ? ? ? R2? ? ? Fnn2 Fmm2 >>>>>> #1 1.212348e-05 0.16777216 0.6634204313 0.00 0.00 0.0000000 0.0000000? 0.0 >>>>>> #2 1.007697e-03 0.08388608 0.0698337296 0.25 0.00 0.1791667 0.3583333? 0.0 >>>>>> #3 1.106946e-05 0.01048576 0.0018377297 0.50 0.00 0.3479167 0.6958333? 0.0 >>>>>> # 2.086758e-01 0.08388608 0.0698337296 0.00 0.25 0.2000000 0.0000000? 0.4 >>>>>> #5 2.382179e-01 0.04194304 0.0073509189 0.25 0.25 0.3791667 0.3583333? 0.4 >>>>>> #6 4.494673e-01 0.00524288 0.0001934452 0.50 0.25 0.5479167 0.6958333? 0.4 >>>>>> #? ? ?Fmm_f2? ? Fnn_f2? ? ? ?Qn2? ? ? ?Qm2 >>>>>> #1 0.0000000 0.0000000 1.0000000 1.0000000 >>>>>> #2 0.0000000 0.3583333 0.6416667 1.0000000 >>>>>> #3 0.0000000 0.6958333 0.3041667 1.0000000 >>>>>> #4 0.2000000 0.2000000 0.8000000 0.8000000 >>>>>> #5 0.3791667 0.3791667 0.6208333 0.6208333 >>>>>> #6 0.4000000 0.6958333 0.3041667 0.6000000 >>>>>> >>>>>> >>>>>> A.K. >>>>>> >>>>>> >>>>>> >>>>>> >>>>>> >>>>>> >>>>>> >>>>>> ________________________________ >>>>>> From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=0>> >>>>>> >>>>>> To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=1>> >>>>> >>>>>> >>>>>> Sent: Friday, February 22, 2013 11:02 AM >>>>>> Subject: Re: [R] cumulative sum by group and under some criteria >>>>>> >>>>>> >>>>>> Thanks!? Then I need to create new variables based on the res2.? I can't >>>>>> find Fmm_f1, Fnn_f2, R2, Qm2, Qn2 until? running the code several times and >>>>>> the values of Fnn_f2, Fmm_f2 are correct. >>>>>> >>>>>> attach(res2) >>>>>> res2$p1<-x/m >>>>>> res2$p2<-y/n >>>>>> res2$term2_p0 <- dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, >>>>>> log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, >>>>>> log=FALSE) >>>>>> res2$term2_p1 <- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, >>>>>> log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, >>>>>> log=FALSE) >>>>>> Pm2<-rbeta(1000, 0.2+x, 0.8+m-x) >>>>>> Fm2<-ecdf(Pm2) >>>>>> res2$Fmm2<-Fm2(x/m)? #not correct, it comes out after running code two >>>>>> times >>>>>> Pn2<-rbeta(1000, 0.2+y, 0.8+n-y) >>>>>> Fn2<-ecdf(Pn2) >>>>>> res2$Fnn2<-Fn2(y/n) >>>>>> res2$R2<-(Fmm2+Fnn2)/2 >>>>>> res2$Fmm_f2<-min(R2,Fmm2)? # not correct >>>>>> res2$Fnn_f2<-max(R2,Fnn2) >>>>>> res2$Qm2<-(1-Fmm_f2) >>>>>> res2$Qn2<-(1-Fnn_f2) >>>>>> detach(res2) >>>>>> res2 >>>>>> head(res2) >>>>>> >>>>>> >>>>>> >>>>>> On Tue, Feb 19, 2013 at 4:09 PM, arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=2>> >>>>> >>>>>> wrote: >>>>>> >>>>>> Hi, >>>>>> >>>>>> > >>>>>> >""suppose that I have a dataset 'd' >>>>>> >? ?m1? n1? ? A? ? ? ? ? ? ?B? ? ?C? ? ? ? ?D >>>>>> >1? 2? 2? ?0.902500? ? 0.640? ?0.9025? ? 0.64 >>>>>> >2? 3? 2? ?0.857375? ? 0.512? ?0.9025? ? 0.64 >>>>>> >I want to add? x1 (from 0 to m1), y1(from 0 to n1), m (range from >>>>>> >m1+2 to 7-n1), n(from n1+2 to 9-m), x (x1 to x1+m-m1), y(y1 to y1+n-n1), >>>>>> expanding to another dataset 'd2' based on each row (combination of m1 >>>>>> >and n1)"" >>>>>> > >>>>>> > >>>>>> >Try: >>>>>> > >>>>>> > >>>>>> > d<-read.table(text=" >>>>>> > >>>>>> >m1? n1? ? A? ? ? ? ? ? ?B? ? ?C? ? ? ? ?D >>>>>> >1? 2? 2? ?0.902500? ? 0.640? ?0.9025? ? 0.64 >>>>>> >2? 3? 2? ?0.857375? ? 0.512? ?0.9025? ? 0.64 >>>>>> >",sep="",header=TRUE) >>>>>> > >>>>>> >vec1<- paste(d[,1],d[,2],d[,3],d[,4],d[,5],d[,6]) >>>>>> >res1<- do.call(rbind,lapply(vec1,function(m1) >>>>>> do.call(rbind,lapply(0:(as.numeric(substr(m1,1,1))),function(x1) >>>>>> do.call(rbind,lapply(0:(as.numeric(substr(m1,3,3))),function(y1) >>>>>> do.call(rbind,lapply((as.numeric(substr(m1,1,1))+2):(7-as.numeric(substr(m1,3,3))),function(m) >>>>>> do.call(rbind,lapply((as.numeric(substr(m1,3,3))+2):(9-m),function(n) >>>>>> > >>>>>> > do.call(rbind,lapply(x1:(x1+m-as.numeric(substr(m1,1,1))), function(x) >>>>>> > do.call(rbind,lapply(y1:(y1+n-as.numeric(substr(m1,3,3))), function(y) >>>>>> > expand.grid(m1,x1,y1,m,n,x,y)) ))))))))))))) >>>>>> > >>>>>> names(res1)<- c("group","x1","y1","m","n","x","y") >>>>>> >>>>>> > res1$m1<- NA; res1$n1<- NA; res1$A<- NA; res1$B<- NA; res1$C<- NA;res1$D >>>>>> <- NA >>>>>> >res1[,8:13]<-do.call(rbind,lapply(strsplit(as.character(res1$group)," >>>>>> "),as.numeric)) >>>>>> >res2<- res1[,c(8:9,2:7,10:13)] >>>>>> > >>>>>> > >>>>>> > head(res2) >>>>>> >#? m1 n1 x1 y1 m n x y? ? ? A? ? B? ? ? C? ? D >>>>>> >#1? 2? 2? 0? 0 4 4 0 0 0.9025 0.64 0.9025 0.64 >>>>>> >#2? 2? 2? 0? 0 4 4 0 1 0.9025 0.64 0.9025 0.64 >>>>>> >#3? 2? 2? 0? 0 4 4 0 2 0.9025 0.64 0.9025 0.64 >>>>>> >#4? 2? 2? 0? 0 4 4 1 0 0.9025 0.64 0.9025 0.64 >>>>>> >#5? 2? 2? 0? 0 4 4 1 1 0.9025 0.64 0.9025 0.64 >>>>>> >#6? 2? 2? 0? 0 4 4 1 2 0.9025 0.64 0.9025 0.64 >>>>>> > >>>>>> > >>>>>> > >>>>>> > >>>>>> > >>>>>> > >>>>>> >________________________________ >>>>>> >From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=3>> >>>>>> >>>>>> >To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=4>> >>>>> >>>>>> >>>>>> >Sent: Tuesday, February 19, 2013 11:43 AM >>>>>> > >>>>>> >Subject: Re: [R] cumulative sum by group and under some criteria >>>>>> > >>>>>> > >>>>>> >Thanks. I can get the data I expected (get rid of the m1=3, n1=3) using >>>>>> the join and 'inner' code, but just curious about the way to expand the >>>>>> data. There should be a way to expand the data based on each row >>>>>> (combination of the variables), unique(d3$m1 & d3$n1) ?. >>>>>> > >>>>>> >or is there a way to use 'data.frame' and 'for' loop to expand directly >>>>>> from the data? like res1<-data.frame (d3) for () {.... >>>>>> > >>>>>> > >>>>>> >On Tue, Feb 19, 2013 at 9:55 AM, arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=5>> >>>>> >>>>>> wrote: >>>>>> > >>>>>> >If you can provide me the output that you expect with all the rows of the >>>>>> combination in the res2, I can take a look. >>>>>> >> >>>>>> >> >>>>>> >> >>>>>> >> >>>>>> >> >>>>>> >> >>>>>> >>________________________________ >>>>>> >> >>>>>> >>From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=6>> >>>>>> >>>>>> >>To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=7>> >>>>> >>>>>> >>>>>> >> >>>>>>? >>Sent: Tuesday, February 19, 2013 10:42 AM >>>>>> >> >>>>>> >>Subject: Re: [R] cumulative sum by group and under some criteria >>>>>> >> >>>>>> >> >>>>>> >>Thanks. But I thougth the expanded dataset 'res1' should not have >>>>>> combination of m1=3 and n1=3 because it is based on dataset 'd3' which >>>>>> doesn't have m1=3 and n1=3, right?> >>>>>> >>>In the example that you provided: >>>>>> >>> (m1+2):(maxN-(n1+2)) >>>>>> >>>#[1] 5 >>>>>> >>> (n1+2):(maxN-5) >>>>>> >>>#[1] 4 >>>>>> >>>#Suppose >>>>>> >>> x1<- 4 >>>>>> >>> y1<- 2 >>>>>> >>> x1:(x1+5-m1) >>>>>> >>>#[1] 4 5 6 >>>>>> >>> y1:(y1+4-n1) >>>>>> >>>#[1] 2 3 4 >>>>>> >>> >>>>>> >>> datnew<-expand.grid(5,4,4:6,2:4) >>>>>> >>> colnames(datnew)<- c("m","n","x","y") >>>>>> >>>datnew<-within(datnew,{p1<- x/m;p2<-y/n}) >>>>>> >>>res<-cbind(datnew,d2[rep(1:nrow(d2),nrow(datnew)),]) >>>>>> >>> row.names(res)<- 1:nrow(res) >>>>>> >>> res >>>>>> >>>#? m n x y? ?p2? p1 m1 n1 cterm1_P1L cterm1_P0H >>>>>> >>>#1 5 4 4 2 0.50 0.8? 3? 2? ? 0.00032? ? ?0.0025 >>>>>> >>>#2 5 4 5 2 0.50 1.0? 3? 2? ? 0.00032? ? ?0.0025 >>>>>> >>>#3 5 4 6 2 0.50 1.2? 3? 2? ? 0.00032? ? ?0.0025 >>>>>> >>>#4 5 4 4 3 0.75 0.8? 3? 2? ? 0.00032? ? ?0.0025 >>>>>> >>>#5 5 4 5 3 0.75 1.0? 3? 2? ? 0.00032? ? ?0.0025 >>>>>> >>>#6 5 4 6 3 0.75 1.2? 3? 2? ? 0.00032? ? ?0.0025 >>>>>> >>>#7 5 4 4 4 1.00 0.8? 3? 2? ? 0.00032? ? ?0.0025 >>>>>> >>>#8 5 4 5 4 1.00 1.0? 3? 2? ? 0.00032? ? ?0.0025 >>>>>> >>>#9 5 4 6 4 1.00 1.2? 3? 2? ? 0.00032? ? ?0.0025 >>>>>> >>> >>>>>> >>>A.K. >>>>>> >>> >>>>>> >>> >>>>>> >>> >>>>>> >>> >>>>>> >>> >>>>>> >>>----- Original Message ----- >>>>>> >>>From: Zjoanna <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=8>> >>>>>> >>>>>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=9> >>>>> >>>>>> >>>Cc: >>>>>> >>> >>>>>> >>>Sent: Sunday, February 10, 2013 6:04 PM >>>>>> >>>Subject: Re: [R] cumulative sum by group and under some criteria >>>>>> >>> >>>>>> >>> >>>>>> >>>Hi, >>>>>> >>>How to expand or loop for one variable n based on another variable? for >>>>>> >>>example, I want to add m (from m1 to maxN- n1-2) and for each m, I want >>>>>> to >>>>>> >>>add n (n1+2 to maxN-m), and similarly add x and y, then I need to do >>>>>> some >>>>>> >>>calculations. >>>>>> >>> >>>>>> >>>d3<-data.frame(d2) >>>>>> >>>? ? for (m in (m1+2):(maxN-(n1+2)){ >>>>>> >>>? ? ? ?for (n in (n1+2):(maxN-m)){ >>>>>> >>>? ? ? ? ? ? ?for (x in x1:(x1+m-m1)){ >>>>>> >>>? ? ? ? ? ? ? ? ? for (y in y1:(y1+n-n1)){ >>>>>> >>>? ? ? ? ? ? ? ? ? ? ? ?p1<- x/m >>>>>> >>>? ? ? ? ? ? ? ? ? ? ? ?p2<- y/n >>>>>> >>>}}}} >>>>>> >>> >>>>>> >>>On Thu, Feb 7, 2013 at 12:16 AM, arun kirshna [via R] < >>>>>>? >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4659514&i=10>> >>>>> >>>>>> wrote: >>>>>> >>> >>>>>> >>>> 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]< >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=0>> >>>>>> >>>> >>>>>> >>>> To: arun <[hidden email]< >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=1>> >>>>>> >>> >>>>>> >>>> >>>>>> >>>> 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]< >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=2>> >>>>>> >>> >>>>>> >>>> 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]< >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=3>> >>>>>> >>>> >>>>>> >>>> >To: arun <[hidden email]< >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=4>> >>>>>> >>> >>>>>> >>>> >>>>>> >>>> >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]< >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=5>> >>>>>> >>> >>>>>> >>>> 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]< >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=6>> >>>>>> >>>> >>>>>> >>>> >>To: arun <[hidden email]< >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=7>> >>>>>> >>> >>>>>> >>>> >>>>>> >>>> >>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]< >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=8>> >>>>>> >>> >>>>>> >>>> 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]< >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=9>";;;;;;;; >>>>>> >>>> <[hidden email] < >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=10>> >>>>>> >>>> >>>To: [hidden email]< >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=11> >>>>>> >>>>? >>>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]< >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=12>";;;;;;;; >>>>>> >>>> <[hidden email] < >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=13>> >>>>>> >>>> >>>To: [hidden email]< >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=14> >>>>>> >>>>? >>>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]< >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=15>> >>>>>> >>>> >>>>>> >>>> >>>To: [hidden email]< >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=16> >>>>>> >>> >>>>>> >>>> >>>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] < >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=17>> >>>>>> >>> >>>>>> >>>> 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] < >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=18>mailing list >>>>>> >>> >>>>>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>>>> >>>> >>>PLEASE do read the posting guide >>>>>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>>>> <http://www.r-project.org/posting-guide.html> >>>>>> >>> >>>>>> >>>> >>>and provide commented, minimal, self-contained, reproducible code. >>>>>> >>>> >>> >>>>>> >>>> >>> >>>>>> >>>> >>>______________________________________________ >>>>>> >>>> >>>[hidden email] < >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=19>mailing list >>>>>> >>> >>>>>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>>>> >>>> >>>PLEASE do read the posting guide >>>>>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>>>> <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] < >>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=20>mailing list >>>>>> >>> >>>>>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>>>> >>>> >>>PLEASE do read the posting guide >>>>>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>>>> <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] <http://user/SendEmail.jtp?type=node&node=4657773&i=21>mailing >>>>>> list >>>>>> >>> >>>>>> >>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>>>> >>>> PLEASE do read the posting guide >>>>>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>>>> <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-tp4657074p4657773.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-tp4657074p4658133.html >>>>>> >>> >>>>>> >>>Sent from the R help mailing list archive at Nabble.com. >>>>>> >>>? ? [[alternative HTML version deleted]] >>>>>> >>> >>>>>> >>>______________________________________________ >>>>>> >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4659514&i=11>mailing list >>>>> >>>>>> >>> >>>>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>>>> >>>PLEASE do read the posting guide >>>>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>>>> >>>and provide commented, minimal, self-contained, reproducible code. >>>>>> >>> >>>>>> >>> >>>>>> >> >>>>>> > >>>>>> >>>>>> ______________________________________________ >>>>>> [hidden email] <http://user/SendEmail.jtp?type=node&node=4659514&i=12>mailing list >>>>> >>>>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>>>> PLEASE do read the posting guide >>>>>> http://www.R-project.org/posting-guide.html<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-tp4657074p4659514.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-tp4657074p4659717.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. >>>>> >>>>> >>>> >>>> >>>>??????????????????????????????????????????? ?????????????????????? ? >>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? >>?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ??? >
res<- do.call(rbind,lapply(result,function(x) x[which.max(x$N),])) res #??? m1 n1 x1 y1 m n x y? N #668? 2? 2? 2? 2 4 6 4 6 28 #144? 3? 3? 3? 3 5 5 5 5 32 A.K. ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Saturday, March 2, 2013 8:52 PM Subject: Re: [R] cumulative sum by group and under some criteria this is an example of my data, the max value of N will not always be the last row of each data. It could be in any position in the data. On Sat, Mar 2, 2013 at 7:39 PM, arun <smartpink111 at yahoo.com> wrote:> > >Alight, but when you say the max value of N, it is the last row in the results[[3]] and results[[2]] >tail(result[[2]],3) >??? m1 n1 x1 y1 m n x y? N >709? 2? 2? 2? 2 6 4 6 2 26 >710? 2? 2? 2? 2 6 4 6 3 27 >711? 2? 2? 2? 2 6 4 6 4 28 >> tail(result[[3]],3) >??? m1 n1 x1 y1 m n x y? N >142? 3? 3? 3? 3 5 5 5 3 30 >143? 3? 3? 3? 3 5 5 5 4 31 >144? 3? 3? 3? 3 5 5 5 5 32 > > >So, are you saying that you want only row #711 and #144? as the final result. > > >A.K. >________________________________ >From: Joanna Zhang <zjoanna2013 at gmail.com> >To: arun <smartpink111 at yahoo.com> >Sent: Saturday, March 2, 2013 7:37 PM > >Subject: Re: [R] cumulative sum by group and under some criteria > > >Change maxN to 10, you will get result[2] and [3] > > > >On Sat, Mar 2, 2013 at 6:35 PM, arun <smartpink111 at yahoo.com> wrote: > > >> >>AFter running this code, I get result[[1]], [[3]],[[4]], [[5]] as NULL.? The only list element is [[2]].? what do you mean by putting all data result[] in one dataset? >> >> >> >> >> >>________________________________ >>From: Joanna Zhang <zjoanna2013 at gmail.com> >>To: arun <smartpink111 at yahoo.com> >>Sent: Saturday, March 2, 2013 7:27 PM >> >>Subject: Re: [R] cumulative sum by group and under some criteria >> >> >>Hi, is there a way to put all data "result[] " ?in one data set and extract the row with ?the max value of N? I am almost there. Really appreciate your help. I have to finish it this weekend. >> >> >> >>maxN<-9 >>c11<-0.4 >>c12<-0.4 >>c1<-0.5 >>c2<-0.5 >> >>p0L<-0.05 >>p0H<-0.05 >>p1L<-0.25 >>p1H<-0.25 >> >>result <- vector("list",5) >> >>for (a in 2: (maxN-6)) { >>d <- data.frame () >>for ( m1 in a:a) { >>? ? ? ?for (n1 in 2: (maxN-m1-4)){ >>? ? ? ? ? for (x1 in 0: m1) { >>? ? ? ? ? ? ? ?for (y1 in 0: n1) { >>? ? ? ? ? ? ? ? ? ? ? ?#p11<- (x1/m1) >>? ? ? ? ? ? ? ? ? ? ? ?#p12<- (y1/n1) ? ? ? ? ? ? ? ? ? ? ? ? ? ?? >> >>? ? ? ? ? ? ? ? ? ? ? ?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,term1_p0,term1_p1)) >>}} >>}} >>colnames(d)<-c("m1","n1","x1","y1","term1_p0","term1_p1")? >>tail(d) >> >>########## add Qm Qn ################################## >>set.seed(8) >>d1<-do.call(rbind,lapply(seq_len(nrow(d)),function(i){ >>Pm<- rbeta(1000,0.2+d[i,"x1"],0.8+d[i,"m1"]-d[i,"x1"]); >>Pn<- rbeta(1000,0.2+d[i,"y1"],0.8+d[i,"n1"]-d[i,"y1"]); >>Fm<- ecdf(Pm); >>Fn<- ecdf(Pn); >> >>Fmm<- Fm(p1L); >>Fnn<- Fn(p1H); >> >>R<- (Fmm+Fnn)/2;? >>Fmm_f<- max(R, Fmm); >>Fnn_f<- min(R, Fnn); >>Qm<- 1-Fmm_f; >>Qn<- 1-Fnn_f; >>data.frame(Qm,Qn)})) >>d2<-cbind(d,d1) >>head(d2) >> >> >>library(zoo) >>lst1<- split(d2,list(d$m1,d$n1))? >>d2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){? >>x[,9:14]<-NA;? >>x[,9:10][x$Qm<=c11,]<-cumsum(x[,5:6][x$Qm<=c11,]);? >>x[,11:12][x$Qn<=c12,]<-cumsum(x[,5:6][x$Qn<=c12,]); >>x[,13:14]<-cumsum(x[,5:6]);? >>colnames(x)[9:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1");? >>x1<-na.locf(x);? >>x1[,9:14][is.na(x1[,9:14])]<-0;? >>x1} >>))? >>row.names(d2)<-1:nrow(d2)? >>tail(d2) >> >>res1<-aggregate(.~m1+n1,data=d2[,c(1:2,9:12)],max) >>head(res1) >> >>d3<-res1[res1[,4]<=0.60 & res1[,6]<0.40,]? >>tail(d3) >> >>library(plyr)? >>d4<- join(d3,d,by=c("m1","n1"),type="inner") >>head(d4)? >> >>if ( nrow(d4)==0) break >> >>res2<-do.call(rbind,lapply(unique(d4$m1),function(m1)? >>do.call(rbind,lapply(unique(d4$n1),function(n1) >>do.call(rbind,lapply(unique(d4$x1),function(x1) >>do.call(rbind,lapply(unique(d4$y1),function(y1) >> >>#do.call(rbind,lapply(0:m1,function(x1)? >>#do.call(rbind,lapply(0:n1,function(y1)? >>do.call(rbind,lapply((m1+2):(maxN-2-n1),function(m)? >>do.call(rbind,lapply((n1+2):(maxN-m),function(n)? >>do.call(rbind,lapply(x1:(x1+m-m1), function(x)? >>do.call(rbind,lapply(y1:(y1+n-n1), function(y) >>expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) >> >> >>names(res2)<- c("m1","n1","x1","y1","m","n","x","y") >>attr(res2,"out.attrs")<-NULL? >>tail(res2) >>res2$N<-res2$m1+res2$n1+res2$x1+res2$y1+res2$m+res2$n+res2$x+res2$y >>result[[a]]<-res2 >> >>} >> >>result >> >> >> >> >>On Sat, Mar 2, 2013 at 4:56 PM, Joanna Zhang <zjoanna2013 at gmail.com> wrote: >> >>I just used if (nrow(d4)==0) break to solve the problem. >>> >>> >>> >>>On Sat, Mar 2, 2013 at 4:00 PM, arun <smartpink111 at yahoo.com> wrote: >>> >>> >>>> >>>>f1<- function(dat){ >>>>stopifnot(nrow(dat)!=0) >>>>do.call(rbind,lapply(unique(dat$m1),function(m1) >>>>?do.call(rbind,lapply(unique(dat$n1),function(n1) >>>>?do.call(rbind,lapply(unique(dat$x1),function(x1) >>>>?do.call(rbind,lapply(unique(dat$y1),function(y1) >>>> >>>>? >>>>?#do.call(rbind,lapply(0:m1,function(x1) >>>>?#do.call(rbind,lapply(0:n1,function(y1) >>>>?do.call(rbind,lapply((m1+2):(maxN-2-n1),function(m) >>>>?do.call(rbind,lapply((n1+2):(maxN-m),function(n) >>>>?do.call(rbind,lapply(x1:(x1+m-m1), function(x) >>>>?do.call(rbind,lapply(y1:(y1+n-n1), function(y) >>>>?expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) >>>>} >>>> >>>>f1(d4) >>>>#Error: nrow(dat) != 0 is not TRUE >>>>?head(f1(d3),2) >>>>#? Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 >>>>#1??? 3??? 2??? 0??? 0??? 5??? 4??? 0??? 0 >>>>#2??? 3??? 2??? 0??? 0??? 5??? 4??? 0??? 1 >>>> >>>>A.K. >>>> >>>> >>>> >>>>________________________________ >>>>From: Joanna Zhang <zjoanna2013 at gmail.com> >>>>To: arun <smartpink111 at yahoo.com> >>>>Sent: Saturday, March 2, 2013 4:47 PM >>>> >>>>Subject: Re: [R] cumulative sum by group and under some criteria >>>> >>>> >>>>got it. is there a way to exit the loop if there is no row (empty data) in d3new? >>>> >>>> >>>> >>>>On Sat, Mar 2, 2013 at 3:38 PM, arun <smartpink111 at yahoo.com> wrote: >>>> >>>>HI, >>>>>d >>>>> >>>>>#?? m1 n1 x1 y1???? term1_p0???? term1_p1 >>>>>#1?? 3? 2? 0? 0 0.7737809375 0.2373046875 >>>>>#2?? 3? 2? 0? 1 0.0814506250 0.1582031250 >>>>>#3?? 3? 2? 0? 2 0.0021434375 0.0263671875 >>>>>#4?? 3? 2? 1? 0 0.1221759375 0.2373046875 >>>>>#5?? 3? 2? 1? 1 0.0128606250 0.1582031250 >>>>>#6?? 3? 2? 1? 2 0.0003384375 0.0263671875 >>>>>#7?? 3? 2? 2? 0 0.0064303125 0.0791015625 >>>>>#8?? 3? 2? 2? 1 0.0006768750 0.0527343750 >>>>>#9?? 3? 2? 2? 2 0.0000178125 0.0087890625 >>>>>#10? 3? 2? 3? 0 0.0001128125 0.0087890625 >>>>>#11? 3? 2? 3? 1 0.0000118750 0.0058593750 >>>>>#12? 3? 2? 3? 2 0.0000003125 0.0009765625 >>>>> >>>>>d2 >>>>>#?? m1 n1 x1 y1???? term1_p0???? term1_p1???? Qm???? Qn >>>>>#1?? 3? 2? 0? 0 0.7737809375 0.2373046875 0.0500 0.0810 >>>>>#2?? 3? 2? 0? 1 0.0814506250 0.1582031250 0.0560 0.6690 >>>>>#3?? 3? 2? 0? 2 0.0021434375 0.0263671875 0.0410 0.9680 >>>>>#4?? 3? 2? 1? 0 0.1221759375 0.2373046875 0.3150 0.3150 >>>>>#5?? 3? 2? 1? 1 0.0128606250 0.1582031250 0.5200 0.6580 >>>>>#6?? 3? 2? 1? 2 0.0003384375 0.0263671875 0.5360 0.9640 >>>>>#7?? 3? 2? 2? 0 0.0064303125 0.0791015625 0.4945 0.4945 >>>>>#8?? 3? 2? 2? 1 0.0006768750 0.0527343750 0.7815 0.7815 >>>>>#9?? 3? 2? 2? 2 0.0000178125 0.0087890625 0.9030 0.9650 >>>>>#10? 3? 2? 3? 0 0.0001128125 0.0087890625 0.5350 0.5350 >>>>>#11? 3? 2? 3? 1 0.0000118750 0.0058593750 0.8195 0.8195 >>>>>#12? 3? 2? 3? 2 0.0000003125 0.0009765625 0.9835 0.9835 >>>>> >>>>>?lst1<- split(d2,list(d2$m1,d2$n1)) >>>>>d3<- do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){ >>>>> >>>>>x[,9:14]<-NA; >>>>>x[,9:10][x$Qm<=c11,]<-cumsum(x[,5:6][x$Qm<=c11,]); >>>>>x[,11:12][x$Qn<=c12,]<-cumsum(x[,5:6][x$Qn<=c12,]); >>>>>x[,13:14]<-cumsum(x[,5:6]); >>>>>colnames(x)[9:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1"); >>>>>x1<-na.locf(x); >>>>>x1[,9:14][is.na(x1[,9:14])]<-0; >>>>>x1} >>>>>)) >>>>>?row.names(d3)<-1:nrow(d3) >>>>> >>>>>res1<-aggregate(.~m1+n1,data=d3[,c(1:2,9:12)],max) >>>>>res1 >>>>># m1 n1 cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H >>>>>#1? 3? 2? 0.9795509? 0.6591797? 0.8959569? 0.4746094 >>>>>?d3New<- res1[res1[,4]<=0.60 & res1[,6]<0.40,] >>>>>d3New >>>>>#[1] m1???????? n1???????? cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H? # zero 0 rows >>>>>#<0 rows> (or 0-length row.names) >>>>>?library(plyr) >>>>>?d4<-join(d3New,d,by=c("m1","n1"),type="inner") >>>>>?d4 >>>>># [1] m1???????? n1???????? cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H #zero 0 rows >>>>># [7] x1???????? y1???????? term1_p0?? term1_p1? >>>>>#<0 rows> (or 0-length row.names) >>>>> >>>>>do.call(rbind,lapply(unique(d4$m1),function(m1) >>>>> >>>>>?do.call(rbind,lapply(unique(d4$n1),function(n1) >>>>>?do.call(rbind,lapply(unique(d4$x1),function(x1) >>>>>?do.call(rbind,lapply(unique(d4$y1),function(y1) >>>>>? >>>>>?#do.call(rbind,lapply(0:m1,function(x1) >>>>>?#do.call(rbind,lapply(0:n1,function(y1) >>>>>?do.call(rbind,lapply((m1+2):(maxN-2-n1),function(m) >>>>>?do.call(rbind,lapply((n1+2):(maxN-m),function(n) >>>>>?do.call(rbind,lapply(x1:(x1+m-m1), function(x) >>>>>?do.call(rbind,lapply(y1:(y1+n-n1), function(y) >>>>>?expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) >>>>>#NULL >>>>> >>>>> >>>>> >>>>> >>>>> >>>>>A.K. >>>>>________________________________ >>>>>From: Joanna Zhang <zjoanna2013 at gmail.com> >>>>>To: arun <smartpink111 at yahoo.com> >>>>>Sent: Saturday, March 2, 2013 3:59 PM >>>>> >>>>>Subject: Re: [R] cumulative sum by group and under some criteria >>>>> >>>>> >>>>>I can't figure out why there is an error for names (): Error in names(res2) <- c("m1", "n1", "x1", "y1", "m", "n", "x", "y") :? >>>>>? attempt to set an attribute on NULL >>>>> >>>>> >>>>> >>>>> >>>>>maxN<-9 >>>>>c11<-0.4 >>>>>c12<-0.4 >>>>>c1<-0.5 >>>>>c2<-0.5 >>>>> >>>>>p0L<-0.05 >>>>>p0H<-0.05 >>>>>p1L<-0.25 >>>>>p1H<-0.25 >>>>> >>>>>alpha<-0.20 >>>>>beta<-0.80 >>>>> >>>>>result <- vector("list",5) >>>>>result >>>>> >>>>>for (a in 2: (maxN-6)) { >>>>>d <- data.frame () >>>>>for ( m1 in a:a) { >>>>>? ? ? ?for (n1 in 2: (maxN-m1-4)){ >>>>>? ? ? ? ? for (x1 in 0: m1) { >>>>>? ? ? ? ? ? ? ?for (y1 in 0: n1) { >>>>> >>>>> >>>>>? ? ? ? ? ? ? ? ? ? ? ?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,term1_p0,term1_p1)) >>>>>}} >>>>>}} >>>>>colnames(d)<-c("m1","n1","x1","y1","term1_p0","term1_p1")? >>>>>tail(d) >>>>> >>>>>set.seed(8) >>>>>d1<-do.call(rbind,lapply(seq_len(nrow(d)),function(i){ >>>>>Pm<- rbeta(1000,0.2+d[i,"x1"],0.8+d[i,"m1"]-d[i,"x1"]); >>>>>Pn<- rbeta(1000,0.2+d[i,"y1"],0.8+d[i,"n1"]-d[i,"y1"]); >>>>>Fm<- ecdf(Pm); >>>>>Fn<- ecdf(Pn); >>>>>#Fmm<- Fm(d[i,"p11"]); >>>>>#Fnn<- Fn(d[i,"p12"]); >>>>> >>>>>Fmm<- Fm(p1L); >>>>>Fnn<- Fn(p1H); >>>>> >>>>>R<- (Fmm+Fnn)/2;? >>>>>Fmm_f<- max(R, Fmm); >>>>>Fnn_f<- min(R, Fnn); >>>>>Qm<- 1-Fmm_f; >>>>>Qn<- 1-Fnn_f; >>>>>data.frame(Qm,Qn)})) >>>>>d2<-cbind(d,d1) >>>>>head(d2) >>>>> >>>>> >>>>>library(zoo) >>>>>lst1<- split(d2,list(d$m1,d$n1))? >>>>>d2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){? >>>>>x[,9:14]<-NA;? >>>>>x[,9:10][x$Qm<=c11,]<-cumsum(x[,5:6][x$Qm<=c11,]);? >>>>>x[,11:12][x$Qn<=c12,]<-cumsum(x[,5:6][x$Qn<=c12,]); >>>>>x[,13:14]<-cumsum(x[,5:6]);? >>>>>colnames(x)[9:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1");? >>>>>x1<-na.locf(x);? >>>>>x1[,9:14][is.na(x1[,9:14])]<-0;? >>>>>x1} >>>>>))? >>>>>row.names(d2)<-1:nrow(d2)? >>>>>tail(d2) >>>>> >>>>>res1<-aggregate(.~m1+n1,data=d2[,c(1:2,9:12)],max) >>>>>head(res1) >>>>> >>>>>d3<-res1[res1[,4]<=0.60 & res1[,6]<0.40,]? >>>>>tail(d3) >>>>> >>>>>library(plyr)? >>>>>d4<- join(d3,d,by=c("m1","n1"),type="inner") >>>>>head(d4)? >>>>>tail(d4) >>>>> >>>>>res2<-do.call(rbind,lapply(unique(d4$m1),function(m1)? >>>>>do.call(rbind,lapply(unique(d4$n1),function(n1) >>>>>do.call(rbind,lapply(unique(d4$x1),function(x1) >>>>>do.call(rbind,lapply(unique(d4$y1),function(y1) >>>>> >>>>>#do.call(rbind,lapply(0:m1,function(x1)? >>>>>#do.call(rbind,lapply(0:n1,function(y1)? >>>>>do.call(rbind,lapply((m1+2):(maxN-2-n1),function(m)? >>>>>do.call(rbind,lapply((n1+2):(maxN-m),function(n)? >>>>>do.call(rbind,lapply(x1:(x1+m-m1), function(x)? >>>>>do.call(rbind,lapply(y1:(y1+n-n1), function(y) >>>>>expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) >>>>> >>>>>names(res2)<- c("m1","n1","x1","y1","m","n","x","y") >>>>>attr(res2,"out.attrs")<-NULL? >>>>>tail(res2) >>>>>result[[a]]<-res2 >>>>>} >>>>>result >>>>> >>>>> >>>>> >>>>> >>>>>On Sat, Mar 2, 2013 at 1:05 PM, arun <smartpink111 at yahoo.com> wrote: >>>>> >>>>>Alright, then go ahead and use a loop. >>>>>> >>>>>>A.K. >>>>>> >>>>>> >>>>>> >>>>>> >>>>>> >>>>>> >>>>>>________________________________ >>>>>>From: Joanna Zhang <zjoanna2013 at gmail.com> >>>>>>To: arun <smartpink111 at yahoo.com> >>>>>>Sent: Saturday, March 2, 2013 1:53 PM >>>>>> >>>>>>Subject: Re: [R] cumulative sum by group and under some criteria >>>>>> >>>>>> >>>>>>Thank you! When I run my real data, there was a warning massage 'lack of memory'. I am thinking that I can use a loop to run the same code for each part of the data. But I need to keep each output data "d2" and combine them.? >>>>>> >>>>>>for example: >>>>>> >>>>>>maxN<-9 >>>>>>c11<-0.4 >>>>>>c12<-0.4 >>>>>> >>>>>>p0L<-0.05 >>>>>>p0H<-0.05 >>>>>>p1L<-0.25 >>>>>>p1H<-0.25 >>>>>> >>>>>> >>>>>>for (a in 2: (maxN-6)) { >>>>>>d <- data.frame () >>>>>>for ( m1 in a:a) { >>>>>>? ? ?for (n1 in 2: (maxN-m1-4)){ >>>>>>? ? ? ? ? for (x1 in 0: m1) { >>>>>>? ? ? ? ? ? ? ?for (y1 in 0: n1) { >>>>>>? ? ? ? ? ? ? ? ? ? ? ?p11<- (x1/m1) >>>>>>? ? ? ? ? ? ? ? ? ? ? ?p12<- (y1/n1) ? ? ? ? ? ? ? ? ? ? ? ? ? ?? >>>>>> >>>>>>? ? ? ? ? ? ? ? ? ? ? ?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,p11,p12,term1_p0,term1_p1)) >>>>>>}} >>>>>>}} >>>>>>colnames(d)<-c("m1","n1","x1","y1","p11","p12","term1_p0","term1_p1")? >>>>>>tail(d) >>>>>> >>>>>>set.seed(8) >>>>>>d1<-do.call(rbind,lapply(seq_len(nrow(d)),function(i){ >>>>>>Pm<- rbeta(1000,0.2+d[i,"x1"],0.8+d[i,"m1"]-d[i,"x1"]); >>>>>>Pn<- rbeta(1000,0.2+d[i,"y1"],0.8+d[i,"n1"]-d[i,"y1"]); >>>>>>Fm<- ecdf(Pm); >>>>>>Fn<- ecdf(Pn); >>>>>> >>>>>>Fmm<- Fm(p1L); >>>>>>Fnn<- Fn(p1H); >>>>>> >>>>>>R<- (Fmm+Fnn)/2;? >>>>>>Fmm_f<- max(R, Fmm); >>>>>>Fnn_f<- min(R, Fnn); >>>>>>Qm<- 1-Fmm_f; >>>>>>Qn<- 1-Fnn_f; >>>>>>data.frame(Qm,Qn)})) >>>>>>d2<-cbind(d,d1) >>>>>>head(d2) ? ? ? ? ? ? ? ? ? ? ? ?# need to name "d2" using the value of a (or another way) to?distinguish from each other?? >>>>>>} >>>>>> >>>>>># combine all "d2" here >>>>>> >>>>>> >>>>>> >>>>>>On Fri, Mar 1, 2013 at 12:51 PM, arun <smartpink111 at yahoo.com> wrote: >>>>>> >>>>>> >>>>>>> >>>>>>>Hi, >>>>>>> >>>>>>>It seems like you haven't even looked at the output of d2, (first d2) >>>>>>>d2<- cbind(d,d1) >>>>>>>head(d2,3) >>>>>>>#? m1 n1 x1 y1 p11 p12?? term1_p0?? term1_p1??? Qm??? Qn >>>>>>>#1? 2? 2? 0? 0?? 0 0.0 0.81450625 0.31640625 1.000 1.000 >>>>>>>#2? 2? 2? 0? 1?? 0 0.5 0.08573750 0.21093750 0.666 0.666 >>>>>>>#3? 2? 2? 0? 2?? 0 1.0 0.00225625 0.03515625 0.500 0.500 >>>>>>>?ncol(d2)? # you have only 10 columns in d2 >>>>>>>#[1] 10 >>>>>>> >>>>>>> >>>>>>>#2nd problem: >>>>>>>You are splitting using d >>>>>>> >>>>>>>###############Your code >>>>>>> >>>>>>>library(zoo) >>>>>>>lst1<- split(d,list(d$m1,d$n1)) # should split byd2, because `d` doesn't have Qm or Qn columns? >>>>>>>d2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){ >>>>>>>x[,13:18]<-NA; #### this code was created for another dataset which obviously had 12 columns >>>>>>>x[,13:14][x$Qm<=c11,]<-cumsum(x[,11:12][x$Qm<=c11,]); #### here your term1_p0 and term1_p1 are columns 7 and 8. >>>>>>> >>>>>>>x[,15:16][x$Qn<=c12,]<-cumsum(x[,11:12][x$Qn<=c12,]); >>>>>>>x[,17:18]<-cumsum(x[,11:12]); >>>>>>>colnames(x)[13:18]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1"); >>>>>>>x1<-na.locf(x); >>>>>>>x1[,13:18][is.na(x1[,13:18])]<-0; >>>>>>>x1} >>>>>>>)) >>>>>>>########################################## >>>>>>> >>>>>>> >>>>>>>#corrected codes: >>>>>>> >>>>>>>lst1<- split(d2,list(d2$m1,d2$n1)) >>>>>>> >>>>>>>dNew<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){ >>>>>>>x[,11:16]<-NA; >>>>>>>x[,11:12][x$Qm<=c11,]<-cumsum(x[,7:8][x$Qm<=c11,]); >>>>>>>x[,13:14][x$Qn<=c12,]<-cumsum(x[,7:8][x$Qn<=c12,]); >>>>>>>x[,15:16]<-cumsum(x[,7:8]); >>>>>>>colnames(x)[11:16]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1"); >>>>>>>x1<-na.locf(x); >>>>>>>x1[,11:16][is.na(x1[,11:16])]<-0; >>>>>>>x1} >>>>>>>)) >>>>>>>?row.names(dNew)<- 1:nrow(dNew) >>>>>>>?head(dNew,3) >>>>>>>#? m1 n1 x1 y1 p11 p12?? term1_p0?? term1_p1??? Qm??? Qn cterm1_P0L cterm1_P1L >>>>>>>#1? 2? 2? 0? 0?? 0 0.0 0.81450625 0.31640625 1.000 1.000????????? 0????????? 0 >>>>>>>#2? 2? 2? 0? 1?? 0 0.5 0.08573750 0.21093750 0.666 0.666????????? 0????????? 0 >>>>>>>#3? 2? 2? 0? 2?? 0 1.0 0.00225625 0.03515625 0.500 0.500????????? 0????????? 0 >>>>>>>#? cterm1_P0H cterm1_P1H sumTerm1_p0 sumTerm1_p1 >>>>>>>#1????????? 0????????? 0?? 0.8145062?? 0.3164062 >>>>>>>#2????????? 0????????? 0?? 0.9002438?? 0.5273438 >>>>>>>#3????????? 0????????? 0?? 0.9025000?? 0.5625000 >>>>>>> >>>>>>> >>>>>>>A.K. >>>>>>> >>>>>>> >>>>>>>________________________________ >>>>>>> >>>>>>>From: Joanna Zhang <zjoanna2013 at gmail.com> >>>>>>>To: arun <smartpink111 at yahoo.com> >>>>>>>Sent: Friday, March 1, 2013 11:40 AM >>>>>>> >>>>>>>Subject: Re: [R] cumulative sum by group and under some criteria >>>>>>> >>>>>>> >>>>>>>Hi, why there is an error when I run the cumulative sum code below? >>>>>>> >>>>>>>Error in `[<-.data.frame`(`*tmp*`, , 16:21, value = NA) : >>>>>>>? new columns would leave holes after existing columns >>>>>>> >>>>>>> >>>>>>>maxN<-9 >>>>>>>c11<-0.4 >>>>>>>c12<-0.4 >>>>>>>c1<-0.5 >>>>>>>c2<-0.5 >>>>>>>p0L<-0.05 >>>>>>>p0H<-0.05 >>>>>>>p1L<-0.25 >>>>>>>p1H<-0.25 >>>>>>> >>>>>>>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)???????????????????????????? >>>>>>> >>>>>>>?????????????????????? 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,p11,p12,term1_p0,term1_p1)) >>>>>>>}} >>>>>>>}} >>>>>>>colnames(d)<-c("m1","n1","x1","y1","p11","p12","term1_p0","term1_p1") >>>>>>>d >>>>>>>tail(d) >>>>>>>set.seed(8) >>>>>>>d1<-do.call(rbind,lapply(seq_len(nrow(d)),function(i){ >>>>>>>Pm<- rbeta(1000,0.2+d[i,"x1"],0.8+d[i,"m1"]-d[i,"x1"]); >>>>>>>Pn<- rbeta(1000,0.2+d[i,"y1"],0.8+d[i,"n1"]-d[i,"y1"]); >>>>>>>Fm<- ecdf(Pm); >>>>>>>Fn<- ecdf(Pn); >>>>>>>Fmm<- Fm(d[i,"p11"]); >>>>>>>Fnn<- Fn(d[i,"p12"]); >>>>>>>R<- (Fmm+Fnn)/2; >>>>>>>Fmm_f<- max(R, Fmm); >>>>>>>Fnn_f<- min(R, Fnn); >>>>>>>Qm<- 1-Fmm_f; >>>>>>>Qn<- 1-Fnn_f; >>>>>>>data.frame(Qm,Qn)})) >>>>>>>d2<-cbind(d,d1) >>>>>>>d2 >>>>>>> >>>>>>>library(zoo) >>>>>>>lst1<- split(d,list(d$m1,d$n1)) >>>>>>>d2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){ >>>>>>>x[,13:18]<-NA; >>>>>>>x[,13:14][x$Qm<=c11,]<-cumsum(x[,11:12][x$Qm<=c11,]); >>>>>>>x[,15:16][x$Qn<=c12,]<-cumsum(x[,11:12][x$Qn<=c12,]); >>>>>>>x[,17:18]<-cumsum(x[,11:12]); >>>>>>>colnames(x)[13:18]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1"); >>>>>>>x1<-na.locf(x); >>>>>>>x1[,13:18][is.na(x1[,13:18])]<-0; >>>>>>>x1} >>>>>>>)) >>>>>>> >>>>>>> >>>>>>> >>>>>>> >>>>>>>On Tue, Feb 26, 2013 at 8:56 PM, arun <smartpink111 at yahoo.com> wrote: >>>>>>> >>>>>>>?? >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>>________________________________ >>>>>>>> From: Joanna Zhang <zjoanna2013 at gmail.com> >>>>>>>>To: arun <smartpink111 at yahoo.com> >>>>>>>>Sent: Tuesday, February 26, 2013 9:51 PM >>>>>>>> >>>>>>>>Subject: Re: [R] cumulative sum by group and under some criteria >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>>Hi, >>>>>>>> >>>>>>>># >>>>>>>>Pm2<-rbeta(1000, 0.2+1, 0.8+3) #obs4? >>>>>>>>this is for x=1, m=2 >>>>>>>> >>>>>>>>?length(Pm2) >>>>>>>>>#[1] 1000 >>>>>>>>> >>>>>>>>> >>>>>>>>>Pn2<-rbeta(1000, 0.2, 0.8+4) >>>>>>>>>?length(Pn2) >>>>>>>>>#[1] 1000 >>>>>>>>>Here, you are creating Pm2 or Pn2 from a single observation. >>>>>>>>> >>>>>>>>>In the code, it is creating 1000 values in total from the combination of values from x, m, >>>>>>>>>?Pm2<-rbeta(1000, 0.2+res2$x, 0.8+res2$m-res2$x) >>>>>>>>>?length(Pm2) >>>>>>>>>#[1] 1000 >>>>>>>>> >>>>>>>>>I don't get it here. What values of x and m are used here? I thought it should create 1000 observations for each combination of x,m in the data and this is what I want. >>>>>>>>> >>>>>>>>? >>>>>>>>A.K. >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>>----- Original Message ----- >>>>>>>>> >>>>>>>>>From: Zjoanna <Zjoanna2013 at gmail.com> >>>>>>>>>To: r-help at r-project.org >>>>>>>>>Cc: >>>>>>>>> >>>>>>>>>Sent: Tuesday, February 26, 2013 3:13 PM >>>>>>>>>Subject: Re: [R] cumulative sum by group and under some criteria >>>>>>>>> >>>>>>>>> >>>>>>>>>Hi Arun >>>>>>>>> >>>>>>>>>I noticed that the values of Fmm, Fnn, and other corresponding variables >>>>>>>>>are not correct, for example,? for the 4th obs after you run this code, the >>>>>>>>>Fmm is 0.40,? but if you use the x, m, y, n in the 4th row to calculate >>>>>>>>>them, the results are not consistent, same for the 5th obs. >>>>>>>>> >>>>>>>>>#check >>>>>>>>># >>>>>>>>>Pm2<-rbeta(1000, 0.2+1, 0.8+3) #obs4 >>>>>>>>>Pn2<-rbeta(1000, 0.2, 0.8+4) >>>>>>>>>Fm2<- ecdf(Pm2) >>>>>>>>>Fn2<- ecdf(Pn2) >>>>>>>>>Fmm2<-Fm2(1/4) >>>>>>>>>Fnn2<-Fn2(0) >>>>>>>>>Fmm2? #0.582 >>>>>>>>>Fnn2? ?#0 >>>>>>>>> >>>>>>>>> >>>>>>>>>Pm2<-rbeta(1000, 0.2+1, 0.8+3) #obs5 >>>>>>>>>Pn2<-rbeta(1000, 0.2+1, 0.8+3) >>>>>>>>>Fm2<- ecdf(Pm2) >>>>>>>>>Fn2<- ecdf(Pn2) >>>>>>>>>Fmm2<-Fm2(1/4) >>>>>>>>>Fnn2<-Fn2(1/4) >>>>>>>>>Fmm2 #0.404 >>>>>>>>>Fnn2? #0.416 >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>>On Sat, Feb 23, 2013 at 10:53 PM, arun kirshna [via R] < >>>>>>>>>ml-node+s789695n4659514h45 at n4.nabble.com> wrote: >>>>>>>>> >>>>>>>>>> Hi, >>>>>>>>>> d3<-structure(list(m1 = c(2, 3, 2), n1 = c(2, 2, 3), cterm1_P0L >>>>>>>>>> c(0.9025, >>>>>>>>>> 0.857375, 0.9025), cterm1_P1L = c(0.64, 0.512, 0.64), cterm1_P0H >>>>>>>>>> c(0.9025, >>>>>>>>>> 0.9025, 0.857375), cterm1_P1H = c(0.64, 0.64, 0.512)), .Names = c("m1", >>>>>>>>>> "n1", "cterm1_P0L", "cterm1_P1L", "cterm1_P0H", "cterm1_P1H"), row.names >>>>>>>>>> c(NA, >>>>>>>>>> 3L), class = "data.frame") >>>>>>>>>> d2<- data.frame() >>>>>>>>>> for (m1 in 2:3) { >>>>>>>>>>? ? ?for (n1 in 2:3) { >>>>>>>>>>? ? ? ? ?for (x1 in 0:(m1-1)) { >>>>>>>>>>? ? ? ? ? ? ?for (y1 in 0:(n1-1)) { >>>>>>>>>>? ? ? ? ?for (m in (m1+2): (7-n1)){ >>>>>>>>>>? ? ? ? ? ? ? ? for (n in (n1+2):(9-m)){ >>>>>>>>>>? ? ? ? ? ? ? ? for (x in x1:(x1+m-m1)){ >>>>>>>>>>? ? ? ? ? ? ? for(y in y1:(y1+n-n1)){ >>>>>>>>>>? d2<- rbind(d2,c(m1,n1,x1,y1,m,n,x,y)) >>>>>>>>>>? }}}}}}}} >>>>>>>>>> colnames(d2)<-c("m1","n1","x1","y1","m","n","x","y") >>>>>>>>>>? #or >>>>>>>>>> >>>>>>>>>> res1<-do.call(rbind,lapply(unique(d3$m1),function(m1) >>>>>>>>>>? do.call(rbind,lapply(unique(d3$n1),function(n1) >>>>>>>>>>? do.call(rbind,lapply(0:(m1-1),function(x1) >>>>>>>>>>? do.call(rbind,lapply(0:(n1-1),function(y1) >>>>>>>>>>? do.call(rbind,lapply((m1+2):(7-n1),function(m) >>>>>>>>>>? do.call(rbind,lapply((n1+2):(9-m),function(n) >>>>>>>>>>? do.call(rbind,lapply(x1:(x1+m-m1), function(x) >>>>>>>>>>? do.call(rbind,lapply(y1:(y1+n-n1), function(y) >>>>>>>>>>? expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) >>>>>>>>>>? names(res1)<- c("m1","n1","x1","y1","m","n","x","y") >>>>>>>>>>? attr(res1,"out.attrs")<-NULL >>>>>>>>>> res1[]<- sapply(res1,as.integer) >>>>>>>>>> >>>>>>>>>> library(plyr) >>>>>>>>>> res2<- join(res1,d3,by=c("m1","n1"),type="inner") >>>>>>>>>> >>>>>>>>>> #Assuming that these are the values you used: >>>>>>>>>> >>>>>>>>>> p0L<-0.05 >>>>>>>>>> p0H<-0.05 >>>>>>>>>> p1L<-0.20 >>>>>>>>>> p1H<-0.20 >>>>>>>>>> res2<- within(res2,{p1<- x/m; p2<- y/n;term2_p0<-dbinom(x1,m1, p0L, >>>>>>>>>> log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* >>>>>>>>>> dbinom(y-y1,n-n1,p0H, log=FALSE);term2_p1<- dbinom(x1,m1, p1L, log=FALSE)* >>>>>>>>>> dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* >>>>>>>>>> dbinom(y-y1,n-n1,p1H, log=FALSE);Pm2<-rbeta(240, 0.2+x, >>>>>>>>>> 0.8+m-x);Pn2<-rbeta(240, 0.2+y, 0.8+n-y)}) >>>>>>>>>> Fm2<- ecdf(res2$Pm2) >>>>>>>>>> Fn2<- ecdf(res2$Pn2) >>>>>>>>>> >>>>>>>>>> res3<- within(res2,{Fmm2<-Fm2(p1);Fnn2<- Fn2(p2);R2<- (Fmm2+Fnn2)/2}) #not >>>>>>>>>> sure about this step especially the Fm2() or Fn2() >>>>>>>>>> res3$Fmm_f2<-apply(res3[,c("R2","Fmm2")],1,min) >>>>>>>>>>? res3$Fnn_f2<-apply(res3[,c("R2","Fnn2")],1,max) >>>>>>>>>> res3<- within(res3,{Qm2<- 1-Fmm_f2;Qn2<- 1-Fnn_f2}) >>>>>>>>>> head(res3) >>>>>>>>>> #? m1 n1 x1 y1 m n x y cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H >>>>>>>>>> Pn2 >>>>>>>>>> #1? 2? 2? 0? 0 4 4 0 0? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>>>>>>>> 0.001084648 >>>>>>>>>> #2? 2? 2? 0? 0 4 4 0 1? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>>>>>>>> 0.504593909 >>>>>>>>>> #3? 2? 2? 0? 0 4 4 0 2? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>>>>>>>> 0.541379357 >>>>>>>>>> #4? 2? 2? 0? 0 4 4 1 0? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>>>>>>>> 0.138947785 >>>>>>>>>> #5? 2? 2? 0? 0 4 4 1 1? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>>>>>>>> 0.272364957 >>>>>>>>>> #6? 2? 2? 0? 0 4 4 1 2? ? ?0.9025? ? ? ?0.64? ? ?0.9025? ? ? ?0.64 >>>>>>>>>> 0.761635059 >>>>>>>>>> #? ? ? ? ? ?Pm2? ?term2_p1? ? ?term2_p0? ?p2? ?p1? ? ? ? R2? ? ? Fnn2 Fmm2 >>>>>>>>>> #1 1.212348e-05 0.16777216 0.6634204313 0.00 0.00 0.0000000 0.0000000? 0.0 >>>>>>>>>> #2 1.007697e-03 0.08388608 0.0698337296 0.25 0.00 0.1791667 0.3583333? 0.0 >>>>>>>>>> #3 1.106946e-05 0.01048576 0.0018377297 0.50 0.00 0.3479167 0.6958333? 0.0 >>>>>>>>>> # 2.086758e-01 0.08388608 0.0698337296 0.00 0.25 0.2000000 0.0000000? 0.4 >>>>>>>>>> #5 2.382179e-01 0.04194304 0.0073509189 0.25 0.25 0.3791667 0.3583333? 0.4 >>>>>>>>>> #6 4.494673e-01 0.00524288 0.0001934452 0.50 0.25 0.5479167 0.6958333? 0.4 >>>>>>>>>> #? ? ?Fmm_f2? ? Fnn_f2? ? ? ?Qn2? ? ? ?Qm2 >>>>>>>>>> #1 0.0000000 0.0000000 1.0000000 1.0000000 >>>>>>>>>> #2 0.0000000 0.3583333 0.6416667 1.0000000 >>>>>>>>>> #3 0.0000000 0.6958333 0.3041667 1.0000000 >>>>>>>>>> #4 0.2000000 0.2000000 0.8000000 0.8000000 >>>>>>>>>> #5 0.3791667 0.3791667 0.6208333 0.6208333 >>>>>>>>>> #6 0.4000000 0.6958333 0.3041667 0.6000000 >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> A.K. >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> ________________________________ >>>>>>>>>> From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=0>> >>>>>>>>>> >>>>>>>>>> To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=1>> >>>>>>>>> >>>>>>>>>> >>>>>>>>>> Sent: Friday, February 22, 2013 11:02 AM >>>>>>>>>> Subject: Re: [R] cumulative sum by group and under some criteria >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> Thanks!? Then I need to create new variables based on the res2.? I can't >>>>>>>>>> find Fmm_f1, Fnn_f2, R2, Qm2, Qn2 until? running the code several times and >>>>>>>>>> the values of Fnn_f2, Fmm_f2 are correct. >>>>>>>>>> >>>>>>>>>> attach(res2) >>>>>>>>>> res2$p1<-x/m >>>>>>>>>> res2$p2<-y/n >>>>>>>>>> res2$term2_p0 <- dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, >>>>>>>>>> log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, >>>>>>>>>> log=FALSE) >>>>>>>>>> res2$term2_p1 <- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, >>>>>>>>>> log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, >>>>>>>>>> log=FALSE) >>>>>>>>>> Pm2<-rbeta(1000, 0.2+x, 0.8+m-x) >>>>>>>>>> Fm2<-ecdf(Pm2) >>>>>>>>>> res2$Fmm2<-Fm2(x/m)? #not correct, it comes out after running code two >>>>>>>>>> times >>>>>>>>>> Pn2<-rbeta(1000, 0.2+y, 0.8+n-y) >>>>>>>>>> Fn2<-ecdf(Pn2) >>>>>>>>>> res2$Fnn2<-Fn2(y/n) >>>>>>>>>> res2$R2<-(Fmm2+Fnn2)/2 >>>>>>>>>> res2$Fmm_f2<-min(R2,Fmm2)? # not correct >>>>>>>>>> res2$Fnn_f2<-max(R2,Fnn2) >>>>>>>>>> res2$Qm2<-(1-Fmm_f2) >>>>>>>>>> res2$Qn2<-(1-Fnn_f2) >>>>>>>>>> detach(res2) >>>>>>>>>> res2 >>>>>>>>>> head(res2) >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> On Tue, Feb 19, 2013 at 4:09 PM, arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=2>> >>>>>>>>> >>>>>>>>>> wrote: >>>>>>>>>> >>>>>>>>>> Hi, >>>>>>>>>> >>>>>>>>>> > >>>>>>>>>> >""suppose that I have a dataset 'd' >>>>>>>>>> >? ?m1? n1? ? A? ? ? ? ? ? ?B? ? ?C? ? ? ? ?D >>>>>>>>>> >1? 2? 2? ?0.902500? ? 0.640? ?0.9025? ? 0.64 >>>>>>>>>> >2? 3? 2? ?0.857375? ? 0.512? ?0.9025? ? 0.64 >>>>>>>>>> >I want to add? x1 (from 0 to m1), y1(from 0 to n1), m (range from >>>>>>>>>> >m1+2 to 7-n1), n(from n1+2 to 9-m), x (x1 to x1+m-m1), y(y1 to y1+n-n1), >>>>>>>>>> expanding to another dataset 'd2' based on each row (combination of m1 >>>>>>>>>> >and n1)"" >>>>>>>>>> > >>>>>>>>>> > >>>>>>>>>> >Try: >>>>>>>>>> > >>>>>>>>>> > >>>>>>>>>> > d<-read.table(text=" >>>>>>>>>> > >>>>>>>>>> >m1? n1? ? A? ? ? ? ? ? ?B? ? ?C? ? ? ? ?D >>>>>>>>>> >1? 2? 2? ?0.902500? ? 0.640? ?0.9025? ? 0.64 >>>>>>>>>> >2? 3? 2? ?0.857375? ? 0.512? ?0.9025? ? 0.64 >>>>>>>>>> >",sep="",header=TRUE) >>>>>>>>>> > >>>>>>>>>> >vec1<- paste(d[,1],d[,2],d[,3],d[,4],d[,5],d[,6]) >>>>>>>>>> >res1<- do.call(rbind,lapply(vec1,function(m1) >>>>>>>>>> do.call(rbind,lapply(0:(as.numeric(substr(m1,1,1))),function(x1) >>>>>>>>>> do.call(rbind,lapply(0:(as.numeric(substr(m1,3,3))),function(y1) >>>>>>>>>> do.call(rbind,lapply((as.numeric(substr(m1,1,1))+2):(7-as.numeric(substr(m1,3,3))),function(m) >>>>>>>>>> do.call(rbind,lapply((as.numeric(substr(m1,3,3))+2):(9-m),function(n) >>>>>>>>>> > >>>>>>>>>> > do.call(rbind,lapply(x1:(x1+m-as.numeric(substr(m1,1,1))), function(x) >>>>>>>>>> > do.call(rbind,lapply(y1:(y1+n-as.numeric(substr(m1,3,3))), function(y) >>>>>>>>>> > expand.grid(m1,x1,y1,m,n,x,y)) ))))))))))))) >>>>>>>>>> > >>>>>>>>>> names(res1)<- c("group","x1","y1","m","n","x","y") >>>>>>>>>> >>>>>>>>>> > res1$m1<- NA; res1$n1<- NA; res1$A<- NA; res1$B<- NA; res1$C<- NA;res1$D >>>>>>>>>> <- NA >>>>>>>>>> >res1[,8:13]<-do.call(rbind,lapply(strsplit(as.character(res1$group)," >>>>>>>>>> "),as.numeric)) >>>>>>>>>> >res2<- res1[,c(8:9,2:7,10:13)] >>>>>>>>>> > >>>>>>>>>> > >>>>>>>>>> > head(res2) >>>>>>>>>> >#? m1 n1 x1 y1 m n x y? ? ? A? ? B? ? ? C? ? D >>>>>>>>>> >#1? 2? 2? 0? 0 4 4 0 0 0.9025 0.64 0.9025 0.64 >>>>>>>>>> >#2? 2? 2? 0? 0 4 4 0 1 0.9025 0.64 0.9025 0.64 >>>>>>>>>> >#3? 2? 2? 0? 0 4 4 0 2 0.9025 0.64 0.9025 0.64 >>>>>>>>>> >#4? 2? 2? 0? 0 4 4 1 0 0.9025 0.64 0.9025 0.64 >>>>>>>>>> >#5? 2? 2? 0? 0 4 4 1 1 0.9025 0.64 0.9025 0.64 >>>>>>>>>> >#6? 2? 2? 0? 0 4 4 1 2 0.9025 0.64 0.9025 0.64 >>>>>>>>>> > >>>>>>>>>> > >>>>>>>>>> > >>>>>>>>>> > >>>>>>>>>> > >>>>>>>>>> > >>>>>>>>>> >________________________________ >>>>>>>>>> >From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=3>> >>>>>>>>>> >>>>>>>>>> >To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=4>> >>>>>>>>> >>>>>>>>>> >>>>>>>>>> >Sent: Tuesday, February 19, 2013 11:43 AM >>>>>>>>>> > >>>>>>>>>> >Subject: Re: [R] cumulative sum by group and under some criteria >>>>>>>>>> > >>>>>>>>>> > >>>>>>>>>> >Thanks. I can get the data I expected (get rid of the m1=3, n1=3) using >>>>>>>>>> the join and 'inner' code, but just curious about the way to expand the >>>>>>>>>> data. There should be a way to expand the data based on each row >>>>>>>>>> (combination of the variables), unique(d3$m1 & d3$n1) ?. >>>>>>>>>> > >>>>>>>>>> >or is there a way to use 'data.frame' and 'for' loop to expand directly >>>>>>>>>> from the data? like res1<-data.frame (d3) for () {.... >>>>>>>>>> > >>>>>>>>>> > >>>>>>>>>> >On Tue, Feb 19, 2013 at 9:55 AM, arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=5>> >>>>>>>>> >>>>>>>>>> wrote: >>>>>>>>>> > >>>>>>>>>> >If you can provide me the output that you expect with all the rows of the >>>>>>>>>> combination in the res2, I can take a look. >>>>>>>>>> >> >>>>>>>>>> >> >>>>>>>>>> >> >>>>>>>>>> >> >>>>>>>>>> >> >>>>>>>>>> >> >>>>>>>>>> >>________________________________ >>>>>>>>>> >> >>>>>>>>>> >>From: Joanna Zhang <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=6>> >>>>>>>>>> >>>>>>>>>> >>To: arun <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=7>> >>>>>>>>> >>>>>>>>>> >>>>>>>>>> >> >>>>>>>>>>? >>Sent: Tuesday, February 19, 2013 10:42 AM >>>>>>>>>> >> >>>>>>>>>> >>Subject: Re: [R] cumulative sum by group and under some criteria >>>>>>>>>> >> >>>>>>>>>> >> >>>>>>>>>> >>Thanks. But I thougth the expanded dataset 'res1' should not have >>>>>>>>>> combination of m1=3 and n1=3 because it is based on dataset 'd3' which >>>>>>>>>> doesn't have m1=3 and n1=3, right?> >>>>>>>>>> >>>In the example that you provided: >>>>>>>>>> >>> (m1+2):(maxN-(n1+2)) >>>>>>>>>> >>>#[1] 5 >>>>>>>>>> >>> (n1+2):(maxN-5) >>>>>>>>>> >>>#[1] 4 >>>>>>>>>> >>>#Suppose >>>>>>>>>> >>> x1<- 4 >>>>>>>>>> >>> y1<- 2 >>>>>>>>>> >>> x1:(x1+5-m1) >>>>>>>>>> >>>#[1] 4 5 6 >>>>>>>>>> >>> y1:(y1+4-n1) >>>>>>>>>> >>>#[1] 2 3 4 >>>>>>>>>> >>> >>>>>>>>>> >>> datnew<-expand.grid(5,4,4:6,2:4) >>>>>>>>>> >>> colnames(datnew)<- c("m","n","x","y") >>>>>>>>>> >>>datnew<-within(datnew,{p1<- x/m;p2<-y/n}) >>>>>>>>>> >>>res<-cbind(datnew,d2[rep(1:nrow(d2),nrow(datnew)),]) >>>>>>>>>> >>> row.names(res)<- 1:nrow(res) >>>>>>>>>> >>> res >>>>>>>>>> >>>#? m n x y? ?p2? p1 m1 n1 cterm1_P1L cterm1_P0H >>>>>>>>>> >>>#1 5 4 4 2 0.50 0.8? 3? 2? ? 0.00032? ? ?0.0025 >>>>>>>>>> >>>#2 5 4 5 2 0.50 1.0? 3? 2? ? 0.00032? ? ?0.0025 >>>>>>>>>> >>>#3 5 4 6 2 0.50 1.2? 3? 2? ? 0.00032? ? ?0.0025 >>>>>>>>>> >>>#4 5 4 4 3 0.75 0.8? 3? 2? ? 0.00032? ? ?0.0025 >>>>>>>>>> >>>#5 5 4 5 3 0.75 1.0? 3? 2? ? 0.00032? ? ?0.0025 >>>>>>>>>> >>>#6 5 4 6 3 0.75 1.2? 3? 2? ? 0.00032? ? ?0.0025 >>>>>>>>>> >>>#7 5 4 4 4 1.00 0.8? 3? 2? ? 0.00032? ? ?0.0025 >>>>>>>>>> >>>#8 5 4 5 4 1.00 1.0? 3? 2? ? 0.00032? ? ?0.0025 >>>>>>>>>> >>>#9 5 4 6 4 1.00 1.2? 3? 2? ? 0.00032? ? ?0.0025 >>>>>>>>>> >>> >>>>>>>>>> >>>A.K. >>>>>>>>>> >>> >>>>>>>>>> >>> >>>>>>>>>> >>> >>>>>>>>>> >>> >>>>>>>>>> >>> >>>>>>>>>> >>>----- Original Message ----- >>>>>>>>>> >>>From: Zjoanna <[hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=8>> >>>>>>>>>> >>>>>>>>>> >>>To: [hidden email]<http://user/SendEmail.jtp?type=node&node=4659514&i=9> >>>>>>>>> >>>>>>>>>> >>>Cc: >>>>>>>>>> >>> >>>>>>>>>> >>>Sent: Sunday, February 10, 2013 6:04 PM >>>>>>>>>> >>>Subject: Re: [R] cumulative sum by group and under some criteria >>>>>>>>>> >>> >>>>>>>>>> >>> >>>>>>>>>> >>>Hi, >>>>>>>>>> >>>How to expand or loop for one variable n based on another variable? for >>>>>>>>>> >>>example, I want to add m (from m1 to maxN- n1-2) and for each m, I want >>>>>>>>>> to >>>>>>>>>> >>>add n (n1+2 to maxN-m), and similarly add x and y, then I need to do >>>>>>>>>> some >>>>>>>>>> >>>calculations. >>>>>>>>>> >>> >>>>>>>>>> >>>d3<-data.frame(d2) >>>>>>>>>> >>>? ? for (m in (m1+2):(maxN-(n1+2)){ >>>>>>>>>> >>>? ? ? ?for (n in (n1+2):(maxN-m)){ >>>>>>>>>> >>>? ? ? ? ? ? ?for (x in x1:(x1+m-m1)){ >>>>>>>>>> >>>? ? ? ? ? ? ? ? ? for (y in y1:(y1+n-n1)){ >>>>>>>>>> >>>? ? ? ? ? ? ? ? ? ? ? ?p1<- x/m >>>>>>>>>> >>>? ? ? ? ? ? ? ? ? ? ? ?p2<- y/n >>>>>>>>>> >>>}}}} >>>>>>>>>> >>> >>>>>>>>>> >>>On Thu, Feb 7, 2013 at 12:16 AM, arun kirshna [via R] < >>>>>>>>>>? >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4659514&i=10>> >>>>>>>>> >>>>>>>>>> wrote: >>>>>>>>>> >>> >>>>>>>>>> >>>> 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]< >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=0>> >>>>>>>>>> >>>> >>>>>>>>>> >>>> To: arun <[hidden email]< >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=1>> >>>>>>>>>> >>> >>>>>>>>>> >>>> >>>>>>>>>> >>>> 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]< >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=2>> >>>>>>>>>> >>> >>>>>>>>>> >>>> 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]< >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=3>> >>>>>>>>>> >>>> >>>>>>>>>> >>>> >To: arun <[hidden email]< >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=4>> >>>>>>>>>> >>> >>>>>>>>>> >>>> >>>>>>>>>> >>>> >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]< >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=5>> >>>>>>>>>> >>> >>>>>>>>>> >>>> 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]< >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=6>> >>>>>>>>>> >>>> >>>>>>>>>> >>>> >>To: arun <[hidden email]< >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=7>> >>>>>>>>>> >>> >>>>>>>>>> >>>> >>>>>>>>>> >>>> >>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]< >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=8>> >>>>>>>>>> >>> >>>>>>>>>> >>>> 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]< >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=9>";;;;;;;;;; >>>>>>>>>> >>>> <[hidden email] < >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=10>> >>>>>>>>>> >>>> >>>To: [hidden email]< >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=11> >>>>>>>>>> >>>>? >>>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]< >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=12>";;;;;;;;;; >>>>>>>>>> >>>> <[hidden email] < >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=13>> >>>>>>>>>> >>>> >>>To: [hidden email]< >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=14> >>>>>>>>>> >>>>? >>>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]< >> > >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=15>> >>>>>>>>>> >>>> >>>>>>>>>> >>>> >>>To: [hidden email]< >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=16> >>>>>>>>>> >>> >>>>>>>>>> >>>> >>>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] < >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=17>> >>>>>>>>>> >>> >>>>>>>>>> >>>> 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] < >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=18>mailing list >>>>>>>>>> >>> >>>>>>>>>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>>>>>>>> >>>> >>>PLEASE do read the posting guide >>>>>>>>>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>>>>>>>> <http://www.r-project.org/posting-guide.html> >>>>>>>>>> >>> >>>>>>>>>> >>>> >>>and provide commented, minimal, self-contained, reproducible code. >>>>>>>>>> >>>> >>> >>>>>>>>>> >>>> >>> >>>>>>>>>> >>>> >>>______________________________________________ >>>>>>>>>> >>>> >>>[hidden email] < >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=19>mailing list >>>>>>>>>> >>> >>>>>>>>>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>>>>>>>> >>>> >>>PLEASE do read the posting guide >>>>>>>>>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>>>>>>>> <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] < >>>>>>>>>> http://user/SendEmail.jtp?type=node&node=4657773&i=20>mailing list >>>> >>>>>>>>>> >>> >>>>>>>>>> >>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>>>>>>>> >>>> >>>PLEASE do read the posting guide >>>>>>>>>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>>>>>>>> <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] <http://user/SendEmail.jtp?type=node&node=4657773&i=21>mailing >>>> >>>>>>>>>> list >>>>>>>>>> >>> >>>>>>>>>> >>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>>>>>>>> >>>> PLEASE do read the posting guide >>>>>>>>>> >>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>>>>>>>> <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-tp4657074p4657773.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-tp4657074p4658133.html >>>> >>>>>>>>>> >>> >>>>>>>>>> >>>Sent from the R help mailing list archive at Nabble.com. >>>>>>>>>> >>>? ? [[alternative HTML version deleted]] >>>>>>>>>> >>> >>>>>>>>>> >>>______________________________________________ >>>>>>>>>> >>>[hidden email] <http://user/SendEmail.jtp?type=node&node=4659514&i=11>mailing list >>>> >>>>>>>>> >>>>>>>>>> >>> >>>>>>>>>> >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>>>>>>>> >>>PLEASE do read the posting guide >>>>>>>>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >>>> >>>>>>>>>> >>>and provide commented, minimal, self-contained, reproducible code. >>>>>>>>>> >>> >>>>>>>>>> >>> >>>>>>>>>> >> >>>>>>>>>> > >>>>>>>>>> >>>>>>>>>> ______________________________________________ >>>>>>>>>> [hidden email] <http://user/SendEmail.jtp?type=node&node=4659514&i=12>mailing list >>>> >>>>>>>>> >>>>>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>>>>>>>> PLEASE do read the posting guide >>>>>>>>>> http://www.R-project.org/posting-guide.html<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-tp4657074p4659514.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-tp4657074p4659717.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. >>>>>>>>> >>>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>>??????????????????????????????????????????? ?????????????????????? ? >>>>>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? >>>>>>?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ??? >>>>> >>>> >>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ??? >> >
HI, maxN<-10 c11<-0.07 c12<-0.07 c1<-0.15 c2<-0.20 p0L<-0.05 p0H<-0.05 p1L<-0.25 p1H<-0.25 alpha<-0.6 beta<-0.6 bay<-function (c11,c12,c1,c2){ d<-do.call(rbind,lapply(2:(maxN-6),function(m1) do.call(rbind,lapply(2:(maxN-m1-4),function(n1) do.call(rbind,lapply(0:m1,function(x1) do.call(rbind,lapply(0:n1,function(y1) expand.grid(m1,n1,x1,y1))))))))) names(d)<-c("m1","n1","x1","y1") d<-within(d,{ term1_p1<- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, log=FALSE) term1_p0<- dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE) })????? ########## add Qm Qn ################################## set.seed(8) d1<-do.call(rbind,lapply(seq_len(nrow(d)),function(i){ Pm<- rbeta(1000,0.2+d[i,"x1"],0.8+d[i,"m1"]-d[i,"x1"]); Pn<- rbeta(1000,0.2+d[i,"y1"],0.8+d[i,"n1"]-d[i,"y1"]); Fm<- ecdf(Pm); Fn<- ecdf(Pn); #Fmm<- Fm(d[i,"p11"]); #Fnn<- Fn(d[i,"p12"]); Fmm<- Fm(p1L); Fnn<- Fn(p1H); R<- (Fmm+Fnn)/2; Fmm_f<- max(R, Fmm); Fnn_f<- min(R, Fnn); Qm<- 1-Fmm_f; Qn<- 1-Fnn_f; data.frame(Qm,Qn)})) d2<-cbind(d,d1) library(zoo) lst1<- split(d2,list(d$m1,d$n1)) d2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){ x[,9:14]<-NA; x[,9:10][x$Qm<=c11,]<-cumsum(x[,5:6][x$Qm<=c11,]); x[,11:12][x$Qn<=c12,]<-cumsum(x[,5:6][x$Qn<=c12,]); x[,13:14]<-cumsum(x[,5:6]); colnames(x)[9:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1"); x1<-na.locf(x); x1[,9:14][is.na(x1[,9:14])]<-0; x1} )) row.names(d2)<-1:nrow(d2) res1<-aggregate(.~m1+n1,data=d2[,c(1:2,9:12)],max) d3<-res1[res1[,4]<=beta & res1[,6]<beta,] f1<-function(dat){ stopifnot(nrow(dat)!=0) library(plyr) join(dat,d2,by=c("m1","n1"),type="inner") }###not present d4<-f1(d3)###not run ######################### ################ 2nd stage ################################ #### this method not look at the combination of m1 and n1, #### so need to merger the orginal data 'd2 res2<-do.call(rbind,lapply(unique(d4$m1),function(m1) do.call(rbind,lapply(unique(d4$n1),function(n1) do.call(rbind,lapply(unique(d4$x1),function(x1) do.call(rbind,lapply(unique(d4$y1),function(y1) ? #do.call(rbind,lapply(0:m1,function(x1) #do.call(rbind,lapply(0:n1,function(y1) do.call(rbind,lapply((m1+2):(maxN-2-n1),function(m) do.call(rbind,lapply((n1+2):(maxN-m),function(n) do.call(rbind,lapply(x1:(x1+m-m1), function(x) do.call(rbind,lapply(y1:(y1+n-n1), function(y) expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) names(res2)<- c("m1","n1","x1","y1","m","n","x","y") attr(res2,"out.attrs")<-NULL res3<- join(res2,d4,by=c("m1","n1","x1","y1"),type="inner") res3<-res3[,c(1:16)] ############################################################# add more variables another method ############################# res3<- within(res3,{ term2_p0<- dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, log=FALSE); term2_p1<- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, log=FALSE)}) #term2_p0<- term1_p0*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, log=FALSE); #term2_p1<- term1_p1*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, log=FALSE)}) res4<-do.call(rbind,lapply(seq_len(nrow(res3)),function(i){ Pm2<-rbeta(1000,0.2+res3[i,"x"],0.8+res3[i,"m"]-res3[i,"x"]); Pn2<- rbeta(1000,0.2+res3[i,"y"],0.8+res3[i,"n"]-res3[i,"y"]); Fm2<- ecdf(Pm2); Fn2<- ecdf(Pn2); #Fmm2<- Fm2(res3[i,"p1"]); #Fnn2<- Fn2(res3[i,"p2"]); Fmm2<- Fm2(p1L); Fnn2<- Fn2(p1H); R2<- (Fmm2+Fnn2)/2; Fmm_f2<- max(R2, Fmm2); Fnn_f2<- min(R2, Fnn2); Qm2<- 1-Fmm_f2; Qn2<- 1-Fnn_f2; data.frame(Qm2,Qn2)})) res5<-cbind(res3,res4) ################################################# calculate cumulative sum ################################# lst2<- split(res5,list(res5$m1,res5$n1,res5$m,res5$n)) ? res5<-do.call(rbind,lapply(lst2[lapply(lst2,nrow)!=0], function(x){ x[,21:26]<-NA; x[,21:22][x$Qm2<=c1 & x$Qm>c11,]<-cumsum(x[,17:18][x$Qm2<=c1 & x$Qm>c11,]); x[,23:24][x$Qn2<=c2 & x$Qn>c12,]<-cumsum(x[,17:18][x$Qn2<=c2 & x$Qn>c12,]); x[,25:26]<-cumsum(x[,17:18]); colnames(x)[21:26]<- c("cterm2_P1L","cterm2_P0L","cterm2_P1H","cterm2_P0H","sumT2p0","sumT2p1"); x2<-na.locf(x); x2[,21:26][is.na(x2[,21:26])]<-0; x2} )) row.names(res5)<-1:nrow(res5) res6<-aggregate(.~m1+n1+m+n,data=res5[,c(1:6,9:12,21:24)] ,max) res6<-within(res6,{AL<-1-(cterm1_P0L + cterm2_P0L); AH<-1-(cterm1_P0H + cterm2_P0H); BL<-(cterm1_P1L + cterm2_P1L); BH<-(cterm1_P1H + cterm2_P1H); EN<- m1 + (m-m1)*(1-cterm1_P0L) + n1 + (n-n1)*(1-cterm1_P0H); N<-m+n }) res7<-res6[,c(1:4,7,9,15:20)] res7<-res7[res7[,9]<=beta & res7[,10]<=beta &res7[,11] <= alpha & res7[,12] <= alpha,] } bay(0.07,0.07,0.15,0.20) res7 ?head(res7) #? m1 n1 m n cterm1_P0L cterm1_P0H? N?????? EN??????? BH??????? BL??????? AH #1? 2? 2 4 4? 0.8167625????????? 0? 8 6.366475 0.1001129 0.4702148 0.3365796 #2? 2? 2 5 4? 0.8167625????????? 0? 9 6.549713 0.2002258 0.4405518 0.2038955 #3? 3? 2 5 4? 0.8573750????????? 0? 9 7.285250 0.2002258 0.4218750 0.2038955 #4? 2? 2 6 4? 0.8167625????????? 0 10 6.732950 0.1689405 0.4558468 0.2121882 #5? 3? 2 6 4? 0.8573750????????? 0 10 7.427875 0.1689405 0.4781885 0.2121882 #6? 4? 2 6 4? 0.8145062????????? 0 10 8.370988 0.1689405 0.3914909 0.2121882 ?# ??????? AL #1 0.10585941 #2 0.10972831 #3 0.14262500 #4 0.05037883 #5 0.04808759 #6 0.05944387 A.K. ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Monday, March 11, 2013 3:55 PM Subject: Re: [R] cumulative sum by group and under some criteria ok. Could you take a look at this code when you have time? I really can't figure out why the 'res 7' not found if I put the code in functions. if I take out the Bay function and f1 function, res7 should have some data. rm(list=ls()) ls() ls(all=true) search() maxN<-10 c11<-0.07 c12<-0.07 c1<-0.15 c2<-0.20 p0L<-0.05 p0H<-0.05 p1L<-0.25 p1H<-0.25 alpha<-0.6 beta<-0.6 bay<-function (c11,c12,c1,c2){ d<-do.call(rbind,lapply(2:(maxN-6),function(m1)? do.call(rbind,lapply(2:(maxN-m1-4),function(n1) do.call(rbind,lapply(0:m1,function(x1) do.call(rbind,lapply(0:n1,function(y1) expand.grid(m1,n1,x1,y1)))))))))? names(d)<-c("m1","n1","x1","y1") d<-within(d,{ term1_p1<- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, log=FALSE) term1_p0<- dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE) }) ? ? ? ########## add Qm Qn ################################## set.seed(8) d1<-do.call(rbind,lapply(seq_len(nrow(d)),function(i){ Pm<- rbeta(1000,0.2+d[i,"x1"],0.8+d[i,"m1"]-d[i,"x1"]); Pn<- rbeta(1000,0.2+d[i,"y1"],0.8+d[i,"n1"]-d[i,"y1"]); Fm<- ecdf(Pm); Fn<- ecdf(Pn); #Fmm<- Fm(d[i,"p11"]); #Fnn<- Fn(d[i,"p12"]); Fmm<- Fm(p1L); Fnn<- Fn(p1H); R<- (Fmm+Fnn)/2;? Fmm_f<- max(R, Fmm); Fnn_f<- min(R, Fnn); Qm<- 1-Fmm_f; Qn<- 1-Fnn_f; data.frame(Qm,Qn)})) d2<-cbind(d,d1) library(zoo) lst1<- split(d2,list(d$m1,d$n1))? d2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){? x[,9:14]<-NA;? x[,9:10][x$Qm<=c11,]<-cumsum(x[,5:6][x$Qm<=c11,]);? x[,11:12][x$Qn<=c12,]<-cumsum(x[,5:6][x$Qn<=c12,]); x[,13:14]<-cumsum(x[,5:6]);? colnames(x)[9:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1");? x1<-na.locf(x);? x1[,9:14][is.na(x1[,9:14])]<-0;? x1} ))? row.names(d2)<-1:nrow(d2) res1<-aggregate(.~m1+n1,data=d2[,c(1:2,9:12)],max) d3<-res1[res1[,4]<=beta & res1[,6]<beta,]? d3 f1<-function(dat){ stopifnot(nrow(dat)!=0) library(plyr)? d4<- join(dat,d2,by=c("m1","n1"),type="inner") d4 ######################### ################ 2nd stage ################################ #### this method not look at the combination of m1 and n1, #### so need to merger the orginal data 'd2 res2<-do.call(rbind,lapply(unique(d4$m1),function(m1)? do.call(rbind,lapply(unique(d4$n1),function(n1) do.call(rbind,lapply(unique(d4$x1),function(x1) do.call(rbind,lapply(unique(d4$y1),function(y1) #do.call(rbind,lapply(0:m1,function(x1)? #do.call(rbind,lapply(0:n1,function(y1)? do.call(rbind,lapply((m1+2):(maxN-2-n1),function(m)? do.call(rbind,lapply((n1+2):(maxN-m),function(n)? do.call(rbind,lapply(x1:(x1+m-m1), function(x)? do.call(rbind,lapply(y1:(y1+n-n1), function(y) expand.grid(m1,n1,x1,y1,m,n,x,y)) )))))))))))))))? names(res2)<- c("m1","n1","x1","y1","m","n","x","y")? attr(res2,"out.attrs")<-NULL? #install.packages(pkgs="plyr") library(plyr)? res3<- join(res2,d4,by=c("m1","n1","x1","y1"),type="inner")? res3<-res3[,c(1:16)] ############################################################# add more variables another method ############################# res3<- within(res3,{ term2_p0<- dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, log=FALSE); term2_p1<- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, log=FALSE)})? #term2_p0<- term1_p0*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, log=FALSE); #term2_p1<- term1_p1*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, log=FALSE)})? res4<-do.call(rbind,lapply(seq_len(nrow(res3)),function(i){ Pm2<-rbeta(1000,0.2+res3[i,"x"],0.8+res3[i,"m"]-res3[i,"x"]); Pn2<- rbeta(1000,0.2+res3[i,"y"],0.8+res3[i,"n"]-res3[i,"y"]); Fm2<- ecdf(Pm2);? Fn2<- ecdf(Pn2); #Fmm2<- Fm2(res3[i,"p1"]); #Fnn2<- Fn2(res3[i,"p2"]); Fmm2<- Fm2(p1L); Fnn2<- Fn2(p1H); R2<- (Fmm2+Fnn2)/2;? Fmm_f2<- max(R2, Fmm2); Fnn_f2<- min(R2, Fnn2);? Qm2<- 1-Fmm_f2; Qn2<- 1-Fnn_f2; data.frame(Qm2,Qn2)})) res5<-cbind(res3,res4) ################################################# calculate cumulative sum ################################# lst2<- split(res5,list(res5$m1,res5$n1,res5$m,res5$n)) res5<-do.call(rbind,lapply(lst2[lapply(lst2,nrow)!=0], function(x){? x[,21:26]<-NA;? x[,21:22][x$Qm2<=c1 & x$Qm>c11,]<-cumsum(x[,17:18][x$Qm2<=c1 & x$Qm>c11,]);? x[,23:24][x$Qn2<=c2 & x$Qn>c12,]<-cumsum(x[,17:18][x$Qn2<=c2 & x$Qn>c12,]);? x[,25:26]<-cumsum(x[,17:18]); colnames(x)[21:26]<- c("cterm2_P1L","cterm2_P0L","cterm2_P1H","cterm2_P0H","sumT2p0","sumT2p1");? x2<-na.locf(x);? x2[,21:26][is.na(x2[,21:26])]<-0; x2} ))? row.names(res5)<-1:nrow(res5) res6<-aggregate(.~m1+n1+m+n,data=res5[,c(1:6,9:12,21:24)] ,max)? res6<-within(res6,{AL<-1-(cterm1_P0L + cterm2_P0L); AH<-1-(cterm1_P0H + cterm2_P0H); BL<-(cterm1_P1L + cterm2_P1L); BH<-(cterm1_P1H + cterm2_P1H); EN<- m1 + (m-m1)*(1-cterm1_P0L) + n1 + (n-n1)*(1-cterm1_P0H); N<-m+n }) res7<-res6[,c(1:4,7,9,15:20)] res7<-res7[res7[,9]<=beta & res7[,10]<=beta &res7[,11] <= alpha & res7[,12] <= alpha,]? } f1(d3) } bay(0.07,0.07,0.15,0.20) res7
fun1<- function(maxN,p1L,p1H,p0L,p0H,c11,c12,c1,c2,beta,alpha){ d<-do.call(rbind,lapply(2:(maxN-6),function(m1) do.call(rbind,lapply(2:(maxN-m1-4),function(n1) do.call(rbind,lapply(0:m1,function(x1) do.call(rbind,lapply(0:n1,function(y1) data.frame(m1,n1,x1,y1))))))))) colnames(d)<-c("m1","n1","x1","y1") d1<-within(d,{ term1_p1<- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, log=FALSE) term1_p0<- dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE) }) set.seed(8) d2<-do.call(rbind,lapply(seq_len(nrow(d)),function(i){ Pm<- rbeta(1000,0.2+d[i,"x1"],0.8+d[i,"m1"]-d[i,"x1"]); Pn<- rbeta(1000,0.2+d[i,"y1"],0.8+d[i,"n1"]-d[i,"y1"]); Fm<- ecdf(Pm); Fn<- ecdf(Pn); #Fmm<- Fm(d[i,"p11"]); #Fnn<- Fn(d[i,"p12"]); Fmm<- Fm(p1L); Fnn<- Fn(p1H); R<- (Fmm+Fnn)/2; Fmm_f<- max(R, Fmm); Fnn_f<- min(R, Fnn); Qm<- 1-Fmm_f; Qn<- 1-Fnn_f; data.frame(Qm,Qn)})) d2<-cbind(d1,d2) library(zoo) lst1<- split(d2,list(d$m1,d$n1)) d2New<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){ x[,9:14]<-NA; x[,9:10][x$Qm<=c11,]<-cumsum(x[,5:6][x$Qm<=c11,]); x[,11:12][x$Qn<=c12,]<-cumsum(x[,5:6][x$Qn<=c12,]); x[,13:14]<-cumsum(x[,5:6]); colnames(x)[9:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1"); x1<-na.locf(x); x1[,9:14][is.na(x1[,9:14])]<-0; x1} )) row.names(d2New)<-1:nrow(d2New) res1<-aggregate(.~m1+n1,data=d2New[,c(1:2,9:12)],max) d3<-res1[res1[,4]<=beta & res1[,6]<beta,] f1<-function(dat){ stopifnot(nrow(dat)!=0) library(plyr) join(dat,d2New,type="inner") } d4<- f1(d3) res2<-do.call(rbind,lapply(unique(d4$m1),function(m1) do.call(rbind,lapply(unique(d4$n1),function(n1) do.call(rbind,lapply(unique(d4$x1),function(x1) do.call(rbind,lapply(unique(d4$y1),function(y1) ? #do.call(rbind,lapply(0:m1,function(x1) #do.call(rbind,lapply(0:n1,function(y1) do.call(rbind,lapply((m1+2):(maxN-2-n1),function(m) do.call(rbind,lapply((n1+2):(maxN-m),function(n) do.call(rbind,lapply(x1:(x1+m-m1), function(x) do.call(rbind,lapply(y1:(y1+n-n1), function(y) data.frame(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) colnames(res2)<- c("m1","n1","x1","y1","m","n","x","y") res3<- join(res2,d4,by=c("m1","n1","x1","y1"),type="inner") res3<-res3[,c(1:16)] res3New<- within(res3,{ term2_p0<- dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, log=FALSE); term2_p1<- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, log=FALSE)}) res4<-do.call(rbind,lapply(seq_len(nrow(res3New)),function(i){ Pm2<-rbeta(1000,0.2+res3[i,"x"],0.8+res3[i,"m"]-res3[i,"x"]); Pn2<- rbeta(1000,0.2+res3[i,"y"],0.8+res3[i,"n"]-res3[i,"y"]); Fm2<- ecdf(Pm2); Fn2<- ecdf(Pn2); #Fmm2<- Fm2(res3[i,"p1"]); #Fnn2<- Fn2(res3[i,"p2"]); Fmm2<- Fm2(p1L); Fnn2<- Fn2(p1H); R2<- (Fmm2+Fnn2)/2; Fmm_f2<- max(R2, Fmm2); Fnn_f2<- min(R2, Fnn2); Qm2<- 1-Fmm_f2; Qn2<- 1-Fnn_f2; data.frame(Qm2,Qn2)})) res5<-cbind(res3New,res4) lst2<- split(res5,list(res5$m1,res5$n1,res5$m,res5$n)) ? res5New<-do.call(rbind,lapply(lst2[lapply(lst2,nrow)!=0], function(x){ x[,21:26]<-NA; x[,21:22][x$Qm2<=c1 & x$Qm>c11,]<-cumsum(x[,17:18][x$Qm2<=c1 & x$Qm>c11,]); x[,23:24][x$Qn2<=c2 & x$Qn>c12,]<-cumsum(x[,17:18][x$Qn2<=c2 & x$Qn>c12,]); x[,25:26]<-cumsum(x[,17:18]); colnames(x)[21:26]<- c("cterm2_P1L","cterm2_P0L","cterm2_P1H","cterm2_P0H","sumT2p0","sumT2p1"); x2<-na.locf(x); x2[,21:26][is.na(x2[,21:26])]<-0; x2} )) row.names(res5New)<-1:nrow(res5New) res6<-aggregate(.~m1+n1+m+n,data=res5New[,c(1:6,9:12,21:24)] ,max) res6New<-within(res6,{AL<-1-(cterm1_P0L + cterm2_P0L); AH<-1-(cterm1_P0H + cterm2_P0H); BL<-(cterm1_P1L + cterm2_P1L); BH<-(cterm1_P1H + cterm2_P1H); EN<- m1 + (m-m1)*(1-cterm1_P0L) + n1 + (n-n1)*(1-cterm1_P0H); N<-m+n }) res7<-res6New[,c(1:4,7,9,15:20)] res7New<-res7[res7[,9]<=beta & res7[,10]<=beta &res7[,11] <= alpha & res7[,12] <= alpha,] return(res7New) } ###Different scenarios ####1 ?fun1(10,0.25,0.25,0.05,0.05,0.07,0.07,0.15,0.20,0.6,0.6) #Joining by: m1, n1, cterm1_P0L, cterm1_P1L, cterm1_P0H, cterm1_P1H #?? m1 n1 m n cterm1_P0L cterm1_P0H? N?????? EN??????? BH??????? BL??????? AH #8?? 2? 3 4 5? 0.7737809? 0.7737809? 9 5.904876 0.2373047 0.4202271 0.2262191 #11? 2? 3 5 5? 0.7737809? 0.7737809 10 6.131095 0.2748470 0.3744965 0.1631941 #12? 3? 3 5 5? 0.8573750? 0.7350919 10 6.815066 0.2342920 0.4218750 0.1703707 #14? 2? 3 4 6? 0.7737809? 0.7737809 10 6.131095 0.2373047 0.4202271 0.2262191 #15? 2? 4 4 6? 0.7350919? 0.7350919 10 7.059632 0.1779785 0.3942719 0.2649081 #????????? AL #8? 0.1100501 #11 0.1158586 #12 0.1426250 #14 0.1100501 #15 0.1138223 ####2 fun1(10,0.25,0.25,0.05,0.05,0.07,0.07,0.15,0.20,0.1,0.1) #Error: nrow(dat) != 0 is not TRUE ####3 running again previous case fun1(10,0.25,0.25,0.05,0.05,0.07,0.07,0.15,0.20,0.6,0.6) Joining by: m1, n1, cterm1_P0L, cterm1_P1L, cterm1_P0H, cterm1_P1H #?? m1 n1 m n cterm1_P0L cterm1_P0H? N?????? EN??????? BH??????? BL??????? AH #8?? 2? 3 4 5? 0.7737809? 0.7737809? 9 5.904876 0.2373047 0.4202271 0.2262191 #11? 2? 3 5 5? 0.7737809? 0.7737809 10 6.131095 0.2748470 0.3744965 0.1631941 #12? 3? 3 5 5? 0.8573750? 0.7350919 10 6.815066 0.2342920 0.4218750 0.1703707 #14? 2? 3 4 6? 0.7737809? 0.7737809 10 6.131095 0.2373047 0.4202271 0.2262191 #15? 2? 4 4 6? 0.7350919? 0.7350919 10 7.059632 0.1779785 0.3942719 0.2649081 #????????? AL #8? 0.1100501 #11 0.1158586 #12 0.1426250 #14 0.1100501 #15 0.1138223 ####4 change beta, alpha fun1(10,0.25,0.25,0.05,0.05,0.07,0.07,0.15,0.20,0.4,0.4) #Joining by: m1, n1, cterm1_P0L, cterm1_P1L, cterm1_P0H, cterm1_P1H #?? m1 n1 m n cterm1_P0L cterm1_P0H? N?????? EN??????? BH??????? BL??????? AH #8?? 2? 3 5 5? 0.7737809? 0.7737809 10 6.131095 0.2748470 0.3744965 0.1631941 #11? 2? 4 4 6? 0.7350919? 0.7350919 10 7.059632 0.1779785 0.3942719 0.2649081 #????????? AL #8? 0.1158586 #11 0.1138223 ###5 ?fun1(10,0.21,0.15,0.15,0.15,0.05,0.05,0.11,0.18,0.4,0.4) #Joining by: m1, n1, cterm1_P0L, cterm1_P1L, cterm1_P0H, cterm1_P1H # [1] m1???????? n1???????? m????????? n????????? cterm1_P0L cterm1_P0H # [7] N????????? EN???????? BH???????? BL???????? AH???????? AL??????? #<0 rows> (or 0-length row.names) ####6 fun1(10,0.15,0.25,0.05,0.06,0.07,0.07,0.15,0.20,0.5,0.4) #Joining by: m1, n1, cterm1_P0L, cterm1_P1L, cterm1_P0H, cterm1_P1H #? m1 n1 m n cterm1_P0L cterm1_P0H N EN??????? BH??????? BL??????? AH??????? AL #2? 2? 2 5 4????????? 0????????? 0 9? 9 0.1403911 0.4437053 0.3958713 0.2262191 #3? 3? 2 5 4????????? 0????????? 0 9? 9 0.1403911 0.4437053 0.3958713 0.2262191 A.K. ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Monday, March 11, 2013 10:54 PM Subject: Re: [R] cumulative sum by group and under some criteria Thanks! it works well if I open up R and run this code first. but if I change alpha<-0.1, beta<-0.1, and run the codes, the res7 not found, which is correct as d3 is empty, however, if I change back to alpha<-0.6,? beta<-0.6, and run the codes, res7 still not found, which is not correct. could you try it? On Mon, Mar 11, 2013 at 8:14 PM, arun <smartpink111 at yahoo.com> wrote: HI,> > >maxN<-10 >c11<-0.07 >c12<-0.07 >c1<-0.15 >c2<-0.20 > >p0L<-0.05 >p0H<-0.05 >p1L<-0.25 >p1H<-0.25 > >alpha<-0.6 >beta<-0.6 > > >bay<-function (c11,c12,c1,c2){ >d<-do.call(rbind,lapply(2:(maxN-6),function(m1) >do.call(rbind,lapply(2:(maxN-m1-4),function(n1) >do.call(rbind,lapply(0:m1,function(x1) >do.call(rbind,lapply(0:n1,function(y1) >expand.grid(m1,n1,x1,y1))))))))) >names(d)<-c("m1","n1","x1","y1") > >d<-within(d,{ >term1_p1<- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, log=FALSE) >term1_p0<- dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE) >})????? > >########## add Qm Qn ################################## >set.seed(8) >d1<-do.call(rbind,lapply(seq_len(nrow(d)),function(i){ >Pm<- rbeta(1000,0.2+d[i,"x1"],0.8+d[i,"m1"]-d[i,"x1"]); >Pn<- rbeta(1000,0.2+d[i,"y1"],0.8+d[i,"n1"]-d[i,"y1"]); >Fm<- ecdf(Pm); >Fn<- ecdf(Pn); >#Fmm<- Fm(d[i,"p11"]); >#Fnn<- Fn(d[i,"p12"]); >Fmm<- Fm(p1L); >Fnn<- Fn(p1H); >R<- (Fmm+Fnn)/2; >Fmm_f<- max(R, Fmm); >Fnn_f<- min(R, Fnn); >Qm<- 1-Fmm_f; >Qn<- 1-Fnn_f; >data.frame(Qm,Qn)})) >d2<-cbind(d,d1) > > >library(zoo) >lst1<- split(d2,list(d$m1,d$n1)) >d2<-do.call(rbind,lapply(lst1[lapply(lst1,nrow)!=0],function(x){ >x[,9:14]<-NA; >x[,9:10][x$Qm<=c11,]<-cumsum(x[,5:6][x$Qm<=c11,]); >x[,11:12][x$Qn<=c12,]<-cumsum(x[,5:6][x$Qn<=c12,]); >x[,13:14]<-cumsum(x[,5:6]); >colnames(x)[9:14]<- c("cterm1_P0L","cterm1_P1L","cterm1_P0H","cterm1_P1H","sumTerm1_p0","sumTerm1_p1"); >x1<-na.locf(x); >x1[,9:14][is.na(x1[,9:14])]<-0; >x1} >)) >row.names(d2)<-1:nrow(d2) > > >res1<-aggregate(.~m1+n1,data=d2[,c(1:2,9:12)],max) > >d3<-res1[res1[,4]<=beta & res1[,6]<beta,] >f1<-function(dat){ >stopifnot(nrow(dat)!=0) >library(plyr) > >join(dat,d2,by=c("m1","n1"),type="inner") >}###not present > >d4<-f1(d3)###not run > >######################### >################ 2nd stage ################################ >#### this method not look at the combination of m1 and n1, >#### so need to merger the orginal data 'd2 > >res2<-do.call(rbind,lapply(unique(d4$m1),function(m1) >do.call(rbind,lapply(unique(d4$n1),function(n1) >do.call(rbind,lapply(unique(d4$x1),function(x1) >do.call(rbind,lapply(unique(d4$y1),function(y1) >? >#do.call(rbind,lapply(0:m1,function(x1) >#do.call(rbind,lapply(0:n1,function(y1) >do.call(rbind,lapply((m1+2):(maxN-2-n1),function(m) >do.call(rbind,lapply((n1+2):(maxN-m),function(n) >do.call(rbind,lapply(x1:(x1+m-m1), function(x) >do.call(rbind,lapply(y1:(y1+n-n1), function(y) >expand.grid(m1,n1,x1,y1,m,n,x,y)) ))))))))))))))) > >names(res2)<- c("m1","n1","x1","y1","m","n","x","y") >attr(res2,"out.attrs")<-NULL > >res3<- join(res2,d4,by=c("m1","n1","x1","y1"),type="inner") >res3<-res3[,c(1:16)] >############################################################# add more variables another method ############################# >res3<- within(res3,{ >term2_p0<- dbinom(x1,m1, p0L, log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, log=FALSE); >term2_p1<- dbinom(x1,m1, p1L, log=FALSE)* dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, log=FALSE)}) > >#term2_p0<- term1_p0*dbinom(x-x1,m-m1, p0L, log=FALSE)* dbinom(y-y1,n-n1,p0H, log=FALSE); >#term2_p1<- term1_p1*dbinom(x-x1,m-m1, p1L, log=FALSE)* dbinom(y-y1,n-n1,p1H, log=FALSE)}) > >res4<-do.call(rbind,lapply(seq_len(nrow(res3)),function(i){ >Pm2<-rbeta(1000,0.2+res3[i,"x"],0.8+res3[i,"m"]-res3[i,"x"]); >Pn2<- rbeta(1000,0.2+res3[i,"y"],0.8+res3[i,"n"]-res3[i,"y"]); >Fm2<- ecdf(Pm2); >Fn2<- ecdf(Pn2); >#Fmm2<- Fm2(res3[i,"p1"]); >#Fnn2<- Fn2(res3[i,"p2"]); > >Fmm2<- Fm2(p1L); >Fnn2<- Fn2(p1H); > >R2<- (Fmm2+Fnn2)/2; >Fmm_f2<- max(R2, Fmm2); >Fnn_f2<- min(R2, Fnn2); >Qm2<- 1-Fmm_f2; >Qn2<- 1-Fnn_f2; >data.frame(Qm2,Qn2)})) > >res5<-cbind(res3,res4) >################################################# calculate cumulative sum ################################# > >lst2<- split(res5,list(res5$m1,res5$n1,res5$m,res5$n)) >? >res5<-do.call(rbind,lapply(lst2[lapply(lst2,nrow)!=0], >function(x){ >x[,21:26]<-NA; >x[,21:22][x$Qm2<=c1 & x$Qm>c11,]<-cumsum(x[,17:18][x$Qm2<=c1 & x$Qm>c11,]); >x[,23:24][x$Qn2<=c2 & x$Qn>c12,]<-cumsum(x[,17:18][x$Qn2<=c2 & x$Qn>c12,]); >x[,25:26]<-cumsum(x[,17:18]); > >colnames(x)[21:26]<- c("cterm2_P1L","cterm2_P0L","cterm2_P1H","cterm2_P0H","sumT2p0","sumT2p1"); >x2<-na.locf(x); >x2[,21:26][is.na(x2[,21:26])]<-0; x2} >)) >row.names(res5)<-1:nrow(res5) >res6<-aggregate(.~m1+n1+m+n,data=res5[,c(1:6,9:12,21:24)] ,max) >res6<-within(res6,{AL<-1-(cterm1_P0L + cterm2_P0L); >AH<-1-(cterm1_P0H + cterm2_P0H); >BL<-(cterm1_P1L + cterm2_P1L); >BH<-(cterm1_P1H + cterm2_P1H); >EN<- m1 + (m-m1)*(1-cterm1_P0L) + n1 + (n-n1)*(1-cterm1_P0H); >N<-m+n >}) >res7<-res6[,c(1:4,7,9,15:20)] >res7<-res7[res7[,9]<=beta & res7[,10]<=beta &res7[,11] <= alpha & res7[,12] <= alpha,] >} >bay(0.07,0.07,0.15,0.20) >res7 > >?head(res7) >#? m1 n1 m n cterm1_P0L cterm1_P0H? N?????? EN??????? BH??????? BL??????? AH >#1? 2? 2 4 4? 0.8167625????????? 0? 8 6.366475 0.1001129 0.4702148 0.3365796 >#2? 2? 2 5 4? 0.8167625????????? 0? 9 6.549713 0.2002258 0.4405518 0.2038955 >#3? 3? 2 5 4? 0.8573750????????? 0? 9 7.285250 0.2002258 0.4218750 0.2038955 >#4? 2? 2 6 4? 0.8167625????????? 0 10 6.732950 0.1689405 0.4558468 0.2121882 >#5? 3? 2 6 4? 0.8573750????????? 0 10 7.427875 0.1689405 0.4781885 0.2121882 >#6? 4? 2 6 4? 0.8145062????????? 0 10 8.370988 0.1689405 0.3914909 0.2121882 >?# ??????? AL >#1 0.10585941 >#2 0.10972831 >#3 0.14262500 >#4 0.05037883 >#5 0.04808759 >#6 0.05944387 > > > > > >A.K. >
Hi, con<-file("Rout1112.text") ?Lines1<- readLines(con) ?close(con) ?indx<-rep(rep(c(TRUE,FALSE),each=2),2) ?Lines2<-Lines1[!grepl("[A-Za-z]",Lines1)] res<-read.table(text=paste(gsub("^\\d+\\s+","",Lines2[indx]),gsub("^\\d+\\s+","",Lines2[!indx])),sep="",header=FALSE) ?nm1<-unlist(strsplit(gsub("^ +","",paste(Lines1[grepl("[A-Za-z]",Lines1)][1:2],collapse=" "))," ")) colnames(res)<- nm1[nm1!=""] res #m1 n1? m n cterm1_P0L cterm1_P0H? c11? c12?? c1?? c2 alpha beta?? T_error? N #1? 4? 5 11 9? 0.8145062? 0.6302494 0.03 0.03 0.15 0.15?? 0.1? 0.4 0.6339515 20 #2? 7? 4 11 8? 0.6983373? 0.0000000 0.03 0.03 0.15 0.15?? 0.1? 0.4 0.4899431 19 #3? 4? 5 10 9? 0.8145062? 0.6302494 0.03 0.03 0.15 0.20?? 0.1? 0.4 0.6831988 19 #4? 5? 4? 9 8? 0.7737809? 0.0000000 0.03 0.03 0.15 0.20?? 0.1? 0.4 0.6458095 17 ?# ????? EN???????? BH??????? BL???????? AH???????? AL #1 11.77746 0.12159579 0.3846999 0.09567271 0.03198315 #2 16.20665 0.09819012 0.2550532 0.09581478 0.04088503 #3 11.59196 0.15166360 0.3943098 0.08405337 0.05317206 #4 13.90488 0.14031630 0.3624458 0.08268336 0.06036395 A.K. ________________________________ From: Joanna Zhang <zjoanna2013 at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Thursday, March 21, 2013 3:03 PM Subject: Re: [R] cumulative sum by group and under some criteria Hi, I used a computer cluster and output the file attached here, I need to read the file in R. I tried to use read.table, but it seems that the format is not recognized by R, could you help?