Excluding segments of time that last less than x

First time poster, I hope I do this the correct way...

Okay, so I have a dataframe that looks like this

Stationary
Heartrate Datetime Behaviour Dog Visitor Variable
104 2018-10-16 10:19:33 NA NA NA Stationary
104 2018-10-16 10:19:34 NA NA NA Stationary
104 2018-10-16 10:19:35 NA NA NA Stationary

As you can see, my Datetime is in seconds, and each row equals one second of Stationary behaviour.
My goal here is to write a code that makes Rstudio exclude each segment lasting less than 120 seconds. So, if the Datetime jumps from 2018-10-16 10:19:33 to 2018-10-16 10:22:45, these will be recognized as seperate events, and the continuous time will be registrated. All segments less than 120 seconds long (120 rows with continuous time) should be excluded from the rest of my analysis in order to establish a mean heart rate.

I'm very new to Rstudio, and have not yet found a code that helps me do this, perhaps because I'm not really sure what I'm looking for. Any pointers would be awesome!

Hi, and welcome!

Please see the FAQ: What's a reproducible example (`reprex`) and how do I do one? Using a reprex, complete with representative data will attract quicker and more answers.

Date arithmetic in combination with data frames can be tricky. What I'd outline, for lack of a reprex is using dplyr::mutate(cumulative_interval = FUNCTION) to create a new variable to keep a running tally of cumulative time, then we'd figure out how to delete the rows back to the last row with a cumulative time less than 30 from rows with cumulative times that end greater than 30. It will be tricky, but the first step is to look at the duration() function in the lubridate package to get familiar with one way to handle date arithmetic.

2 Likes

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