Skip to content

Instantly share code, notes, and snippets.

@ateucher
Created May 10, 2019 18:47
Show Gist options
  • Save ateucher/81a2799e838a5a6fcd66cc6f20b14269 to your computer and use it in GitHub Desktop.
Save ateucher/81a2799e838a5a6fcd66cc6f20b14269 to your computer and use it in GitHub Desktop.
values <- runif(100)
# get runs where values > 0.75 (these are TRUE/FALSE)
runs <- rle(values > 0.75)
# Mask out the runs with lengths < 3
runs$values[runs$lengths < 3] <- FALSE
# Get a vector of positions that meet the run criteria
masked <- inverse.rle(runs)
# The above gives TRUE/FALSE but can convert to TRUE/NA like the
# original solution
masked[!masked] <- NA
masked
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment