Remove timestamp rows in a dataset, when a conditional pattern is True

I have a dataset, df, and would like to record the 'Connect' datetime and remove an Action where:

I would like to first

1. Identify the 'Connect' value timestamp

2. Check the action that follows, and check to see if the next action

   is an 'Ended' or 'Attempt' with a less than or equal to 60 second gap

3. If this <= gap of 60 second is present, I wish for the code to Skip these timestamps

   and keep iterating until it comes to the next 'Ended' value, and to record this value.

The output should always follow a 'Connect' and 'Ended' pattern

We start with:



Connect            4/6/2020 1:11:41 PM



Then look to the next line:



Ended              4/6/2020 1:14:20 PM



Now look to the line that follows:



Attempt            4/6/2020 1:15:20 PM





These two timestamps are less than or equal to 60 seconds, so we keep going    

until we come across an Ended value where these conditions do not apply. 

So the Ended value of 



Ended              4/6/2020 2:05:18 PM    gets recorded.









Action             Time



Connect            4/6/2020 1:11:41 PM

Ended              4/6/2020 1:14:20 PM

Attempt            4/6/2020 1:15:20 PM

Connect            4/6/2020 1:15:21 PM

Ended              4/6/2020 2:05:18 PM

Connect            3/31/2020 11:00:08 AM

Ended              3/31/2020 11:14:54 AM

Ended              3/31/2020 4:17:43 PM

Essentially, I would like to remove this part of the dataset:

Ended              4/6/2020 1:14:20 PM

Attempt            4/6/2020 1:15:20 PM

Connect            4/6/2020 1:15:21 PM

Ended              3/31/2020 4:17:43 PM

Desired Output:

Action              Time



Connect             4/6/2020 1:11:41 PM        

Ended               4/6/2020 2:05:18 PM

Connect             3/31/2020 11:00:08 AM

Ended               3/31/2020 11:14:54 AM

The output pattern should always follow a 'Connect' and 'Ended'

Dput:

structure(list(Action = structure(c(2L, 3L, 1L, 2L, 3L, 2L, 3L, 

3L), .Label = c("Attempt", "Connect", "Ended"), class =     "factor"), 

 Time = structure(c(4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L), .Label =      c("3/31/2020 11:00:08 AM", 

 "3/31/2020 11:14:54 AM", "3/31/2020 4:17:43 PM", "4/6/2020      1:11:41 PM", 

  "4/6/2020 1:14:20 PM", "4/6/2020 1:15:20 PM", "4/6/2020  1:15:21   PM", 

 "4/6/2020 2:05:18 PM"), class = "factor")), class =     "data.frame", row.names = c(NA, 

-8L))

This is what I am thinking:

library(lubridate)

  if (value <= 60) {

   print("") 

   } else {

   Expr2

   }

or perhaps an index that follows a condition

I am thinking that I should use a loop, but not exactly sure on how to construct this. Any help is appreciated.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.