[R] How do I avoid a loop?
Feng, Ken
ken.feng at citi.com
Tue Jun 19 11:11:58 CEST 2007
Hi,
I start with an array of booleans:
x <- c( TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE );
I want to define an y <- f(x) such that:
y <- c( 1, 2, 3, 0, 0, 1, 2, 0, 1 );
In other words, do a cumsum when I see a TRUE, but reset to 0 if I see a FALSE.
I know I can do this with a very slow and ugly loop or maybe use apply,
but I was hoping there are some R experts out there who can show me
a cleaner/more elegant solution?
Thanks in advance.
- Ken
More information about the R-help
mailing list