3

I am coding analytics on a financial signal that has output of -1, 0, or 1 and am trying to track how many days it has been since the signal has changed for a daily analytics report.

example: if the tail of my df is 0 1 0 0 0 0 ...

end result goal is a vector like: 0 0 0 1 2 3

How might I go about this? My first thought was a for loop of some kind but I only know how to write those in Python.

  • Does this answer your question? [Cumulative sum that resets when 0 is encountered](https://stackoverflow.com/questions/32501902/cumulative-sum-that-resets-when-0-is-encountered). Of course, you would want when `x != 0`. Best of luck! – Andrew Jun 05 '20 at 15:04

3 Answers3

7

You could simply use rle and sequence:

vec <- c(0, 1, 0, 0, 0, 0)

with(rle(vec), sequence(lengths) - 1)
[1] 0 0 0 1 2 3
Onyambu
  • 67,392
  • 3
  • 24
  • 53
1

An option with rleid from data.table

library(data.table)
sequence(table(rleid(vec)))-1
#[1] 0 0 0 1 2 3

Or use tabulate as @Andrew suggested (which would be more faster)

sequence(tabulate(rleid(vec)))-1

data

vec <- c(0, 1, 0, 0, 0, 0)
akrun
  • 874,273
  • 37
  • 540
  • 662
0

something like this could work. It depends on the structure of your data, you might need to switch length with nrow for instance

data <- c(0,1,0,0,0,0)

c( rep(0,which(data == 1)+1), 1:(length(data)-which(data == 1))-1 )

output:

> data 
[1] 0 1 0 0 0 0

>output
[1] 0 0 0 0 1 2 3
Daniel O
  • 4,258
  • 6
  • 20
  • not quite, the output should be 0 0 0 1 2 3. I flagged another answer that works perfectly above. – jakewillms17 Jun 05 '20 at 15:12
  • Fixed the answer, but agree the other answer is cleaner. – Daniel O Jun 05 '20 at 16:04
  • 1
    This will not work. Recal the signal can be 1, -1 or even 0. You have not taken -1 into consideration. Also you code simply cannot work eg `data <- c(0,1,0,0,0,1,1,1)` cannot run for your data – Onyambu Jun 05 '20 at 17:03
  • @Onyambu The example given does not properly encapsulate the whole problem. But I appreciate your thoroughness. I was trying to think of a way to answer this problem with `diff` Do you have any ideas? – Daniel O Jun 05 '20 at 17:40
  • You cannot use diff here. Recall the data has 1,0,and -1. Check the solution I provided above. The example has `...` which literally means its not the end of the vector – Onyambu Jun 05 '20 at 17:41