Skip to content

Instantly share code, notes, and snippets.

View dholstius's full-sized avatar

David Holstius dholstius

  • Bay Area Air Quality Management District
  • San Francisco Bay Area
  • X @dholstius
View GitHub Profile
@dholstius
dholstius / titlecase.R
Created November 18, 2014 23:54
titlecase.R
titlecase <- function (x) {
# TODO: use Rex package
gsub("\\b([a-z])([a-z]+)", "\\U\\1\\L\\2", tolower(x), perl = TRUE)
}
@dholstius
dholstius / .gitignore
Last active August 29, 2015 14:09 — forked from mbostock/.block
California census tracts
.DS_Store
build
node_modules
@dholstius
dholstius / read_tbl.R
Last active August 29, 2015 14:08
read_tbl
read_tbl <- function (..., stringsAsFactors = FALSE, check.names = FALSE) {
require(dplyr)
as.tbl(read.csv(..., stringsAsFactors = stringsAsFactors, check.names = check.names))
}
write_tbl <- function (x, ..., row.names = FALSE) {
write.csv(as.tbl(x), ..., row.names = row.names)
}
@dholstius
dholstius / merge.SpatialPointsDataFrame.R
Created October 2, 2014 18:58
Merge a SpatialPolygonsDataFrame with a data.frame
merge.SpatialPolygonsDataFrame <- function (x, y, by, ...) {
require(sp)
y <- as.data.frame(y)
if (missing(by)) {
if (is.null(row.names(x@data)) || is.null(row.names(y))) {
warning("[merge.SpatialPolygonsDataFrame] merging by position")
i <- 1:nrow(x@data)
} else {
warning("[merge.SpatialPolygonsDataFrame] merging by row names")
i <- row.names(x@data)
@dholstius
dholstius / CES_data-examples.R
Last active August 29, 2015 14:07
Export tract-level CalEnviroScreen 2.0 estimates for PM25 and DieselPM to shapefile
# Installation
library(devtools)
install_github("BAAQMD/CalEnviroScreen")
# Helper function: merge.SpatialPolygonsDataFrame()
source_gist("8d1aa85a1623243f8f1f")
# Data from CalEnviroScreen 2 (CES2) package
data(California, package = "CalEnviroScreen")
data(CES2, package = "CalEnviroScreen")
@dholstius
dholstius / global.R
Last active August 29, 2015 14:07
Fetch, display, and download data from ARB
suppressPackageStartupMessages({
library(dplyr) # install.packages("dplyr")
library(tidyr) # install.packages("tidyr")
library(httr) # install.packages("httr")
library(lubridate) # install.packages("lubridate")
library(stringr) # install.packages("stringr")
library(ggvis) # install.packages("ggvis")
library(digest) # install.packages("digest")
})
@dholstius
dholstius / layer_abline.R
Created September 24, 2014 21:42
layer_abline() for ggvis
abline_data <- function (domain, intercept, slope) {
data.frame(x = domain, y = domain * slope + intercept)
}
untick <- function (x) {
stopifnot(all(sapply(x, is.name)))
str_replace_all(as.character(x), "`", "")
}
layer_abline <- function (.vis, domain, intercept = 0, slope = 1, dash = 6, ...) {
@dholstius
dholstius / optim_lag.R
Last active August 29, 2015 14:06
Optimize alignment of "zoo" objects
optim_lag <- function (z1, z2, merit = cor, max_lag = 120, warn = FALSE) {
require(zoo)
ow <- as.integer(options("warn"))
options(warn = -1 * as.integer(!warn))
on.exit(options(warn = ow))
f <- approxfun(x = index(z2), y = as.numeric(z2))
t_start <- max(start(z1), start(z2))
t_end <- min(end(z1), end(z2))
z1 <- window(z1, start = t_start, end = t_end)
z2 <- window(z2, start = t_start, end = t_end)
@dholstius
dholstius / tsreg.R
Created September 23, 2014 23:15
Theil-Sen regression
# Ported by David Holstius <dholstius@baaqmd.gov>
# from http://skip.ucsc.edu/leslie_MOUSE/programs/plotting/tsreg.r
# Module tsreg
# Author: E. A. Houseman
# Last update July 2004
# AR(q) time series regression assuming regular intervals
# Support for cholesky residuals [Houseman, Ryan, Coull (2004, JASA)]
@dholstius
dholstius / rbindapply.R
Created September 22, 2014 22:14
lapply() mixed with rbind()
rbindapply <- function (
X,
FUN,
...
) {
results <- lapply(X, FUN, ...)
if (!is.null(names(X))) {
names(results) <- names(X)
} else {
if (is.character(X))