This is my example code:
pdata <- tibble(
id = rep(1:5, each = 5),
time = rep(2016:2020, times = 5),
value = c(c(1,1,1,0,1), c(1,1,0,1,1), c(1,1,1,0,1), c(1,1,1,1,1), c(1,0,1,1,1))
)
How can I select all the rows with a 0 plus just the one next row (regardless of the value)? I'd like to have an outcome like that:
# A tibble: 25 × 4
id time value condition
<int> <int> <dbl> <lgl>
1 1 2016 1 TRUE
2 1 2017 1 TRUE
3 1 2018 1 TRUE
4 1 2019 0 FALSE
5 1 2020 1 FALSE
6 2 2016 1 TRUE
7 2 2017 1 TRUE
8 2 2018 0 FALSE
9 2 2019 1 FALSE
10 2 2020 1 TRUE
# … with 15 more rows
Importantly, I want to be able to filter both rows (0 and the next one) out of my data set afterwards.
I've tried the mutate()
function and a for loop, but nothing has worked, neither have other posts on stackoverflow. Thanks for your help!
CodePudding user response:
Using a combination of filter()
and lag()
from the tidyverse
library(tidyverse)
pdata %>%
filter(value == 0 | lag(value) == 0)