Sequence of win streaks
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I'm trying to calculate a sequence of win streaks for a binary vector. Given a vector
set.seed(2)
x <- sample(c(0,1), 10, replace = TRUE)
[1] 0 1 1 0 1 1 0 1 0 1
I want to calculate the cumulative sum of ones with a "reset" every time there's a zero.
So, in this case, the output of the function should be
[1] 0 1 2 0 1 2 0 1 0 1
What's the easiest way to do this on R?
r
add a comment |Â
up vote
6
down vote
favorite
I'm trying to calculate a sequence of win streaks for a binary vector. Given a vector
set.seed(2)
x <- sample(c(0,1), 10, replace = TRUE)
[1] 0 1 1 0 1 1 0 1 0 1
I want to calculate the cumulative sum of ones with a "reset" every time there's a zero.
So, in this case, the output of the function should be
[1] 0 1 2 0 1 2 0 1 0 1
What's the easiest way to do this on R?
r
1
See Count how many consecutive values are true and 'Linked' therein.
– Henrik
Aug 13 at 7:06
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I'm trying to calculate a sequence of win streaks for a binary vector. Given a vector
set.seed(2)
x <- sample(c(0,1), 10, replace = TRUE)
[1] 0 1 1 0 1 1 0 1 0 1
I want to calculate the cumulative sum of ones with a "reset" every time there's a zero.
So, in this case, the output of the function should be
[1] 0 1 2 0 1 2 0 1 0 1
What's the easiest way to do this on R?
r
I'm trying to calculate a sequence of win streaks for a binary vector. Given a vector
set.seed(2)
x <- sample(c(0,1), 10, replace = TRUE)
[1] 0 1 1 0 1 1 0 1 0 1
I want to calculate the cumulative sum of ones with a "reset" every time there's a zero.
So, in this case, the output of the function should be
[1] 0 1 2 0 1 2 0 1 0 1
What's the easiest way to do this on R?
r
asked Aug 13 at 1:28
user3294195
5991420
5991420
1
See Count how many consecutive values are true and 'Linked' therein.
– Henrik
Aug 13 at 7:06
add a comment |Â
1
See Count how many consecutive values are true and 'Linked' therein.
– Henrik
Aug 13 at 7:06
1
1
See Count how many consecutive values are true and 'Linked' therein.
– Henrik
Aug 13 at 7:06
See Count how many consecutive values are true and 'Linked' therein.
– Henrik
Aug 13 at 7:06
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
11
down vote
accepted
We can use ave
and create a grouping variable with cumsum
at every occurrence of 0 in the vector and count the consecutive numbers without 0 in each group.
ave(x, cumsum(x==0), FUN = seq_along) - 1
#[1] 0 1 2 0 1 2 0 1 0 1
1
This will be plenty fast for almost any occasion, but if someone is looking for an Rcpp option for this query - see here stackoverflow.com/a/51054267/496803
– thelatemail
Aug 13 at 1:55
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
accepted
We can use ave
and create a grouping variable with cumsum
at every occurrence of 0 in the vector and count the consecutive numbers without 0 in each group.
ave(x, cumsum(x==0), FUN = seq_along) - 1
#[1] 0 1 2 0 1 2 0 1 0 1
1
This will be plenty fast for almost any occasion, but if someone is looking for an Rcpp option for this query - see here stackoverflow.com/a/51054267/496803
– thelatemail
Aug 13 at 1:55
add a comment |Â
up vote
11
down vote
accepted
We can use ave
and create a grouping variable with cumsum
at every occurrence of 0 in the vector and count the consecutive numbers without 0 in each group.
ave(x, cumsum(x==0), FUN = seq_along) - 1
#[1] 0 1 2 0 1 2 0 1 0 1
1
This will be plenty fast for almost any occasion, but if someone is looking for an Rcpp option for this query - see here stackoverflow.com/a/51054267/496803
– thelatemail
Aug 13 at 1:55
add a comment |Â
up vote
11
down vote
accepted
up vote
11
down vote
accepted
We can use ave
and create a grouping variable with cumsum
at every occurrence of 0 in the vector and count the consecutive numbers without 0 in each group.
ave(x, cumsum(x==0), FUN = seq_along) - 1
#[1] 0 1 2 0 1 2 0 1 0 1
We can use ave
and create a grouping variable with cumsum
at every occurrence of 0 in the vector and count the consecutive numbers without 0 in each group.
ave(x, cumsum(x==0), FUN = seq_along) - 1
#[1] 0 1 2 0 1 2 0 1 0 1
answered Aug 13 at 1:31


Ronak Shah
22.2k83451
22.2k83451
1
This will be plenty fast for almost any occasion, but if someone is looking for an Rcpp option for this query - see here stackoverflow.com/a/51054267/496803
– thelatemail
Aug 13 at 1:55
add a comment |Â
1
This will be plenty fast for almost any occasion, but if someone is looking for an Rcpp option for this query - see here stackoverflow.com/a/51054267/496803
– thelatemail
Aug 13 at 1:55
1
1
This will be plenty fast for almost any occasion, but if someone is looking for an Rcpp option for this query - see here stackoverflow.com/a/51054267/496803
– thelatemail
Aug 13 at 1:55
This will be plenty fast for almost any occasion, but if someone is looking for an Rcpp option for this query - see here stackoverflow.com/a/51054267/496803
– thelatemail
Aug 13 at 1:55
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51814151%2fsequence-of-win-streaks%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
See Count how many consecutive values are true and 'Linked' therein.
– Henrik
Aug 13 at 7:06