Seems like a problem of Inter-Event Time, mainly used in time series of environmental variables . The aim is to separate/classify different events in the signal based on a Inter-Event Time where "nothing happen". I used the package IETD for rain events that could be used as well with your data. Some tricks to do are following:
library(tidyverse)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
# data
x <- c(0,0,0,0,0,0,0,0,15,40,70,80,80,80,
40,15,0,0,0,0,0,0,0,0,0,20,45,80,
80,75,60,50,20,0,0,1, 0, 5)
data_raw <- tibble(Frames = seq_along(x),
Time = runif(length(x), 4.5, 6.5),
Abs_time = Sys.time() + Time,
Force = x)
data_raw
#> # A tibble: 38 x 4
#> Frames Time Abs_time Force
#> <int> <dbl> <dttm> <dbl>
#> 1 1 5.70 2020-12-02 12:50:46 0
#> 2 2 5.53 2020-12-02 12:50:45 0
#> 3 3 5.31 2020-12-02 12:50:45 0
#> 4 4 5.37 2020-12-02 12:50:45 0
#> 5 5 6.38 2020-12-02 12:50:46 0
#> 6 6 5.05 2020-12-02 12:50:45 0
#> 7 7 4.78 2020-12-02 12:50:45 0
#> 8 8 6.13 2020-12-02 12:50:46 0
#> 9 9 5.44 2020-12-02 12:50:45 15
#> 10 10 5.54 2020-12-02 12:50:45 40
#> # … with 28 more rows
#Build a data frame with a pseudo-timestamp as first column,
# then signal column, and the everything else
data <- data_raw %>%
mutate(pseudo_date = Sys.time() + Frames * 1000) %>%
select(pseudo_date, Force, everything())
data
#> # A tibble: 38 x 5
#> pseudo_date Force Frames Time Abs_time
#> <dttm> <dbl> <int> <dbl> <dttm>
#> 1 2020-12-02 13:07:20 0 1 5.70 2020-12-02 12:50:46
#> 2 2020-12-02 13:24:00 0 2 5.53 2020-12-02 12:50:45
#> 3 2020-12-02 13:40:40 0 3 5.31 2020-12-02 12:50:45
#> 4 2020-12-02 13:57:20 0 4 5.37 2020-12-02 12:50:45
#> 5 2020-12-02 14:14:00 0 5 6.38 2020-12-02 12:50:46
#> 6 2020-12-02 14:30:40 0 6 5.05 2020-12-02 12:50:45
#> 7 2020-12-02 14:47:20 0 7 4.78 2020-12-02 12:50:45
#> 8 2020-12-02 15:04:00 0 8 6.13 2020-12-02 12:50:46
#> 9 2020-12-02 15:20:40 15 9 5.44 2020-12-02 12:50:45
#> 10 2020-12-02 15:37:20 40 10 5.54 2020-12-02 12:50:45
#> # … with 28 more rows
# separate events
IETD::drawre(as.data.frame(data), 0, 0)[[2]] %>%
#extract first row of each event
map_df(slice, n = 1)
#> pseudo_date Force Frames Time Abs_time
#> 1 2020-12-02 15:20:40 15 9 5.442333 2020-12-02 12:50:45
#> 2 2020-12-02 20:04:00 20 26 6.315026 2020-12-02 12:50:46
#> 3 2020-12-02 22:50:40 1 36 5.312519 2020-12-02 12:50:45
#> 4 2020-12-02 23:24:00 5 38 5.815844 2020-12-02 12:50:46
Created on 2020-12-02 by the reprex package (v0.3.0)
The psuedo_date
column can be calculated with a base time like Sys.Time()
plus a sequence numbers multiplied by 1000. This is needed because you data is 10 minutes long (no enough time range to use IETD functions). And the column reordering is because the drawre()
function requirements.