I am working in R and have a dataframe with various series. I need to perform on most of these columns the following two operations:
- max(0,x_t - max(x_{t-1}, x_{t-2}, x_{t-3}, x_{t-4}))
- max(0,-1 + x_t / max(x_{t-1}, x_{t-2}, x_{t-3}, x_{t-4}))
I tried this solution for one column
df %>% mutate( pmax(0,x - pmax(lag(x), lag(x,2), lag(x,3), lag(x,4))) )
but I guess it’s possible to do this for all columns and for both operations using dplyr’s across and purrr syntax. Any ideas on how to do this?