Skip to content

Instantly share code, notes, and snippets.

@ClaytonJY
Created April 3, 2017 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ClaytonJY/76fa5cedd64a8b3dbd51adcc5e8226ec to your computer and use it in GitHub Desktop.
Save ClaytonJY/76fa5cedd64a8b3dbd51adcc5e8226ec to your computer and use it in GitHub Desktop.
Two-in-a-row below a threshold
# x is assumed to be a numeric vector
# output will be TRUE if both that value and the previous value are strictly below the threshold, FALSE otherwise
f <- function(x, threshold) {
less_than <- (x < threshold)
less_than & c(FALSE, !diff(less_than))
}
#### usage ####
x <- c(9,8,7,6,8,9,4,3,3,9)
f(x, 8)
library(dplyr)
tbl <- tibble(id = LETTERS[1:10], val = x)
tbl
tbl %>% mutate(two_in_a_row = f(x, 8))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment