Skip to content

Instantly share code, notes, and snippets.

View mroutley's full-sized avatar

Matthew Routley mroutley

View GitHub Profile
library(tidyverse)
library(magrittr)
library(cansim)
# For filtering later, here are the locations and products that we'll want
keep_GEO <- c("Canada")
keep_products <- c("Total Industrial product price index (IPPI), excluding energy and petroleum products",
"Plastic and rubber products [P32]",
"Lumber and other wood products [P41]",
"Fabricated metal products and construction materials [P63]",
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mroutley
mroutley / vpi.R
Last active November 16, 2020 22:22
Initial maps of VPI
library(readxl)
library(tidyverse)
library(magrittr)
library(rmapshaper)
# Shapefiles --------------------------------------------------------------
# Federal Electoral Districts shapefile
shape_file_url <- "http://ftp.maps.canada.ca/pub/elections_elections/Electoral-districts_Circonscription-electorale/Elections_Canada_2019/federal_electoral_districts_boundaries_2019.shp.zip"
@mroutley
mroutley / cpi.R
Last active October 3, 2021 18:31
Consumer Price Index charts
library(tidyverse)
library(magrittr)
library(cansim)
# For filtering later, here are the locations and products that we'll want
keep_GEO <- c("Canada", "Toronto, Ontario", "Ontario")
keep_products <- c("All-items", "Alcoholic beverages", "Apples",
"Transportation", "Public transportation", "Parking fees")
cansim_table <- "18-10-0004"
@mroutley
mroutley / gtfs.R
Created November 7, 2019 17:22
Experimenting with GTFS and tidytransit
library(magrittr)
ttc_gtfs <- tidytransit::feedlist %>%
dplyr::filter(t=="TTC GTFS") %>%
dplyr::pull(url_d) %>%
tidytransit::read_gtfs()
attach(ttc_gtfs)
routes %>% dplyr::inner_join(trips, by="route_id") %>%
dplyr::inner_join(stop_times) %>%
dplyr::inner_join(stops, by = "stop_id") %>%
@mroutley
mroutley / 2019_predictions.R
Last active October 20, 2019 18:02
Mapping our predictions for the 2019 Federal Election
library(magrittr)
# Federal Electoral Districts shapefile
shape_file_url <- "http://ftp.maps.canada.ca/pub/elections_elections/Electoral-districts_Circonscription-electorale/federal_electoral_districts_boundaries_2015/federal_electoral_districts_boundaries_2015_shp_en.zip"
download.file(shape_file_url,
destfile = "data-raw/federal_electoral_districts_boundaries_2015_shp_en.zip", quiet = TRUE)
unzip("data-raw/federal_electoral_districts_boundaries_2015_shp_en.zip", exdir="data-raw/federal_electoral_districts_boundaries_2015_shp_en")
federal_shapefile <- sf::read_sf("data-raw/federal_electoral_districts_boundaries_2015_shp_en",
layer = "FED_CA_2_2_ENG") %>%
sf::st_transform(crs = "+init=epsg:4326")
@mroutley
mroutley / CTs_by_ED.R
Created September 29, 2019 19:51
Allocate Census Tracts to Federal Electoral Districts
url <- "https://www.elections.ca/res/rep/off/ovr2015app/41/data_donnees/pollresults_resultatsbureauCanada.zip"
pins::pin(url, name = "2015_fed_results_zip")
unzip(pins::pin_get("2015_fed_results_zip"), exdir=paste("data-raw", "pollresults", sep = "/"))
on_files <- list.files(path = "data-raw/pollresults",
pattern = "pollresults_resultatsbureau35[[:digit:]]{3}.csv",
full.names = TRUE)
federal_results <- do.call("rbind", lapply(on_files, function(.file){readr::read_csv(.file)}))
@mroutley
mroutley / reconPlots.Rmd
Last active January 3, 2019 19:03
Testing reconPlots
---
title: "Supply-demand charts in R"
output: html_notebook
---
```{r setup}
library(tidyverse)
library(devtools)
install_github("andrewheiss/reconPlots")
library(reconPlots)
@mroutley
mroutley / pick_number.R
Created April 25, 2018 19:49
Plot a distribution of winning numbers from a three-person contest to guess the right number
pick_numbers <- function() {
trunc(runif(4, min = 1, max = 100))
}
winning_number <- function(numbers) {
numbers[2:4][which.min(abs(numbers[2:4] - numbers[1]))]
}
data <- replicate(1000000, winning_number(pick_numbers()))
hist(data,
@mroutley
mroutley / sample_delays.R
Last active April 19, 2018 14:25
Calculate reductions in the standard deviation in delays based on a random sampling
library(tidyverse)
data <- readxl::read_excel("Eco Ben Cleansed Passenger Counts v1.xlsx",
sheet = "Data") %>%
dplyr::transmute( # Shorten column names
passengers = `Station Passenger Count FINAL`,
delay = `System Minutes Delayed`
)
# Calculate current sd in delays