Last active
July 21, 2021 12:56
-
-
Save DavisVaughan/2cd47a52f12cb09d264b7ca829422f35 to your computer and use it in GitHub Desktop.
This file contains 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
library(slider) | |
library(dplyr) | |
library(lubridate) | |
df <- tibble( | |
x = c(.5, .8, .7, .5, .2, .06, 0, 0, 0, 0, 0, .1, .3, .2, 0), | |
date = as.Date("2005-07-20") + 0:14 | |
) | |
# Check that the window made up of "the current day plus 4 days before it" are all >0 | |
mutate(df, id = slide_index_lgl(x, date, ~all(.x > 0), .before = days(4))) | |
#> # A tibble: 15 x 3 | |
#> x date id | |
#> <dbl> <date> <lgl> | |
#> 1 0.5 2005-07-20 TRUE | |
#> 2 0.8 2005-07-21 TRUE | |
#> 3 0.7 2005-07-22 TRUE | |
#> 4 0.5 2005-07-23 TRUE | |
#> 5 0.2 2005-07-24 TRUE | |
#> 6 0.06 2005-07-25 TRUE | |
#> 7 0 2005-07-26 FALSE | |
#> 8 0 2005-07-27 FALSE | |
#> 9 0 2005-07-28 FALSE | |
#> 10 0 2005-07-29 FALSE | |
#> 11 0 2005-07-30 FALSE | |
#> 12 0.1 2005-07-31 FALSE | |
#> 13 0.3 2005-08-01 FALSE | |
#> 14 0.2 2005-08-02 FALSE | |
#> 15 0 2005-08-03 FALSE | |
# Could also use `slide_lgl()` without `date` if you don't have gaps / missing days | |
# in the series | |
mutate(df, id = slide_lgl(x, ~all(.x > 0), .before = 4)) | |
#> # A tibble: 15 x 3 | |
#> x date id | |
#> <dbl> <date> <lgl> | |
#> 1 0.5 2005-07-20 TRUE | |
#> 2 0.8 2005-07-21 TRUE | |
#> 3 0.7 2005-07-22 TRUE | |
#> 4 0.5 2005-07-23 TRUE | |
#> 5 0.2 2005-07-24 TRUE | |
#> 6 0.06 2005-07-25 TRUE | |
#> 7 0 2005-07-26 FALSE | |
#> 8 0 2005-07-27 FALSE | |
#> 9 0 2005-07-28 FALSE | |
#> 10 0 2005-07-29 FALSE | |
#> 11 0 2005-07-30 FALSE | |
#> 12 0.1 2005-07-31 FALSE | |
#> 13 0.3 2005-08-01 FALSE | |
#> 14 0.2 2005-08-02 FALSE | |
#> 15 0 2005-08-03 FALSE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment