Tutorial Exercises Week 3
Question 1
Install the R package "WDI". This is the World Bank Development Indicators R package. It is a way to access the data on https://data.worldbank.org/ conveniently within R.
Search for the name of indicator which gives the annual inflation in consumer prices (in %). Do this by using the WDIsearch() function to search for “inflation”.
Once you have the name of the indicator, use the WDI() function to get the annual inflation rate in the Netherlands. Use the following four arguments in the WDI() function:
indicator =[the indicator you found in the first step (surrounded by quotes)]country = "NLD"start = 1970end = 2022
What was the inflation rate in the Netherlands in 2022 according to these data?
Write your answer with 3 digits after the decimal
Question 2
What was the average annual inflation rate in consumer prices (in %) over the period 2000-2019?
Write your answer with 3 digits after the decimal.
Question 3
What was the median annual inflation rate in consumer prices (in %) in years that end in a zero between 1970-2020 (so the years 1970, 1980, 1990, 2000, 2010, 2020)?
Write your answer with 3 digits after the decimal.
Question 4
The WDI() function returns a dataframe with the most-recent year first. You want to sort the data so that the years are ascending.
Which of the following commands achieves that goal?
df <- df[order(df$year), ]df <- df[order(df$year, decreasing = TRUE), ]df <- df[sort(df$year), ]df <- df[sort(df$year, ascending = TRUE), ]
Also sort your data by year for the remaining questions.
Question 5
Create a vector called inflation_change which is the change in inflation from the previous year:
- The first element should be the difference in the inflation rate between 1971 and 1970 (inflation 1971 minus inflation 1970).
- The second element should be the difference in the inflation rate between 1972 and 1971.
- …
- The last element should be difference in the inflation rate between 2022 and 2021.
This vector should have 52 elements. We don’t have a change for 1970 because we don’t observe the inflation rate in 1969.
What is the value of the 5th element in your vector? Report your answer with 3 numbers after the decimal.
Question 6
You want to add this inflation_change vector as a variable to your dataframe which is sorted ascending by year.
However, because your vector has only 52 elements (because we do not know the value of inflation in 1969), we cannot add it directly. Instead, we replace the value for the change of inflation in 1970 with a missing value indicator NA.
Which of the following commands will add the inflation_change variable correctly in your data?
df$inflation_change <- c(inflation_change, NA)df$inflation_change <- c(NA, inflation_change)df$inflation_change <- inflation_change