Tidy Freedom Index as an R Package

[This article was first published on pacha.dev/blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

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.

category_scores_items %>%
  filter(grepl("trade union", sub_item_description))
# A tibble: 1 × 4
  item  sub_item item_description                        sub_item_description   
  <chr> <chr>    <chr>                                   <chr>                  
1 E     E3       Associational and Organizational Rights Is there freedom for t…

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

category_scores_items %>%
  filter(sub_item == "E3") %>%
  pull(sub_item_description)
[1] "Is there freedom for trade unions and similar professional or labor organizations?"

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

unions_canada <- category_scores %>%
  filter(
    sub_item == "E3",
    iso3c == "CAN"
  )  %>%
  inner_join(
    country_ratings_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."
To leave a comment for the author, please follow the link and comment on their blog: pacha.dev/blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)