D. Studer
D. Studer

Reputation: 1875

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?

Upvotes: 1

Views: 66

Answers (2)

r2evans
r2evans

Reputation: 160952

Base R:

df2 <- as.data.frame(lapply(df, function(a) c(1, a[-1] / a[-length(a)])))
df2
#     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

I'm inferring the first one should be "1.0" since there is no growth on the first. One could also easily argue that the first should be NA. Over to you.

Upvotes: 2

akrun
akrun

Reputation: 887891

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(., `/`))

Upvotes: 2

Related Questions