I am trying to filter a dataset of depth data and a corresponding date.
The Time column is a POSIXct format = "%Y%m%d%H%M%S"
.
This is how my data looks like:
Depth Time
0.1 2018-06-24 01:26:40
0.2 2018-06-24 01:26:41
0.2 2018-06-24 01:26:56
0.1 2018-06-24 01:26:57
0.1 2018-06-24 01:26:58
0.1 2018-06-24 01:26:59
0.1 2018-06-24 01:27:14
0.1 2018-06-24 01:27:15
0.1 2018-06-24 01:27:16
0.1 2018-06-24 01:27:17
0.1 2018-06-24 01:27:30
I would like to create a dataframe containing the same data but only with a new entry every 15 seconds. My data is sometimes continuous and sometimes there is a gap in the recorded time.
This would be my desired output:
Depth Time
0.2 2018-06-24 01:26:41
0.2 2018-06-24 01:26:56
0.1 2018-06-24 01:27:14
0.1 2018-06-24 01:27:30
I tried using the time difference between rows which works for the parts of the dataset that is consecutive:
dt_filter <- d_cor %>%
mutate(diff = Time - lag(Time, default = first(Time)))
if ((dt_filter$diff < 2) ) {
dt_filter_1 <- dt_filter[seq(1, nrow(dt_filter), 15), ]
}
but that gives me a problem once I try to add the ones in that are not consecutive:
dt_filter_15 <- dt_filter %>%
filter(diff >= 15 )
Since then I don't always have an interval of 15 secs and it obviously doesn't count them in.
So far I could not find a function that is able to filter my Time column. I am quite new to R so not too familiar with writing my own loops which I guess is neccessary...and time data does not make it easier.
Thanks for any help!
EDIT
@Ben Thanks for the quick replies!
That is some of the output that I've got:
Depth Time diff cumdiff x
0.1 2018-06-23 23:59:44 1 1030 0
0.0 2018-06-24 00:01:02 78 1035 5
0.0 2018-06-24 00:01:03 1 1036 1
between the last two lines is only 1s of difference, but it is still added to cumdiff and therefore counted in the x column