Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7492513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:27:16+00:00 2026-05-29T16:27:16+00:00

I have a vector of binary variables which state whether a product is on

  • 0

I have a vector of binary variables which state whether a product is on promotion in the period. I’m trying to work out how to calculate the duration of each promotion and the duration between promotions.

promo.flag = c(1,1,0,1,0,0,1,1,1,0,1,1,0))

So in other words: if promo.flag is same as previous period then running.total + 1, else running.total is reset to 1

I’ve tried playing with apply functions and cumsum but can’t manage to get the conditional reset of running total working 🙁

The output I need is:

promo.flag =  c(1,1,0,1,0,0,1,1,1,0,1,1,0)
rolling.sum = c(1,2,1,1,1,2,1,2,3,1,1,2,0)

Can anybody shed any light on how to achieve this in R?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T16:27:17+00:00Added an answer on May 29, 2026 at 4:27 pm

    It sounds like you need run length encoding (via the rle command in base R).

    unlist(sapply(rle(promo.flag)$lengths,seq))
    

    Gives you a vector 1 2 1 1 1 2 1 2 3 1 1 2 1. Not sure what you’re going for with the zero at the end, but I assume it’s a terminal condition and easy to change after the fact.

    This works because rle() returns a list of two, one of which is named lengths and contains a compact sequence of how many times each is repeated. Then seq when fed a single integer gives you a sequence from 1 to that number. Then apply repeatedly calls seq with the single numbers in rle()$lengths, generating a list of the mini sequences. unlist then turns that list into a vector.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector<char> of data which I want to write into std::stringstream .
I have a binary file that I've disassembled using avr-objcopy. The interrupt vector table
we have a C++ class which basically reads and writes vectors from a binary
How would I get my variables out of a vector? I can't use the
I have a vector of unordered_map which is sorted based on the comparer function
I am facing a small problem. I have a struct, which has a vector.
I have a vector of type template class and I am trying to print
I have a situation in which I'm performing binary serialization of a some items
I have vector<unsigned char> filed with binary data. I need to take, lets say,
I am new to matlab, I have a column vector with binary values I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.