Skip to content

Instantly share code, notes, and snippets.

@expersso
expersso / python_v_r.R
Last active September 6, 2019 15:32
Wikipedia trend for Python and R
library(tidyverse)
library(scales)
library(stringr)
library(grid)
library(gridExtra)
library(wikipediatrend)
df <- wp_trend(c("R_(programming_language)", "Python_(programming_language)"),
from = "2007-12-01", to = "2017-01-12")
@expersso
expersso / matrix_inversion.R
Created January 5, 2017 14:46
Benchmarking matrix inversion in R
library(tidyverse)
inv <- function(m) {
n <- nrow(m)
stopifnot(n == ncol(m))
cofactor <- function(m, i, j) (-1)^(i+j) * det(m[-i, -j, drop = FALSE])
adjoint <- function(m) {
cofactor_matrix <- outer(seq_len(n), seq_len(n),
Vectorize(function(i, j) cofactor(m, i, j)))
@expersso
expersso / SpaceX.R
Created September 2, 2016 08:53
Attempting to detect air pollution following SpaceX explosion
library(dplyr)
library(ggplot2)
library(ggmap)
library(ropenaq)
library(gridExtra)
# Get pollution data
df <-
aq_measurements(
country = "US",
@expersso
expersso / trigonometry.R
Created July 28, 2016 08:44
Trigonometry with magick, gganimate, and purrr
## Purpose: illustrate use of magick, gganimate, and purrr
## Inspiration:
## https://rud.is/b/2016/07/27/u-s-drought-animations-with-the-witchs-brew-purrr-broom-magick/
## and subsequent discussion on magick vs gganimate:
## https://twitter.com/hrbrmstr/status/758304420224466944
library(purrr)
library(ggplot2)
library(gganimate)
library(animation)
library(magick)
@expersso
expersso / ft_age.R
Created June 20, 2016 12:53
Remake of FT chart on ageing
library(tidyr)
library(plyr)
library(dplyr)
library(stringr)
library(ggplot2)
library(scales)
library(ggthemes)
data <-
"
@expersso
expersso / scrape_nfl.R
Last active June 7, 2021 00:01
Scraping NFL data with purrr and tidyr goodness
# Replicating https://t.co/Jq1QfFGpjA
library(rvest)
library(stringr)
library(dplyr)
library(tidyr)
library(purrr)
library(lubridate)
get_and_clean_table <- function(url) {
@expersso
expersso / life_exp.R
Last active June 15, 2016 11:52
Remake of World Bank chart
# Remake of http://blogs.worldbank.org/opendata/measuring-surgical-systems-new-paradigm-health-systems-strengthening
library(dplyr)
library(purrr)
library(ggplot2)
library(scales)
library(ggthemes)
library(readxl)
library(tidyr)
library(wbstats)
@expersso
expersso / guns_homicide.R
Created June 14, 2016 08:28
Remake of reddit chart on gun ownership
# A remake of https://twitter.com/DataIsBeautiful/status/742593686509346816
# using log scales
library(dplyr)
library(purrr)
library(readr)
library(stringr)
library(ggplot2)
library(scales)
library(ggrepel)
@expersso
expersso / fed_locations.R
Last active June 10, 2016 15:11
Get locations of Federal Reserve Speeches
library(purrr)
library(dplyr)
library(rvest)
library(stringr)
library(geoparser)
w_msg <- function(f) {
force(f)
function(...) {
args <- list(...)
@expersso
expersso / pew.R
Created June 7, 2016 15:14
Income and demographics in U.S. metropolitan areas
read_sheet <- function(file, sheet) {
readxl::read_excel(file, sheet, skip = 6) %>% clean_data()
}
fix_names <- function(df) {
names(df)[1] <- "location"
names(df)[-1] <- paste(rep(c(2000, 2014), each = length(names(df)[-1]) / 2),
names(df)[-1], sep = "_")
df
}