Created
December 5, 2017 17:04
-
-
Save chrishanretty/38fb0f1a1ea0195b1d8e8f61b51a1b6e to your computer and use it in GitHub Desktop.
Three elections in three years
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(tidyverse) | |
require(lubridate) | |
#Data from www.parlgov.org | |
dat <- read.csv("view_election.csv") | |
dat <- dat %>% | |
filter(election_type != "ep") %>% | |
mutate(election_date = as.Date(election_date)) %>% | |
filter(election_date > as.Date("1945-01-01")) %>% | |
distinct(country_name, election_date) %>% | |
group_by(country_name) %>% | |
arrange(election_date) %>% | |
mutate(thisYear = year(election_date), | |
lastYear = c(NA, thisYear[-n()]), | |
lastExact = c(NA, as.character(election_date[-n()])), | |
yearBefore = c(NA, lastYear[-n()]), | |
exactBefore = c(NA, lastExact[-n()]), | |
yearBefore2 = c(NA, yearBefore[-n()])) %>% | |
filter(thisYear - yearBefore <= 3) | |
class(dat) <- "data.frame" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment