0
You don’t have to group_by
in this case:
library(dplyr)
df <- data.frame(ano = 2001:2010, indice = runif(10))
df %>%
mutate(variacao = (indice/lag(indice, 1, order_by = ano) - 1)*100)
#> ano indice variacao
#> 1 2001 0.4541271 NA
#> 2 2002 0.9796882 115.72997
#> 3 2003 0.6316009 -35.53042
#> 4 2004 0.7608055 20.45669
#> 5 2005 0.6707154 -11.84141
#> 6 2006 0.5535272 -17.47212
#> 7 2007 0.4728882 -14.56821
#> 8 2008 0.1902217 -59.77449
#> 9 2009 0.3954753 107.90230
#> 10 2010 0.8191409 107.12822
Created on 2019-04-29 by the reprex package (v0.2.1)
Daniel Falbel, thanks to his reply I was able to accomplish what I wanted. Thank you!
– João Sousa