Reputation: 995
What is a simple way to calculate the annual rate of change of a variable in R, that is not a time series (for example GDP, Inflation, etc)?
Upvotes: 1
Views: 6857
Reputation: 995
The package dplyr
helps with its lag()
function, that is not for a ts (time series) data frame. Here an example:
#using the library "dplyr"
library(dplyr)
#setting seed
set.seed(20)
#creating a random dataframe
df <- data.frame(date=paste(rep(2000:2017, each=4),"Q",rep(1:4, 18)), GDP= cumsum(sample(c(-0.5, 3), 72, TRUE)))
#calculating the annual percentage change
df <- df %>% mutate(change=(GDP-lag(GDP,4))/lag(GDP,4)*100)
In case of monthly data, simply set: lag(VAR,12)
where VAR stands for the variable of interest
Upvotes: 3