Created
April 3, 2017 18:06
-
-
Save ClaytonJY/76fa5cedd64a8b3dbd51adcc5e8226ec to your computer and use it in GitHub Desktop.
Two-in-a-row below a threshold
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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