Search code examples
rloopsdummy-variable

Remove indicator regimes that are less than three periods


I am looking for help with the following problem.

Suppose I have an indicator variable:

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

My indicator variable is much longer so this is not a specific problem.

I am interested in writing a loop which does the following:

if the succession of 1's in the vector above is less than 3, keep it zero.

I.e. I want to remove frequent regime shifts where 1 is only 1 or two periods.

The final vector should thus look as follows:

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

Any help in making this dynamic will be appreciated!

Nic


Solution

  • We can use rle to do this. We get the rle of 'ind', change the 'values' by subsetting 'lengths' less than 3 for values that are 1 (!!values), assign the corresponding 'values' to 0, and use inverse.rle to convert back the output to vector.

    inverse.rle(within.list(rle(ind), values[!!values][lengths[!!values]<3] <- 0))
    #[1] 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0
    

    Or a more compact version is

    inverse.rle(within.list(rle(ind), values[values & lengths<3] <- 0))
    # [1] 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0