Skip to content
Prev 317861 / 398506 Next

cumulative sum by group and under some criteria

Hi,

If you don't want the m1=3, n1=3 combination in the final dataset:
library(plyr)
res2<- join(res1,d3,by=c("m1","n1"),type="inner")
tail(res2)
#??? m1 n1 x1 y1 m n x y cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H
#235? 3? 2? 2? 1 5 4 3 1?? 0.857375????? 0.512???? 0.9025?????? 0.64
#236? 3? 2? 2? 1 5 4 3 2?? 0.857375????? 0.512???? 0.9025?????? 0.64
#237? 3? 2? 2? 1 5 4 3 3?? 0.857375????? 0.512???? 0.9025?????? 0.64
#238? 3? 2? 2? 1 5 4 4 1?? 0.857375????? 0.512???? 0.9025?????? 0.64
#239? 3? 2? 2? 1 5 4 4 2?? 0.857375????? 0.512???? 0.9025?????? 0.64
#240? 3? 2? 2? 1 5 4 4 3?? 0.857375????? 0.512???? 0.9025?????? 0.64
A.K.




----- Original Message -----
From: Zjoanna <Zjoanna2013 at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Monday, February 18, 2013 10:36 PM
Subject: Re: [R] cumulative sum by group and under some criteria

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

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

library(plyr)
res2<- join(res1,d3,by=c("m1","n1"),type="full")
res2

On Sun, Feb 17, 2013 at 11:10 PM, arun kirshna [via R] <
ml-node+s789695n4658895h56 at n4.nabble.com> wrote:

            
--
View this message in context: http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4659018.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.