Skip to content

Instantly share code, notes, and snippets.

View Robinlovelace's full-sized avatar

Robin Lovelace Robinlovelace

View GitHub Profile
@Robinlovelace
Robinlovelace / shrink-data.R
Last active August 29, 2015 14:08
Shrinking the Tour de France data
# Take a random selection of the data
set.seed(2014)
tdft <- tdft[sample(nrow(tdft), size = 1000), ]
# Select only the variables of interest
install.packages("dplyr") # library for data manipulation
tdft <- select(tdft, lat, lon, created, text,
language, n_followers, n_tweets, user_location)
@Robinlovelace
Robinlovelace / remove-words.R
Created October 27, 2014 15:21
Removal of sensitive words
# Remove sensitive text
summary(factor(Encoding(tdft$text)))
Encoding(tdft$text) <- "UTF-8"
tdft$text <- iconv(tdft$text, "UTF-8", "UTF-8",sub='')
tdft$text <- gsub('@\\S+', '@', tdft$text) # remove all to '@' texts
tdft$text <- gsub('http\\S+', 'http', tdft$text) # remove all to hyperlinks
head(tdft$text)
@Robinlovelace
Robinlovelace / maxwords.R
Created October 27, 2014 15:26
Reduce max. number of words in a text string, keeping only first
# Function to reduce the max. number of words in a string
maxwords <- function(x, max = 10){
lwords <- length(x)
if(lwords > max) lwords <- max
paste0(x[1:lwords], collapse = " ")
}
# Apply maxwords to the data
tdft$text <- sapply(words, maxwords)
@Robinlovelace
Robinlovelace / ggplot-great-circles.R
Created November 18, 2014 08:49
Great circles in R (broken)
# Download data
library(ggmap) # load the ggmap package
library(geosphere)
download.file("https://dl.dropboxusercontent.com/u/15008199/tmp/origins.csv", "origins.csv", method = "wget")
origins <- read.csv("origins.csv")
os <- SpatialPoints(coords = origins,
proj4string = CRS("+init=epsg:4326"))
@Robinlovelace
Robinlovelace / ggplot-great-circles2.R
Created November 18, 2014 18:49
Great circles in R II
# Download data
x <- c("ggmap", "geosphere", "sp")
lapply(x, library, character.only = TRUE)
download.file("https://dl.dropboxusercontent.com/u/15008199/tmp/origins.csv", "origins.csv", method = "wget")
origins <- read.csv("origins.csv")
os <- SpatialPoints(coords = origins,
proj4string = CRS("+init=epsg:4326"))
@Robinlovelace
Robinlovelace / plot.bike.R
Created May 11, 2015 15:53
A plotting method for the example 'bike' class
library(plotrix)
plot.bike <- function(x, ...){
centre_back <- c(0, x$ws / 2)
centre_front <- c(x$ws + x$ttl + 100, x$ws / 2)
xlim <- c(-x$ws, centre_front[1] + x$ws / 2)
ylim <- c(0, x$ttl + x$ws/2 + 50)
plot.new()
plot.window(xlim, ylim)
draw.circle(x = centre_back[1], y = centre_back[2], radius = x$ws/2)
draw.circle(x = centre_front[1], y = centre_back[2], radius = x$ws/2)
@Robinlovelace
Robinlovelace / bike.R
Created May 11, 2015 15:55
Create an object to be assigned to the class 'bike' in R
# Create a class for bicycle data
wheelsize <- 559 # 26" wheel size, definited by 'bead seat diameter' (BSD)
size <- 21 * 25.4 # top tube length, inches converted to mm
top_tube_length <- 530 # top tobe length
x <- list(ws = wheelsize, s = size, ttl = top_tube_length)
class(x) <- "bike"
@Robinlovelace
Robinlovelace / install-ncl.R
Created May 11, 2015 16:00
Install packages from NCL course
install.packages("drat")
drat::addRepo("rcourses")
install.packages("nclRadvanced", type="source")
@Robinlovelace
Robinlovelace / R-demo.R
Last active August 29, 2015 14:21
Demo of R tutorial
x <- 1:99
y = x^2 / exp(x)
plot(x, y)
pkgs <- c("downloader", "readxl")
install.packages(pkgs)
lapply(pkgs, library, character.only = T)
dir.create("big-data")
download("http://tinyurl.com/r-for-bd-8",
@Robinlovelace
Robinlovelace / tresis-households.R
Created September 4, 2015 20:12
Code chunk that is part of the TRESIS approach to modelling transport behaviour for the book 'Spatial microsimulation with R'
# Households with a car and no professionals
synhhlds <- c(synhhlds,
sample(
filter(hhlds,
numprof == 0,
numcars > 0
)$hhld,
popdist['Car','Other'])
)