Skip to content

Instantly share code, notes, and snippets.

View Robinlovelace's full-sized avatar
💭
It's time to understand the world

Robin Lovelace Robinlovelace

💭
It's time to understand the world
View GitHub Profile
@Robinlovelace
Robinlovelace / mapshaper.R
Created January 7, 2016 16:58
Demonstrates the use of mapshaper called from R to simplify spatial objects: https://github.com/ropensci/stplanr
# # # # # # # # # # # # # # # # # # # #
# Simplify sp objects w. mapshaper #
# Needs https://nodejs.org installed #
# Tested on Windows and Linux #
# # # # # # # # # # # # # # # # # # # #
# # # # # #
# R setup #
# # # # # #
@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'])
)
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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"))