Search code examples
rstring-matchingreplicate

How to check if the value "TRUE" occurs consecutively for x number of times in R?


I am trying to find 'x' or more consecutive missing dates for each group in R. My current approach involves:

  • Using a for loop over each group
  • Find missing dates
  • Find how many of these missing dates are consecutive (here I get a logical vector, saying where the missing dates are consecutive or not.

This is where I am stuck. How to check from the logical vector, if "TRUE" occurs consecutively for 'x' number of times or higher.

logical_vector <- c("TRUE", "TRUE", "TRUE", "FALSE", "TRUE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE")

For example, in the above vector, how do you check if the value "TRUE" occurred 4 times or higher consecutively?

I think it is something very easy, but I cant figure this out and have been stuck for a while. Especially since the 'x' number of times or higher condition needs to be satisfied.

If it does occur 4 times or higher, should we store that as a logical vector as well?

Any help is appreciated.


Solution

  • Keep logical values as logical, not string, and keep all your vectors in a list, then we can loop through them get the index where it meets the criteria, see example:

    # example list of logical vectors 
    l <- list(
      v1 = c(TRUE, TRUE, TRUE, FALSE, TRUE,  FALSE, TRUE,  TRUE, TRUE, TRUE),
      v2 = c(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE),
      v3 = c(TRUE, TRUE, TRUE, TRUE,  TRUE,  FALSE, FALSE, TRUE, TRUE, TRUE))
    
    # get index vector with 4 consequitive TRUE
    ix <- sapply(l, function(i){
      r <- rle(i) 
      any(r$lengths[ r$values ] >= 4)
      })
    
    #get the names of vectors
    names(ix)[ ix ]
    #[1] "v1" "v3"
    
    # subset if needed
    l[ ix ]
    # $v1
    # [1]  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE
    # 
    # $v3
    # [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE