Tidy Freedom Index as an R Package

R
A concrete example about reshaping data to simplify the posterior analysis.
Author

Mauricio “Pachá” Vargas S.

Published

June 26, 2023

R and Shiny Training: If you find this blog to be interesting, please note that I offer personalized and group-based training sessions that may be reserved through Buy me a Coffee. Additionally, I provide training services in the Spanish language and am available to discuss means by which I may contribute to your Shiny project.

Updated 2023-07-05: Reflects changes in the freedomhouse package.

Motivation

As an update to Tidying the Freedom Index, I organized the final result as an R package, with the idea of making it easier to use the data in other projects.

The package is available at GitHub. The package is not available at CRAN, but you can install it from GitHub using the following code:

# install.packages("remotes")
remotes::install_github("pachadotdev/freedomhouse")

Demonstration

Let’s see how the package works. First, we load dplyr and freedomhouse package and the data.

library(dplyr)
library(freedomhouse)

Now we search for “trade union” in the sub_item_description column.

country_scores %>%
  filter(grepl("trade union", sub_item_description)) %>%
  distinct(sub_item_description)
# A tibble: 1 × 1
  sub_item_description                                                          
  <fct>                                                                         
1 Is there freedom for trade unions and similar professional or labor organizat…

Now we know that “E3” is the question about trade unions. Let’s see the question.

country_scores %>%
  filter(sub_item == "E3") %>%
  distinct(sub_item_description) %>%
  pull()
[1] Is there freedom for trade unions and similar professional or labor organizations?
25 Levels: Are individuals able to exercise the right to own property and establish private businesses without undue interference from state or nonstate actors? ...

As an example, we can filter by sub-item code and country code for trade unions in Canada

unions_canada <- country_scores %>%
  filter(
    sub_item == "E3",
    iso3c == "CAN"
  )  %>%
  inner_join(
    country_rating_texts %>%
      select(year, iso3c, sub_item, detail) %>%
      filter(
        sub_item == "E3",
        iso3c == "CAN"
      ),
    by = c("year", "iso3c", "sub_item")
  ) %>%
  select(year, iso3c, sub_item, score, detail)

unions_canada
# A tibble: 6 × 5
   year iso3c sub_item score detail                                             
  <int> <fct> <chr>    <int> <chr>                                              
1  2022 CAN   E3           4 Trade unions and business associations enjoy high …
2  2021 CAN   E3           4 Trade unions and business associations enjoy high …
3  2020 CAN   E3           4 Trade unions and business associations enjoy high …
4  2019 CAN   E3           4 Trade unions and business associations enjoy high …
5  2018 CAN   E3           4 Trade unions and business associations enjoy high …
6  2017 CAN   E3           4 Trade unions and business associations enjoy high …

Even more, we can print the justifications and see why Canada obtained the maximum score in that item (see the methodology).

unions_canada %>%
    distinct(detail) %>%
    pull()
[1] "Trade unions and business associations enjoy high levels of membership and are well organized."                                                                                                                                                                                                                                                                                                        
[2] "Trade unions and business associations enjoy high levels of membership and are well organized. In June 2017, the new Liberal government reversed two controversial labor laws approved by the previous government. The laws had been criticized by unions for putting in place onerous financial disclosure rules, and making it more difficult to organize new unions in federally regulated sectors."