Skip to content

Instantly share code, notes, and snippets.

@expersso
expersso / extracting_html_table_w_attr.R
Last active April 18, 2024 19:01
Extracting HTML tables and arbitrary HTML attributes
# SPECIFIC PROBLEM: Extract an html table and include all links
# (even when there are no links or multiple links per cell)
#
# GENERAL PROBLEM: Extract an html table and include all attributes
# for any given tag and attribute
#
# Example usage at bottom of the page
#
# Motivation: https://twitter.com/daattali/status/717582654476914688
@expersso
expersso / rentswatch.R
Created May 20, 2016 14:20
Explorations in functional programming
library(httr)
library(purrr)
library(dplyr)
library(ggplot2)
library(scales)
library(ggthemes)
with_msg <- function(msg, f) {
function(...) {
@expersso
expersso / geom_twoway_bar.R
Last active April 23, 2016 13:55
Creating stacked barchar in ggplot with positive and negative values
library(ggplot2)
df <- data.frame(
"sector" = c("a", "b", "c"),
"date" = rep(as.Date(c("1998-01-01", "1999-01-01", "2000-01-01")), each = 3),
"value" = c(-1, 1, 1, 1, -1, -3, 1.3, 1, 2)
)
p <- ggplot(df, aes(x = date, y = value, fill = sector))
@expersso
expersso / charsub.R
Created March 10, 2016 13:18
Character substitution by position
x2 <- c("apple", "banana", "orange", "pineapple", "kiwi")
# Original function
charsub <- function(x, i, value) {
out <- lapply(strsplit(x, ""), function(z) {
z <- `[<-`(z, i, value)
z[is.na(z)] <- " "
paste0(z, collapse = "")
})
unlist(out)
@expersso
expersso / slope.R
Created March 4, 2016 09:14
Slope graph for employment by industry
library(xml2)
library(dplyr)
library(tidyr)
library(stringr)
library(ggplot2)
url <- "http://www.bls.gov/opub/ted/2016/employment-by-industry-1910-and-2015.htm"
page <- read_html(url)
@expersso
expersso / anscombe.R
Created February 15, 2016 09:55
Minimal Anscombe
library(dplyr)
library(tidyr)
library(ggplot2)
df <- anscombe %>%
gather() %>%
mutate(axis = substr(key, 1, 1)) %>%
mutate(number = substr(key, 2, 2)) %>%
group_by(axis, number) %>%
mutate(key = 1:n()) %>%
@expersso
expersso / fertility.R
Created February 3, 2016 09:56
Create a plot of changes in African fertility rates (1950-2015)
library(tools)
library(dplyr)
library(readxl)
library(ggplot2)
library(countrycode)
library(ggthemes)
# Function to download and open Excel files
get_data <- function(url, ...) {
@expersso
expersso / movies.R
Created January 6, 2016 13:45
Movie budgets and box office success (1955-2015)
library(dplyr)
library(tidyr)
library(ggplot2)
library(lubridate)
library(stringr)
library(rvest)
library(xml2)
library(OECD)
# Get budget and box office data
@expersso
expersso / oecd_assault_deaths.R
Last active December 14, 2015 10:34
Plot of assault deaths by country using the OECD package
library(OECD)
# Retrieve data from OECD for all countries
df <- get_dataset("HEALTH_STAT", list("CICDHOCD.TXCMFETF+TXCMHOTH+TXCMILTX")) %>%
tbl_df() %>%
setNames(tolower(names(.)))
# Get list of dataframes with human-readable descriptions of variables and units
dims <- get_data_structure("HEALTH_STAT")
@expersso
expersso / binomial_distributions.R
Last active December 8, 2015 11:49
Binomial distributions
library(ggplot2)
library(dplyr)
probs <- seq(0.05, 0.95, each = 0.05)
df <- lapply(probs, rbinom, n = 100, size = 100) %>%
unlist() %>%
data.frame("successes" = .) %>%
mutate(probs = rep(probs, each = 100))