Skip to content

Instantly share code, notes, and snippets.

@svmiller
Created May 22, 2017 19:13
Show Gist options
  • Save svmiller/9d053ee75f79e0bbf8d79d69a45c95fd to your computer and use it in GitHub Desktop.
Save svmiller/9d053ee75f79e0bbf8d79d69a45c95fd to your computer and use it in GitHub Desktop.
Not the most elegant, albeit still fairly "tidy" script to make Freedom House's "Freedom of the Press" data actually usable.
library(tidyverse)
library(stringr)
# Freedom of the Press data ---------
# Oh god, Freedom House, why the sam hell do you do these things...
FP <- readxl::read_excel("~/Dropbox/data/freedom-house/FOTP1980-FOTP2017_Public-Data.xlsx", sheet=2) %>%
repair_names()
# Ugh, all right. Let's do this...
FP <- slice(FP, -1:-3)
colnames(FP) <- sapply(FP[1,], as.character)
colnames(FP) <- str_c(colnames(FP), seq(1,length(colnames(FP))))
colnames(FP)[1] <- "country"
FP <- slice(FP, -1)
# I'm assuming you're interested in the "Total Score"...
FP %>%
select(country,contains("Total")) -> FP
# I'm still not the most fluent in sapply within tidyverse framework. Comments here welcome.
FP[,2:ncol(FP)] <- sapply(FP[,2:ncol(FP)],
function(x)ifelse(x == "-",NA, as.numeric(x)))
# Total Scores start for the 1993 coverage year (i.e. the 1994 report).
colnames(FP)[2:dim(FP)[2]] <- seq(1993,1993+dim(FP)[2]-2)
FP %>%
gather(year, fh, `1993`:`2016`) %>%
mutate(year = as.numeric(year)) -> FP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment