September 22, 2024
wbstats
WDI
vdemlite
vdemdata
vdemlite
offers more functionality, works better in the cloudfilter()
, select()
, mutate()
Along the way we will practice some important dplyr
verbs:
filter()
is used to select observations based on their valuesselect()
is used to select variablesmutate()
is used to create new variables or modifying existing onesAs well as some helpful functions from the janitor
package.
wbstats
(or WDI
)fredr
for Federal Reserve Economic Datatidycensus
for US Census datawbstats
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
vdemdata
and vdemlite
The vdem
function from vdemdata
just downloads all of the data. Try running this code chunk. What do you see in democracy
?
filter()
==
instead of >=
<=
and see what happens=
versus ==
=
is used to assign values to variables, just like <-
==
is used to test if two values are equal to each otherfilter(year == 1990)
will give you just the observations for 1990>=
and <=
>=
is used to test if a value is greater than or equal to another value<=
is used to test if a value is less than or equal to another valuefilter(year >= 1990)
will give you the observations for 1990 and laterfilter(year <= 1990)
will give you the observations for 1990 and earlierselect()
v2x_libdem
instead of v2x_polyarchy
05:00
mutate()
polyarchy
polyarchy
squared?democracy <- vdem |> # download the V-Dem dataset
filter(year == 2015) |> # keep only observations from 2015
select( # select (and rename) these variables
country = country_name, # name before the = sign is new name
vdem_ctry_id = country_id, # name after the = sign is old name
year,
polyarchy = v2x_polyarchy
) |>
mutate(
polyarchy_dbl = polyarchy * 2 # create variable 2X polyarchy
)
glimpse(democracy)
05:00
+
addition-
subtraction*
multiplication/
division^
exponentiation (also **
)vdemdata
Example# Load packages
library(vdemdata) # to download V-Dem data
library(dplyr)
# Download the data
democracy <- vdem |> # download the V-Dem dataset
filter(year == 2015) |> # filter year, keep 2015
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")
)
# View the data
glimpse(democracy)
Use filter()
to select years…
# Download the data
democracy <- vdem |>
filter(year == 2015) |> # keep 2015
select(
country = country_name,
vdem_ctry_id = country_id,
year,
polyarchy = v2x_polyarchy,
gdp_pc = e_gdppc,
region = e_regionpol_6C
) |>
mutate(
region = case_match(region,
1 ~ "Eastern Europe",
2 ~ "Latin America",
3 ~ "Middle East",
4 ~ "Africa",
5 ~ "The West",
6 ~ "Asia")
)
Use select()
to choose variables…
# Download the data
democracy <- vdem |>
filter(year == 2015) |>
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,
1 ~ "Eastern Europe",
2 ~ "Latin America",
3 ~ "Middle East",
4 ~ "Africa",
5 ~ "The West",
6 ~ "Asia")
)
Use mutate
with case_match()
to Recode Region….
# Download the data
democracy <- vdem |>
filter(year == 2015) |>
select(
country = country_name,
vdem_ctry_id = country_id,
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
)
vdemlite
vdemlite
fuctionsfetchdem()
to download the datasummarizedem()
provides searchable table of indicators with summary statssearchdem()
to search for specific indicators or all indicators used to construct an indexfetchdem()
summarizedem()
searchdem()
searchdem()
to find an indicator you are interested in usingsummarizedem()
to get summary statistics for that variablefetchdem()
to download the data for that variable for a country or countries of interestmutate()
to add region codes to the data05:00