Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select a row on condition plus the next one

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Using a combination of filter() and lag() from the tidyverse

library(tidyverse)

pdata %>%
  filter(value == 0 | lag(value) == 0)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading