[R] Conditional cumulative sum

2012-01-26 Thread Axel Urbiz
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  00 0
 2  00 0
 3  11 1
 4  10 1
 5  01 1
 6  10 1
 7  11 2
 8  11 3
 9  10 3
10 01 3

Thank you.

Regards,
Axel.

[[alternative HTML version deleted]]

__
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.


Re: [R] Conditional cumulative sum

2012-01-26 Thread Pete Brecknock

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  00 0
  2  00 0
  3  11 1
  4  10 1
  5  01 1
  6  10 1
  7  11 2
  8  11 3
  9  10 3
 10 01 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  00  0
2  00  0
3  11  1
4  10  1
5  01  1
6  10  1
7  11  2
8  11  3
9  10  3
10 01  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.