Calculating interest rate where values are in a column

Hello everyone.
I fear this one might be a very simple question. Unfortunately I am not able to solve it...

df <- data.frame( y = c(1137,  1159))

I want to simply calculate the interest rate. So I want to do (1159/1137)-1

Thank you everyone and keep safe

You can do this with the lag() function from dplyr.

df <- data.frame( y = c(1137,  1159))
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
df <- df %>% mutate(lagValue = lag(y), Perc = (y/lagValue) - 1)
df
#>      y lagValue       Perc
#> 1 1137       NA         NA
#> 2 1159     1137 0.01934916

Created on 2021-01-25 by the reprex package (v0.3.0)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.