Sequence of win streaks

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
1












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?







share|improve this question
















  • 1




    See Count how many consecutive values are true and 'Linked' therein.
    – Henrik
    Aug 13 at 7:06














up vote
6
down vote

favorite
1












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?







share|improve this question
















  • 1




    See Count how many consecutive values are true and 'Linked' therein.
    – Henrik
    Aug 13 at 7:06












up vote
6
down vote

favorite
1









up vote
6
down vote

favorite
1






1





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?







share|improve this question












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?









share|improve this question











share|improve this question




share|improve this question










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












  • 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












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





share|improve this answer
















  • 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










Your Answer





StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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





share|improve this answer
















  • 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














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





share|improve this answer
















  • 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












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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










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












  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

Confectionery