Search code examples
rlagsapply

Lag Dataframe in R


I have the following data.frame:

A <- c(10,
       12,
       14.4,
       17.28,
       20.736)
B <- c(6,
       7.8,
       10.14,
       13.182,
       17.1366)

df <- data.frame(A, B)
df

Which looks like this:

       A       B
1 10.000  6.0000
2 12.000  7.8000
3 14.400 10.1400
4 17.280 13.1820
5 20.736 17.1366

Now, I'd like to have the exact table, but with growth factors:

   A   B
1  1   1
2  1.2 1.3
3  1.2 1.3
4  1.2 1.3
5  1.2 1.3

So the "lag" should be one position: the next value should be divided by the precedent value. Is there a function for this?


Solution

  • If the values shouldn't update for the next iteration

    library(dplyr)
    df %>%
       mutate_all(~ ./lag(., default = first(.)))
    #   A   B
    #1 1.0 1.0
    #2 1.2 1.3
    #3 1.2 1.3
    #4 1.2 1.3
    #5 1.2 1.3
    

    If values needs to be updated, we can use accumulate from purrr

    df %>% 
         mutate(A =  purrr::accumulate(A, ~ .x/.y))
    

    Or for multiple columns

    df %>%
            mutate_all(~ purrr::accumulate(., `/`))