I want to perform the following mathematical operation between two variables at different time periods of the same dataframe:
lag_var = var1(t)/var2(t-1)
> df
var1 var2 lag_var
1 10 NA
3 12 0.3
1 16 0.083
6 19 0.375
16 7 0.84
Any ideas?
CodePudding user response:
This can be done easiest in dplyr
or in base R by:
df <- read.table(text = "var1 var2
1 10
3 12
1 16
6 19
16 7", header = TRUE)
# dplyr
df$lag_dplyr <- df$var1 / dplyr::lag(df$var2)
# Base R
df$lag_base <- sapply(1:nrow(df), function(x) {
l <- df$var1 / df$var2[x - 1]
l[x]})
Output:
# var1 var2 lag_dplyr lag_base
# 1 1 10 NA NA
# 2 3 12 0.30000000 0.30000000
# 3 1 16 0.08333333 0.08333333
# 4 6 19 0.37500000 0.37500000
# 5 16 7 0.84210526 0.84210526
CodePudding user response:
You can achieve that via the lag()
function and mutate
, e.g.
library(dplyr)
df %>% mutate(lag_var = var1 / dplyr::lag(var2))
Data:
df <- read.table(text = "var1 var2 lag_var
1 10 NA
3 12 0.3
1 16 0.083
6 19 0.375
16 7 0.84", h = T)