## Example 1: Regular time series
t <- 1:5
x <- c(10, 20, 30, 40, 50)
get_diff(x, t)
# [1] NA 10 10 10 10
## Example 2: Time series with a gap
t_gap <- c(1, 2, 4, 5)
x_gap <- c(10, 20, 40, 50)
get_diff(x_gap, t_gap)
# [1] NA 10 NA 10
## Explanation:
## At t = 4, the previous period t-1 = 3 does not exist, so the difference is NA.
## Example 3: Comparison with diff()
diff(x_gap)
# [1] 10 20 10
Run the code above in your browser using DataLab