jenea22
jenea22

Reputation: 11

How can I calculate the inflation rate in a new variable?

So I have this table, and I need to find the inflation rate per quarter using this data. This is Econometrics, so I believe the formula that I have to use is this: Inflation rate=(CPIprevious-CPIcurrent/CPIcurrent. This has to be done in a new variable called Inflation Rate.

          Country Year Quarter Business confidence         CPI
1   Australia 2000       1           100.74487       1.57632489
2   Australia 2000       2           100.33320       2.11182007
3   Australia 2000       3            99.93596       2.85817122
4   Australia 2000       4            98.47090       2.82927131
5   Australia 2001       1            98.46135       2.78136511
6   Australia 2001       2            99.32677       3.08314356

Can someone help me?

I have tried to look for ways to do it on YouTube and on ChatGPT but I didn't find anything. If anyone knows a video that could help me, please show it to me.

Upvotes: 0

Views: 101

Answers (1)

Adriano Mello
Adriano Mello

Reputation: 2022

Here it is:

library(tidyverse)

toy_data <- tibble::tribble(
  ~Country,    ~Year, ~Quarter, ~Business_confidence,       ~CPI,
  "Australia",  2000,        1,            100.74487, 1.57632489,
  "Australia",  2000,        2,             100.3332, 2.11182007,
  "Australia",  2000,        3,             99.93596, 2.85817122,
  "Australia",  2000,        4,              98.4709, 2.82927131,
  "Australia",  2001,        1,             98.46135, 2.78136511,
  "Australia",  2001,        2,             99.32677, 3.08314356)


# Suppose you have - or want - the `lag(default = ...)`
CPI_1999_Q4 <- 2

# If you don't, `default` is `NA`
new_data <- mutate(
  toy_data,
  rate = (lag(CPI, n = 1L, default = CPI_1999_Q4) - CPI) / CPI)

The output:

> new_data
# A tibble: 6 × 6
  Country    Year Quarter Business_confidence   CPI    rate
  <chr>     <dbl>   <dbl>               <dbl> <dbl>   <dbl>
1 Australia  2000       1               101.   1.58  0.269 
2 Australia  2000       2               100.   2.11 -0.254 
3 Australia  2000       3                99.9  2.86 -0.261 
4 Australia  2000       4                98.5  2.83  0.0102
5 Australia  2001       1                98.5  2.78  0.0172
6 Australia  2001       2                99.3  3.08 -0.0979

dplyr::lag and dplyr::lead references here and a SO previous question on basic lag in R vector or dataframe.

Created on 2024-05-09 with reprex v2.1.0

Upvotes: 0

Related Questions