Skip to content

Instantly share code, notes, and snippets.

library(dplyr); library(httr); library(rvest)
addr <- "Big Ben"
GET("http://maps.google.com/maps/api/geocode/xml",
query = list(address = addr)) %>%
content() %>%
html_nodes(xpath = "//location//lat | //location//lng") %>%
html_text() %>%
setNames(c("lng", "lat"))
@expersso
expersso / ggplot2_DRY
Last active May 27, 2019 22:42
Example of using ggplot2's %+% to follow DRY principle
library(eurostat)
library(dplyr)
library(ggplot2)
library(scales)
#### Original version (not using %+%) ####
# Originally posted at http://www.r-bloggers.com/european-debt-and-interest/ on June 7, 2015
r1 <- get_eurostat('gov_10dd_edpt1')
@expersso
expersso / ldply_example
Last active August 29, 2015 14:22
Example use of ldply for web scraping
# Using ldply returns a clean data frame, so avoids
# the lapply + do.call(rbind, ...) idiom
# Also, the .progress argument is very useful
df <- ldply(c(2002:2014, 52, 26, 13, 4, 1), function(x) {
sprintf("http://distrowatch.com/index.php?dataspan=%d", x) %>%
html() %>%
html_nodes(xpath =
"//table[@class = 'News' and @style = 'direction: ltr'][2]") %>%
.[[1]] %>%
@expersso
expersso / fatal_dog_attacks.R
Last active November 30, 2015 15:20
Fatal dog attacks by category of dog
lapply(c("dplyr", "xml2", "rvest", "stringr", "ggplot2"), library,
character.only = TRUE)
url <- "https://en.wikipedia.org/wiki/Fatal_dog_attacks_in_the_United_States"
page <- read_html(url)
# Each year is represented by its own table
tbls <- page %>%
xml_find_all("//table") %>%
@expersso
expersso / wvs_wealth_tolerance.R
Last active November 30, 2015 17:13
Relationship between wealth and tolerance
lapply(c("WDI", "dplyr", "stringr", "ggplot2", "tidyr", "ggthemes"), library,
character.only = TRUE)
library(wvs) # local package
# Get shares who mentioned specific groups when asked who they would not want
# as a neighbour
values <- wvs %>%
filter(wave %in% c("2005-2009", "2010-2014")) %>%
group_by(country.abbreviation) %>%
@expersso
expersso / gist:e69ee0ebb4f214dbe544
Last active December 6, 2015 17:15
Length of names of months and weekdays
library(lubridate)
library(ggplot2)
period_name <- c(month.name, as.character(wday(1:7, label = TRUE, abbr = FALSE)))
name_length <- sapply(strsplit(period_name, ""), length)
df <- data.frame(period_name, name_length, period_order = c(1:12, 1:7),
type = rep(c("Months", "Weekdays"), c(12, 7)))
ggplot(df, aes(x = period_order, y = name_length, label = period_name)) +
@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))
@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 / 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 / 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, ...) {