'Check for consecutive values in R

I have data which looks something like the following:

enter image description here

I would like to write a conditional statement which returns TRUE if 3 or more of the 'flag' values consecutively are TRUE, but I'm not sure how to implement this.

I've put in an example data set below:

example <- data.frame(
  value = c(20, 19.5, 14, 14, 25, 19, 23, 25, 30, 33, 38),
  flag = c(F, F,  F, F, T, F, F, T, T, T, T)
)
r


Solution 1:[1]

Two ways, with slightly different results depending on which you need.

  1. rle, as already mentioned:

    r <- rle(example$flag)
    r
    # Run Length Encoding
    #   lengths: int [1:4] 4 1 2 4
    #   values : logi [1:4] FALSE FALSE FALSE TRUE
    r$lengths >= 3 & r$values
    # [1] FALSE FALSE FALSE  TRUE
    

    This tells us that the fourth sequence is both TRUE and 3 or longer. We can re-assign to values for those that are true but not long enough with

    r$values <- r$lengths > 3 & r$values
    

    and then invert it:

    example$flag3 <- inverse.rle(r)
    example
    #    value  flag flag3
    # 1   20.0 FALSE FALSE
    # 2   19.5 FALSE FALSE
    # 3   14.0 FALSE FALSE
    # 4   14.0 FALSE FALSE
    # 5   25.0  TRUE FALSE
    # 6   19.0 FALSE FALSE
    # 7   23.0 FALSE FALSE
    # 8   25.0  TRUE  TRUE
    # 9   30.0  TRUE  TRUE
    # 10  33.0  TRUE  TRUE
    # 11  38.0  TRUE  TRUE
    
  2. Window operations:

    example$flag3b <- zoo::rollapplyr(example$flag, 3, function(z) length(z) == 3 & all(z), partial = TRUE)
    example
    #    value  flag flag3 flag3b
    # 1   20.0 FALSE FALSE  FALSE
    # 2   19.5 FALSE FALSE  FALSE
    # 3   14.0 FALSE FALSE  FALSE
    # 4   14.0 FALSE FALSE  FALSE
    # 5   25.0  TRUE FALSE  FALSE
    # 6   19.0 FALSE FALSE  FALSE
    # 7   23.0 FALSE FALSE  FALSE
    # 8   25.0  TRUE  TRUE  FALSE
    # 9   30.0  TRUE  TRUE  FALSE
    # 10  33.0  TRUE  TRUE   TRUE
    # 11  38.0  TRUE  TRUE   TRUE
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 r2evans