October 27, 2023
Viewed with View("wb_data_messy")
or by clicking on object…
read_excel()
to read in the datapivot_longer()
takes three arguments:
Can you remember how to make pivot_longer()
work?
dplyr
verbs mutate()
or mutate_at()
mutate()
is if you want to change on variablemutate_at()
is for multiple variablesjanitor
package!wbstats
WDI
vdemdata
wbstats
packagewb_search()
to find some indicators you are interested in05:00
wbstats
Example# Load packages
library(wbstats) # for downloading WB data
library(dplyr) # for selecting, renaming and mutating
library(janitor) # for rounding
# Store the list of indicators in an object
indicators <- c("flfp" = "SL.TLF.CACT.FE.ZS", "women_rep" = "SG.GEN.PARL.ZS")
# Download the data
women_emp <- wb_data(indicators, mrv = 50) |> # download data for last 50 yrs
select(!iso2c) |> # drop the iso2c code which we won't be using
rename(year = date) |> # rename date to year
mutate(
flfp = round_to_fraction(flfp, denominator = 100), # round to nearest 100th
women_rep = round_to_fraction(women_rep, denominator = 100)
)
# View the data
glimpse(women_emp)
05:00
vdem
function from vdemdata
just downloads the datadplyr
functions
filter()
for yearsselect()
for variablescase_match()
to addvdemdata
Example# Load packages
library(vdemdata) # to download V-Dem data
# Download the data
democracy <- vdem |> # download the V-Dem dataset
filter(year >= 1990) |> # filter out years less than 1990
select( # select (and rename) these variables
country = country_name, # the name before the = sign is the new name
vdem_ctry_id = country_id, # the name after the = sign is the old name
year,
polyarchy = v2x_polyarchy,
gdp_pc = e_gdppc,
region = e_regionpol_6C
) |>
mutate(
region = case_match(region, # replace the values in region with country names
1 ~ "Eastern Europe",
2 ~ "Latin America",
3 ~ "Middle East",
4 ~ "Africa",
5 ~ "The West",
6 ~ "Asia")
# number on the left of the ~ is the V-Dem region code
# we are changing the number to the country name on the right
# of the equals sign
)
# View the data
glimpse(democracy)
05:00