Skip to content

Instantly share code, notes, and snippets.

@thoughtfulbloke
thoughtfulbloke / UKelection2019unofficial.csv
Created December 14, 2019 04:42
Scraped Election Results for UK 2019 election, as no official repository exists (source BBC news)
We can't make this file beautiful and searchable because it's too large.
"constituency","party","candidate","attribute","value"
"Aberavon","Labour","Stephen Kinnock","Votes:",17008
"Aberavon","Labour","Stephen Kinnock","Vote share %:",53.8
"Aberavon","Labour","Stephen Kinnock","Vote share change:",-14.3
"Aberavon","Conservative","Charlotte Lang","Votes:",6518
"Aberavon","Conservative","Charlotte Lang","Vote share %:",20.6
"Aberavon","Conservative","Charlotte Lang","Vote share change:",2.9
"Aberavon","The Brexit Party","Glenda Davies","Votes:",3108
"Aberavon","The Brexit Party","Glenda Davies","Vote share %:",9.8
"Aberavon","The Brexit Party","Glenda Davies","Vote share change:",9.8
@thoughtfulbloke
thoughtfulbloke / honest_rents_dec19.R
Created December 9, 2019 05:57
rent change in NZ
library(readr)
geo_mean <- read_csv("https://www.mbie.govt.nz/assets/Data-Files/Building-and-construction/Tenancy-and-housing/Rental-bond-data/Territorial-Authority/ta-geometric-mean.csv",
col_types = cols(
.default = col_double(),
Month = col_date(format = "")
))
library(dplyr)
library(ggplot2)
geo_mean %>% filter(Month >= as.Date("2009-01-01")) %>%
mutate(govt = c(rep("National", 105), rep("Labour",25))) %>%
@thoughtfulbloke
thoughtfulbloke / datacharter_responders.R
Created November 22, 2019 04:15
Code for making graph
# tweets are assumed to be gathered for this
library(stringr)
library(dplyr)
library(tidyr)
library(ggplot2)
library(ggthemes)
library(ggrepel)
library(Rtsne)
disc <- read.csv("discussion.csv", stringsAsFactors = FALSE)
#globe map without countries distorted by polygon points being clipped by the "edge of the world"
#This is my hacking around of the fuller script
#https://github.com/r-spatial/sf/blob/master/demo/twitter.R
#linked to from section 8.1 of https://keen-swartz-3146c4.netlify.com/plotting.html
library(sf)
library(maptools)
library(animation)
data(wrld_simpl)
@thoughtfulbloke
thoughtfulbloke / botypatterns2019.R
Created November 13, 2019 23:40
Voting similarity among Bird of the Year 2019 candidates
library(Rtsne)
library(vroom)
library(dplyr)
library(tidyr)
library(stringr)
library(ggplot2)
library(ggrepel)
library(janitor)
# data from https://www.dragonfly.co.nz/news/2019-11-12-boty.html
@thoughtfulbloke
thoughtfulbloke / breath.R
Created October 31, 2019 00:16
making animated breathing gif in #rstats
library(ggplot2)
library(dplyr)
library(gganimate)
exhale <- 5 # seconds
exhaled_pause <- 3 # seconds
inhale <- 4 # seconds
inhaled_pause <- 4 # seconds
frames_per_second = 16
exh <- data.frame(stage = rep("exhale", exhale*frames_per_second),
# my solution to https://github.com/thoughtfulbloke/tourgroups
library(dplyr)
library(tidyr)
tours <- read.csv("tours.csv",stringsAsFactors = FALSE)
visitors <- read.csv("visitors.csv",stringsAsFactors = FALSE)
# make it about choices
library(rvest)
library(dplyr)
library(tidyr)
library(ggplot2)
library(stringr)
library(ggbeeswarm)
html = read_html("https://www.electionresults.govt.nz/electionresults_2017/electorate-status.html")
lnks <- (html %>% html_nodes("a") %>% html_attr("href"))[25:95]
urls <- tibble(lnks) %>% separate(lnks, into=c("before","url"), sep=2)
x <- urls$url[1]
@thoughtfulbloke
thoughtfulbloke / dcc.stv.csv
Created October 13, 2019 02:45
counting of dunedin city council election results
Iteration Candidate Votes Change
1 BARBOUR-EVANS Scout 729 NA
1 BARKER Sophie 1209 NA
1 BARLIN Bob 867 NA
1 BENSON-POPE David 1926 NA
1 CAMPBELL Finn 664 NA
1 DAVIE-NITIS Sarah 973 NA
1 ELDER Rachel 1227 NA
1 FORSYTH Hugh 126 NA
1 GAREY Christine 3822 NA
@thoughtfulbloke
thoughtfulbloke / Waitangi15.R
Last active October 6, 2019 22:30
Change in opinion about Treaty of Waiting over 2002-2017
library(foreign) # read NZESin SPSS files
library(dplyr) # general data manipulation
library(tidyr) # restructuring
library(ggplot2)
library(ggthemes)
# NZES files in nzes subfolder
nz17 <- suppressWarnings(read.spss("nzes/NZES2017Release14-07-19.sav",
to.data.frame = TRUE, add.undeclared.levels = "sort"))
nz17meta <- data.frame(varnames = attributes(nz17)$names, eyear=2017,