library(dplyr)
# --- USAGE ----
diff_vec(1:10, lag = 2, difference = 2) %>%
diff_inv_vec(lag = 2, difference = 2, initial_values = 1:4)
# --- VECTOR ----
# Get Change
1:10 %>% diff_vec()
# Get Acceleration
1:10 %>% diff_vec(difference = 2)
# Get approximate rate of change
1:10 %>% diff_vec(log = TRUE) %>% exp() - 1
# --- MUTATE ----
m4_daily %>%
group_by(id) %>%
mutate(difference = diff_vec(value, lag = 1)) %>%
mutate(
difference_inv = diff_inv_vec(
difference,
lag = 1,
# Add initial value to calculate the inverse difference
initial_values = value[1]
)
)
Run the code above in your browser using DataLab