I'm trying to figure out how to subset my dataframe to include values that go from 0 to -5 in 3 days or less. I can't seem to figure out how to code the "3 days or less" part to my filter function. Any help would be greatly appreciated!
Repro:
library ("lubridate")
library("dplyr")
library("tidyr")
data <- data.frame(date = c("2020-06-08",
"2020-06-09",
"2020-06-10",
"2020-06-11",
"2020-06-12",
"2021-06-13",
"2021-06-14",
"2021-06-15",
"2021-06-16",
"2021-06-17",
"2021-06-18",
"2021-06-19",
"2021-06-20"),
value = c(2,0,-7,1,0,-1,-2,-3,-4,0,-6,-5,10))
data$date <- as.Date(data$date)
head(data)
the new dataframe should include all the dates and values for: "0,-7,0,-6,-5" since these are the dates that go from 0 to -5 within 3 days or less.
The "0,-1,-2,-3,-4,0" should be left since it doesn't meet this condition.
I hope that makes sense!
expected output would subset the highlighted values:
final output:
CodePudding user response:
Is this what you are looking for:
data %>%
group_by(group_id = as.integer(gl(n(),3,n()))) %>%
filter(value == 0 | value <=-5) %>%
ungroup() %>%
select(-group_id)
date value
<chr> <dbl>
1 2020-06-09 0
2 2020-06-10 -7
3 2020-06-12 0
4 2021-06-17 0
5 2021-06-18 -6
6 2021-06-19 -5