Skip to content

Instantly share code, notes, and snippets.

View sysilviakim's full-sized avatar

Seo-young Silvia Kim sysilviakim

View GitHub Profile
@sysilviakim
sysilviakim / swirl.R
Created February 11, 2023 02:35
Instruction on how to install swirl and swirl courses
# Swirl and swirl lessons installation guide
install.packages("swirl")
library(swirl) # load the swirl package
## To start a swirl lesson,
## Type in `swirl()`
## These courses contain some useful lessons to do via swirl.
install_course_github("kosukeimai", "qss-swirl")
install_course("R Programming")
@sysilviakim
sysilviakim / pew-social-media.R
Created December 7, 2022 20:57
Social Media Usage by Demographic Groups
library(tidyverse)
library(Kmisc)
## https://www.pewresearch.org/internet/fact-sheet/social-media/
# Read tables quickly ==========================================================
pew_01 <- read.table(
text = "Facebook Instagram LinkedIn
Total 69% 40% 28%
Men 61% 36% 31%
Women 77% 44% 26%
@sysilviakim
sysilviakim / pres_turnout_millenium.R
Created November 2, 2021 19:57
Presidential Years Turnout
library(tidyverse)
library(rvest)
library(Kmisc)
url <- paste0(
"https://www.presidency.ucsb.edu/statistics/data/",
"voter-turnout-in-presidential-elections"
)
vap <- read_html(url) %>%
@sysilviakim
sysilviakim / swing_2020_turnout.R
Created November 2, 2021 19:43
Calculate average turnout of swing vs. non-swing states
library(tidyverse)
library(readxl)
library(janitor)
download.file(
url =
"https://www2.census.gov/programs-surveys/cps/tables/p20/585/table04a.xlsx",
destfile = "turnout2020.xlsx",
mode = "wb"
)
@sysilviakim
sysilviakim / polarization_656.R
Created October 26, 2021 18:22
Figures created for 656 class
library(dplyr)
library(ggplot2)
library(ggridges)
library(Kmisc)
# Brookings Party Unity Votes ==================================================
url <-
"https://www.brookings.edu/wp-content/uploads/2017/01/vitalstats_ch8_tbl3.csv"
df <- read.csv(url)
@sysilviakim
sysilviakim / renv_init.R
Last active January 20, 2021 23:06
Basic project start-up
renv::init()
install.packages("devtools")
install.packages("remotes")
install.packages("colorspace")
library(remotes)
install_github(
"sysilviakim/Kmisc", INSTALL_opts = c("--no-multiarch"), dependencies = TRUE
)
Kmisc::proj_skeleton()
@sysilviakim
sysilviakim / pres_scatter.R
Created September 21, 2020 09:07
Comparison of State-Level Two-Party Presidential Vote Share: 2012 and 2016
# Library and data import ======================================================
library(tidyverse)
library(scales)
library(ggrepel)
library(Kmisc) # devtools::install_github("sysilviakim/Kmisc")
options(digits = 5, scipen = 999)
# MEDSL Data: U.S. President 1976–2016
# https://doi.org/10.7910/DVN/42MVDX
# This data file contains constituency (state_po-level) returns for elections
@sysilviakim
sysilviakim / binomial_frequentist_simulation.R
Last active November 1, 2021 03:43
Simulation of Probability Estimate with Repeated Experiments (Coin Toss)
library(tidyverse)
set.seed(100)
df <- tibble(
n = seq(1e5),
x = rbinom(1e5, 1, 0.5),
y = cumsum(x) / n
)
## In base R, plot(df$n, df$y)
@sysilviakim
sysilviakim / redistricting-authorities.R
Last active March 8, 2022 21:39
Scrape Congressional Redistricting Authorities by State
library(dplyr)
library(tibble)
library(rvest)
library(xtable)
url_redist <- "https://ballotpedia.org/State-by-state_redistricting_procedures"
state_df <- read_html(url_redist) %>%
html_table(fill = TRUE) %>%
## Fourth table is the congressional redistricting table, after manual insp.
.[[4]] %>%
@sysilviakim
sysilviakim / population-per-elector.R
Last active September 2, 2020 21:50
How to Scrape/Calculate State-by-state Population Per Electoral College Elector
library(tidyverse)
library(rvest)
library(jsonlite)
library(xtable)
## How many Electoral College electors per state? ==============================
## Page as is for Sep 1, 2020
url_270 <- "https://www.270towin.com"
electors <- read_html(url_270) %>%
html_nodes("script") %>%