## Example 1: Regular time series
t <- 1:5
x <- c(10, 20, 30, 40, 50)
## Lag by one period
get_ts_val(x, t, lag = 1)
# [1] NA 10 20 30 40
## Lead by one period
get_ts_val(x, t, lag = -1)
# [1] 20 30 40 50 NA
## Example 2: Time series with a gap
t_gap <- c(1, 2, 4, 5)
x_gap <- c(10, 20, 40, 50)
## Lag by one period: note the NA at time 4
get_ts_val(x_gap, t_gap, lag = 1)
# [1] NA 10 NA 40
## Explanation:
## - At t = 4, t - 1 = 3 does not exist in t_gap, so NA is returned.
## Example 3: Higher-order lags
get_ts_val(x_gap, t_gap, lag = 2)
# [1] NA NA NA 20
Run the code above in your browser using DataLab