Skip to content

Instantly share code, notes, and snippets.

@danweitzel
danweitzel / democracy_overtime.R
Last active April 11, 2024 22:34
Plotting change and level of electoral democracy in R
# Libraries
# If vdemdata is not installed run below:
#install.packages("devtools")
#devtools::install_github("vdeminstitute/vdemdata", force = TRUE)
library(tidyverse)
library(vdemdata)
# Overhead
set.seed(1904)
theme_set(theme_bw())
@danweitzel
danweitzel / r_intro.R
Created February 9, 2024 02:51
Intro to R for Colorado State University Straayer Center
## Title: Straayer Center Democracy Hackathon
## Instructor: Prof. Daniel Weitzel
## Email: daniel.weitzel@colostate.edu
## Date: 2024-01-29
## Hello and welcome to the Straayer Center Democracy Hackathon!
## In today's session you'll learn how to use R to generate insights from data
## We'll visualize and transform data with a few lines of code!
## Notes for people new to R
@danweitzel
danweitzel / dyadic_parlgov.R
Last active May 8, 2023 15:26
Dyadic Parlgov
# Purpose: Make the ParlGov data dyadic
## Library
library("tidyverse")
## Download the most recent ParlGov election data from the website
df_parlgov <- read_csv("https://www.parlgov.org/data/parlgov-development_csv-utf-8/view_election.csv") |>
filter(election_type == "parliament")
## Generate a sender data set with country, election and party IDs. Rename the party ID sender_id
@danweitzel
danweitzel / conflict_year_dataframe.R
Last active March 2, 2021 17:35
Generate a conflict-year observations data set
##Load libraries
library("tidyverse")
library("lubridate")
## Generate fake data set
df <- tibble(country = rep(c("A", "B"), each = 3),
year_start = c("15jun1816", "04april1918", "01oct2010", "15jun1917", "04april1918", "01oct2010"),
year_end = c("31dec1816", "13may1920", "02oct2010", "31dec1921", "13may1920", "02oct2012"),
conflict_id = seq(1, 6, by=1))
@danweitzel
danweitzel / wvs_worldbank_merge.R
Created February 15, 2021 09:50
Merge World Value Survey and Worlbank data
## Load the libraries
library("tidyverse") # Used for cleaning the data set
library("countrycode") # Used for establishing a unique country identifier in both data sets that allows us to merge
## Data sets are available here:
## https://www.worldvaluessurvey.org/WVSDocumentationWV7.jsp
## https://databank.worldbank.org/source/education-statistics-%5e-all-indicators#
## Load the data
df_gpai <- read_csv("5f29aa54-f976-4948-9e56-85e30763cb54_Data.csv")
@danweitzel
danweitzel / change_indicator.R
Last active February 4, 2021 20:58
Change indicator
## Load the library
library("tidyverse")
## Generate a fake data set with two countries, six years, and a changing indicator
df <- tibble(country = rep(c("A", "B"), each = 6),
year = rep(seq(1:6), times = 2),
ind = c(1,1,2,1,2,1,1,1,1,1,2,1))
## Make a dummy called 'changed' that is TRUE when 'ind' changed in the last three years and FALSE otherwise
## Working with rowMeans calculation, assuming that NA indicates NO change
@danweitzel
danweitzel / transform_merge.R
Last active February 4, 2021 20:58
Example of transformation for event df merge
library("tidyverse")
df_event <- data.frame(country = c("A", "A", "B"),
event = c(1,0,1),
min_date = c(1990,1994,1993),
max_date = c(1996, 1999, 1997))
df <- data.frame(country = c(rep("A", 20), rep("B", 20)),
year = c(rep(seq(1989, 2008, by = 1),2)))