Annual percentage change in R with dplyr

Asked

Viewed 244 times

0

I wanted to use the package dplyr to calculate the annual change in the IFGF from one year to the next and from 2006 to 2016.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Any suggestions? I made some unsuccessful attempts like:

graca_evolucao_ifgf%>%

select(Ano, IFGF) %>%

group_by(Ano) %>%

mutate(Variacao = (IFGF/lag(IFGF))-1*100)

1 answer

2

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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.