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

R: filter rows within timerange from previous rows

let’s imagine I have dataframe like this

event time type
1 2022-07-15 18:08:05 def
2 2022-07-15 18:08:06 att
3 2022-07-15 18:09:00 def
4 2022-07-15 18:09:12 def
5 2022-07-15 18:13:26 def
6 2022-07-15 18:13:36 att
7 2022-07-15 18:19:05 def
8 2022-07-15 18:21:43 def
9 2022-07-15 18:26:06 att
10 2022-07-15 18:27:26 def

I want to leave only rows with type ‘att’ that occurred within 15 sec after rows with type ‘def’.

So desired output is:

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

event time type
2 2022-07-15 18:08:06 att
6 2022-07-15 18:13:36 att

Could you help me please to figure it out?

>Solution :

You could use dplyr::filter() and dplyr::lag() here. Assuming your time variable is in datetime format:

library(dplyr)

df %>%
  filter(type == "att", 
         (time - dplyr::lag(time)) < 15, # could also use `(difftime(time, lag(time), units = "secs")) < 15`
         lag(type) == "def")

#   event                time type
# 1     2 2022-07-15 18:08:06  att
# 2     6 2022-07-15 18:13:36  att

Data:

df <- read.table(text = "event  time    type
1   '2022-07-15 18:08:05'   def
2   '2022-07-15 18:08:06'   att
3   '2022-07-15 18:09:00'   def
4   '2022-07-15 18:09:12'   def
5   '2022-07-15 18:13:26'   def
6   '2022-07-15 18:13:36'   att
7   '2022-07-15 18:19:05'   def
8   '2022-07-15 18:21:43'   def
9   '2022-07-15 18:26:06'   att
10  '2022-07-15 18:27:26'   def", h = TRUE)

df$time <- lubridate::as_datetime(df$time)
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