Time series to Duration series

ts_duration(
  y,
  timeVec = stats::time(y),
  fs = stats::frequency(y),
  tolerance = 0
)

Arguments

y

A time series, numeric vector, or categorical variable.

timeVec

A vector, same length as y containing timestamps, or, sample indices.

fs

Optional sampling frequency if timeVec represents sample indices. An extra column duration.fs will be added which represents 1/fs * duration in samples

tolerance

A number tol indicating a range [y-tol,y+tol] to consider the same value. Useful when y is continuous (default = 0)

Value

A data frame

Examples

library(invctr)
# Create data with events and their timecodes
coder <- data.frame(beh=c("stare","stare","coffee","type","type","stare"),t=c(0,5,10,15,20,25))

ts_duration(y = coder$beh, timeVec = coder$t)
#>   y y.name t.start t.end duration.time duration.samples duration.fs
#> 1 1  stare       0    10            10                2           2
#> 2 2 coffee      10    15             5                1           1
#> 3 3   type      15    25            10                2           2
#> 4 1  stare      25    25             0                1           1