This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(httr) | |
library(purrr) | |
library(dplyr) | |
library(ggplot2) | |
library(scales) | |
library(ggthemes) | |
with_msg <- function(msg, f) { | |
function(...) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) %>% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tools) | |
library(dplyr) | |
library(readxl) | |
library(ggplot2) | |
library(countrycode) | |
library(ggthemes) | |
# Function to download and open Excel files | |
get_data <- function(url, ...) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(tidyr) | |
library(ggplot2) | |
library(lubridate) | |
library(stringr) | |
library(rvest) | |
library(xml2) | |
library(OECD) | |
# Get budget and box office data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |