Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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')
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"))