Axel Urbiz wrote > > Dear List, > > I'll appreciate your help on this. I'm trying to create a variable as in > "cumsum_y.cond1" below, which should compute the cumulative sum of "y" > conditional on the value of cond==1. > > set.seed(1) > d <- data.frame(y= sample(c(0,1), 10, replace= T), > cond= sample(c(0,1), 10, replace= T)) > > > y cond cumsum_y.cond1 > 1 0 0 0 > 2 0 0 0 > 3 1 1 1 > 4 1 0 1 > 5 0 1 1 > 6 1 0 1 > 7 1 1 2 > 8 1 1 3 > 9 1 0 3 > 10 0 1 3 > > Thank you. > > Regards, > Axel. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@ 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. >
is this what you are looking for ... set.seed(1) d <- data.frame(y= sample(c(0,1), 10, replace= T), cond= sample(c(0,1), 10, replace= T)) d$cumsum_y.cond1 = cumsum(d$y & d$cond) # Output y cond cumsum_y.cond1 1 0 0 0 2 0 0 0 3 1 1 1 4 1 0 1 5 0 1 1 6 1 0 1 7 1 1 2 8 1 1 3 9 1 0 3 10 0 1 3 HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Conditional-cumulative-sum-tp4332254p4332344.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@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.