Skip to content

Instantly share code, notes, and snippets.

install.packages("needs")
library(needs)
needs(sjlabelled, tidyverse, haven, magrittr, ggrepel, sf)
# Load in data from all waves
USoc_indresp_1 <- read_dta("/path to wave 1/", encoding = "latin1")
USoc_indresp_2 <- read_dta("/path to wave 2/", encoding = "latin1")
etc
# Join all waves together, keeping the most recent driving licence record for each respondent, and their vote in the last election
area_name per_week_per_100k pop
Aragon 194 1344184
Navarra 154 633017
Catalonia 921 7463471
Basque Country 136 2179532
La Rioja 12 319939
Madrid 229 6373532
Valencia 128 4989631
Extremadura 10 1096421
Murcia 67 1453545
# Install and load required packages
install.packages("needs")
library(needs)
needs(tidyverse, magrittr, animation, pdftools, png, scales)
# Function that extracts data from Google Mobility PDFs
process_google_mobility <- function(country_code, start_date, end_date){
# Convert first page of PDF into high-res PNG
pdf_convert(paste0("https://www.gstatic.com/covid19/mobility/",end_date,"_",country_code,"_Mobility_Report_en.pdf"), format = "png", pages = 1, dpi = 300, filenames = "IMG1.png")
read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv") %>%
gather(date, cases, 5:ncol(.)) %>%
mutate(date = as.Date(date, "%m/%d/%y")) %>%
group_by(country = `Country/Region`, date) %>%
summarise(cases = sum(cases)) %>%
filter(country != "Others" & country != "Mainland China") %>%
bind_rows(
tibble(country = "Republic of Korea", date = as.Date("2020-03-11"), cases = 7755)
) %>%
group_by(country) %>%
install.packages("needs")
library(needs)
needs(tidyverse, magrittr, rvest, sf, raster, rgdal)
ireland_constits <- c("https://en.wikipedia.org/wiki/Carlow%E2%80%93Kilkenny_(D%C3%A1il_constituency)") %>%
read_html() %>%
html_nodes('div[aria-labelledby="Current_Dáil_constituencies"] tr:nth-child(2) ul li a') %>%
html_attr("href") %>%
tail(-1) %>%
c("/wiki/Carlow%E2%80%93Kilkenny_(D%C3%A1il_constituency)", .)
install.packages("needs")
library(needs)
needs(tidyverse, magrittr, rvest, zoo, scales)
WHO_sars_links <- read_html("https://www.who.int/csr/sars/country/en/") %>%
html_nodes("ul.auto_archive") %>%
magrittr::extract(1) %>%
html_nodes("li a") %>%
map_dfr(~{
link <- .x %>% html_attr("href") %>% paste0("https://www.who.int",.)
year value name group lastValue subGroup lat lon city_id
1575 200 Agra India 200 India 27.18333 78.01667 Agra - India
1576 212 Agra India 200 India 27.18333 78.01667 Agra - India
1577 224 Agra India 212 India 27.18333 78.01667 Agra - India
1578 236 Agra India 224 India 27.18333 78.01667 Agra - India
1579 248 Agra India 236 India 27.18333 78.01667 Agra - India
1580 260 Agra India 248 India 27.18333 78.01667 Agra - India
1581 272 Agra India 260 India 27.18333 78.01667 Agra - India
1582 284 Agra India 272 India 27.18333 78.01667 Agra - India
1583 296 Agra India 284 India 27.18333 78.01667 Agra - India
needs(tidyverse, magrittr, png)
# Create a folder for storing the charts
dir.create("BFB_images")
# Loop though constituency codes in England and Wales, downloading the chart for each on from BfB
for(pcon in c(B4B_MRP$westminster_constituency[B4B_MRP$region != "Scotland"])){
tryCatch(download.file(paste0("https://www.getvoting.org/charts/",pcon,"_1.png"), destfile = paste0("BFB_images/", pcon, ".png")), error = function(e){message("No chart")})
}
needs(sjlabelled, tidyverse, haven, magrittr)
# Load wave 8
USoc_indresp_8 <- read_dta("~/Downloads/UKDA-6614-stata/stata11_se/ukhls_w8/h_indresp.dta", encoding = "latin1")
# Load all other waves
USoc_indresp_1 <- read_dta("~/Downloads/UKDA-6614-stata/stata11_se/ukhls_w1/a_indresp.dta", encoding = "latin1")
USoc_indresp_2 <- read_dta("~/Downloads/UKDA-6614-stata/stata11_se/ukhls_w2/b_indresp.dta", encoding = "latin1")
USoc_indresp_3 <- read_dta("~/Downloads/UKDA-6614-stata/stata11_se/ukhls_w3/c_indresp.dta", encoding = "latin1")
USoc_indresp_4 <- read_dta("~/Downloads/UKDA-6614-stata/stata11_se/ukhls_w4/d_indresp.dta", encoding = "latin1")
install.packages("needs")
library(needs)
needs(tidyverse, magrittr)
# I’m using an example dataset of net national income per capita, from the World Bank (https://data.worldbank.org/indicator/NY.ADJ.NNTY.PC.CD?most_recent_value_desc=true)
head(dataset)
# Here’s a basic histogram using the ggplot defaults to show the distribution of NNI per capita across the world:
ggplot(dataset, aes(value)) +
geom_histogram()