time_since_event: Calculate "time since event" in a vector

View source: R/sequence_utilities.R

time_since_eventR Documentation

Calculate "time since event" in a vector

Description

Calculates the time since the nearest event in a sequence, optionally using a custom time scale.

Usage

time_since_event(yvar, tvar = seq_along(yvar))

timeSinceEvent(...)

Arguments

yvar

A numeric or logical vector indicating events.

tvar

An optional numeric vector specifying time values. Defaults to the index.

...

Arguments pased on to time_since_event

Details

Events are coded as 1 (or TRUE). Non-events are anything else. The result includes absolute and signed distances to events.

Value

A data frame with columns 'yvar', 'tvar', 'abs.tse' (absolute time since event), 'sign.tse' (signed time since event), and other helper columns.

Author(s)

Søren Højsgaard, sorenh@math.aau.dk

See Also

subSeq, rle

Examples


## Example 1: Basic usage with default time index
y <- c(0, 0, 1, 0, 0, 1, 0)
tse <- time_since_event(y)
print(tse)

## Example 2: Custom (non-integer) time variable
y <- c(0, 0, 1, 0, 0, 0, 1, 0)
t <- seq(0.5, 3.5, length.out = length(y))
tse <- time_since_event(y, t)
print(tse)

## Example 3: Plotting the signed time since event
plot(sign.tse ~ tvar, data = tse, type = "b",
     main = "Signed time since event",
     xlab = "Time", ylab = "Signed time since event")
grid()
abline(h = 0, col = "red", lty = 2)


doBy documentation built on June 30, 2025, 1:06 a.m.