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 / migrate-pkgs.R
Last active March 4, 2024 18:53
Helping Yuanyuan migrate from R 4.1 to 4.3
old_pkgs <- dir("C:/Users/yfang/Documents/R/win-library/4.1")
for (pkg_name in old_pkgs) {
# If we already installed it, don't do anything
installed_pkgs <- row.names(installed.packages())
if (pkg_name %in% installed_pkgs) {
# pass
} else {
@dholstius
dholstius / RasterLayer-list.R
Last active September 29, 2020 13:17
lists of RasterLayers behaving weirdly
#
# If I wrap a single RasterLayer in a list, I can extract it
# with .[[1]] but not with unlist().
#
# This example just uses a single `RasterLayer` to make the point.
# The broader problem (for me) is that it seems to be precluding the
# possibility of using `purrr::map2()` to combine lists of RasterLayers
# in various ways.
#
@dholstius
dholstius / example-calculations.R
Last active May 19, 2020 20:29
PM25-HIA-methodology
library(units)
install_symbolic_unit("person")
install_conversion_constant("person", "death", const = -1)
options(digits = 8)
# For convenience, let's assume a population of 1 million people.
pop <- as_units(1e6, "person")
# Let's assume that the baseline annual all-cause mortality rate is
# about 1%, i.e., about 10,000 per million (per year).
@dholstius
dholstius / BY2015-animal_waste-emission_factors-vs_BY2011.R
Last active November 21, 2019 23:54
BY2015: Animal Waste Emission Factors
#
# BY2011 -> BY2015: emission factors for "animal waste" categories.
#
# Created by dholstius on 2019-11-21 for aguha.
#
# For the link between the two sets of categories, see (in Dropbox):
#
# - BY2015/Work/Crosswalks/Categories/BY2015_categories_geneaology.xlsx
#
# For more about the operations taking place in the code below, see (in R):
@dholstius
dholstius / QA-BY2015-area_source-forecast.R
Created November 18, 2019 18:56
QA of BY2015 area source forecast
#
# BY2015-area_source-forecast-chart_annual_growth.R
#
# Created 2019-11-18 by dholstius
#
library(inventory)
library(ggtools)
#
@dholstius
dholstius / BY2011-summarytree.R
Last active October 24, 2019 00:06
Summary Tree Tool, demonstrated using the default BY2011 category hierarchy (and BY2011 emissions).
#'
#' Summary Tree Tool
#'
#' Discover which categories --- or groups of categories --- emit the largest
#' share(s) of a particular pollutant.
#'
#' Version history:
#'
#' - Created 2015-12-01 by dholstius
#' - Last updated 2019-10-23 by dholstius
@dholstius
dholstius / show138.R
Last active October 22, 2019 21:43
show138
library(inventory)
show138 <- function (
DB_year,
category_id,
verbose = TRUE
) {
#' Define the preferred sorting order.
PONSCO_vars <-
@dholstius
dholstius / example-DB_find_pollutants.R
Last active October 19, 2019 03:26
DataBank: Find Pollutant Metadata for "Diesel PM"
#
# Turns out that #1350 is the DataBank ID for "Diesel PM".
#
# Here's an "old-school" way to figure that out.
# t0064 contains pollutant metadata.
#
library(Ingres)
help(t0064) # here's some Orange Binder material
view(t0064) # type "Diesel" in the search box (upper right)
@dholstius
dholstius / patch.R
Last active May 10, 2019 21:52
Patch data on-the-fly (DRAFT)
#' Patch data on the fly.
#'
#' @param object to be patched
#' @param cond logical condition(s) to be evaluated within scope of object
#' @param \dots name-value pairs
#' @param quiet suppress messages
#'
#' @examples
#' patch(mtcars, where(vs == 0, am == 1), gear = Inf, carb = carb + 10)
#'
@dholstius
dholstius / kml.R
Last active March 11, 2019 18:01
Reading and writing KML in R
read.kml <- function(file, layers) {
require(sp)
require(rgdal)
read.layer <- function (layer_name) {
spobj <- rgdal::readOGR(dsn=file, layer=layer_name)
coords <- coordinates(spobj)
colnames(coords) <- c('x', 'y', 'z')[1:ncol(coords)]
df <- data.frame(coords, spobj@data)
transform(df, layer=layer_name)
}