Skip to content

Instantly share code, notes, and snippets.

View kjhealy's full-sized avatar

Kieran Healy kjhealy

View GitHub Profile
library(tidyverse)
library(babynames)
babynames %>%
filter(sex == "M") %>%
mutate(endletter = str_sub(name, -1)) %>%
group_by(year, endletter) %>%
summarize(letter_count = n()) %>%
mutate(letter_prop = letter_count / sum(letter_count),
letter_rc = case_when(
@kjhealy
kjhealy / collapse_mask.R
Created February 15, 2022 03:13 — forked from grantmcdermott/collapse_mask.R
Benchmarking collapse_mask
## Context: https://twitter.com/grant_mcdermott/status/1493400952878952448
options(collapse_mask = "all") # NB: see `help('collapse-options')`
library(dplyr)
library(data.table)
library(collapse) # Needs to come after library(dplyr) for collapse_mask to work
flights = fread('https://raw.githubusercontent.com/Rdatatable/data.table/master/vignettes/flights14.csv')
@kjhealy
kjhealy / pnc.r
Last active September 25, 2021 03:29
library(tidyverse)
library(cowplot)
df <- read_csv("https://osf.io/qmhgw/download")
out <- df %>%
ggplot(mapping = aes(x = year,
y = oneper,
group = country,
color = Region)) +
# UK map v. quick example
## Libraries
library(tidyverse)
library(sf)
#> Linking to GEOS 3.8.1, GDAL 3.1.4, PROJ 6.3.1
## Get the map data
@kjhealy
kjhealy / ffmpeg.md
Created March 2, 2021 03:16 — forked from dvlden/ffmpeg.md
Convert video files to MP4 through FFMPEG

This is my personal list of functions that I wrote for converting mov files to mp4!

Command Flags

Flag Options Description
-codec:a libfaac, libfdk_aac, libvorbis Audio Codec
-quality best, good, realtime Video Quality
-b:a 128k, 192k, 256k, 320k Audio Bitrate
-codec:v mpeg4, libx264, libvpx-vp9 Video Codec
library(tidyverse)
library(gapminder)
library(ggalt) ## install with install.packages("ggalt", repos = "http://cran.rstudio.com")
## Make some sample data with three countries that each has nine measures
example_wide <- gapminder %>%
select(country, year, lifeExp, pop, gdpPercap) %>%
filter(country %in% c("United States", "Canada", "Mexico"),
year %in% c(1957, 1977, 2007)) %>%
pivot_wider(names_from = year, values_from = lifeExp:gdpPercap)
@kjhealy
kjhealy / apple-covid.R
Last active May 27, 2020 15:54
Get Apple's COVID data in R
## Get current Apple COVID data
## Kieran Healy / @kjhealy
## E.g. apple_covid <- get_apple_data()
## 1. Find today's URL
## Thanks to David Cabo (@dcabo) for alerting me to the existence of the json file.
get_apple_target <- function(cdn_url = "https://covid19-static.cdn-apple.com",
json_file = "covid19-mobility-data/current/v3/index.json") {
tf <- tempfile(fileext = ".json")
library(dplyr, warn.conflicts = FALSE)
library(gapminder)
probs <- c(0.1, 0.5, 0.9)
gapminder %>%
group_by(continent) %>%
summarise(
probs = probs,
across(is.numeric & !year, ~ quantile(.x, probs))
)
@kjhealy
kjhealy / epl_goal_contribution_matrix_18-19.r
Created May 19, 2019 19:43 — forked from Ryo-N7/epl_goal_contribution_matrix_18-19.r
Goal contribution matrix for Premier League 2018-2019
# pkgs
pacman::p_load(tidyverse, polite, scales, ggimage, ggforce,
rvest, glue, extrafont, ggrepel, magick)
loadfonts()
## add_logo function from Thomas Mock
add_logo <- function(plot_path, logo_path, logo_position, logo_scale = 10){
# Requires magick R Package https://github.com/ropensci/magick
## Start with the code in https://github.com/kjhealy/lexis_surface/blob/master/lexis.r
## At the bottom, the `fra_surface` object is done in 3D with the `plotly` library. We can
## also do it with `rayshade`:
library(rayshader)
## rayshade wants the axes a little differently from plotly
## but like plotly it wants a matrix.
fra_surf <- fra_surf[c(1:201),c(100:1)]