Skip to content

Instantly share code, notes, and snippets.

View Robinlovelace's full-sized avatar
💭
I'm on paternity leave 👶 and may be slow to respond

Robin Lovelace Robinlovelace

💭
I'm on paternity leave 👶 and may be slow to respond
View GitHub Profile
for each item in z List
do
end for
@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 / load-mapinfo-mid.R
Last active November 15, 2017 03:07
Loading MapInfo .mid files with R
# Loading MapInfo .mid files
library(rgdal) # rgdal library
drvs <- ogrDrivers() # ogr capabilities
drvs[grep("Inf", drvs$name),] # test MapInfo reading capability of gdal
f <- "folder/filename.mid" # filename
l <- ogrListLayers(f) # layer name
sp_object <- readOGR(f, layer = l[[1]]) # load!
@Robinlovelace
Robinlovelace / if-devtools.R
Last active October 29, 2020 06:34
Installs devtools if needed (put at top of R scripts)
# Install devtools if needed
if(!require(devtools)) install.packages("devtools")
@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"