Skip to content

Instantly share code, notes, and snippets.

View ateucher's full-sized avatar

Andy Teucher ateucher

View GitHub Profile
@ateucher
ateucher / fill_missing_dates.R
Last active August 19, 2019 07:37
Filling in missing dates in a time series data frame
## First make up some mock data
my_data <- data.frame(date = seq(as.Date("2010-01-01"), as.Date("2015-12-31"),
by = "1 month"),
value = rnorm(72))
## Remove some observations so we have an incomplete data set
my_incomplete_data <- my_data[sort(sample(nrow(my_data), 60)), ]
## Make a data frame with a full series of dates from the min date to the max date
## in the incomplete data frame
@ateucher
ateucher / summarise_daily.R
Created August 18, 2015 20:58
Summarise hourly data by date
library(dplyr)
## Make some mock data for illustration:
date_time <- seq(as.POSIXct("2014-01-01"), as.POSIXct("2015-08-18"), by = "1 hour")
value <- rnorm(length(date_time))
my_data <- data.frame(date_time, value)
# Create a date column (remove time)
my_data$date <- as.Date(my_data$date_time, tz = "Canada/Pacific")
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ateucher
ateucher / find_parens.R
Created October 16, 2015 21:50
Find matching parens in a string
find_matching_paren <- function(str, open_paren = "(") {
close_paren <- switch(open_paren, "(" = ")", "{" = "}", "[" = "]")
# Split string into single characters
str <- strsplit(str, "")[[1]]
## Find the first openening parenthesis in the string
start_pos <- grep(paste0("\\", open_paren), str)[1] + 1
counter <- 1
library(rgdal)
poly <- '{ "type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
]
}'
poly2 <- '{ "type": "FeatureCollection",
"features": [
@ateucher
ateucher / sort_birds.R
Created December 14, 2015 21:05
Sort a list of bird species taxonomically using the rebird packaage
## Make some mock data for demonstration purposes:
Species <- c("Green-winged Teal", "Bald Eagle", "Swainson's Hawk", "Red-tailed Hawk",
"American Coot", "Rock Pigeon", "Red-naped Sapsucker", "Northern Flicker",
"American Kestrel", "Western Wood-Pewee", "Willow Flycatcher",
"Dusky Flycatcher", "Say's Phoebe", "Eastern Kingbird", "Black-billed Magpie",
"Common Raven", "Tree Swallow", "Cliff Swallow", "House Wren",
"Marsh Wren", "Mountain Bluebird", "American Robin", "European Starling",
"Cedar Waxwing", "Yellow-rumped Warbler", "Clay-colored Sparrow",
"White-crowned Sparrow", "Vesper Sparrow", "Western Meadowlark",
"Yellow-headed Blackbird", "Brewer's Blackbird", "Mallard", "Blue-winged Teal",

"Why you should never use attach()"

We will use the internal mtcars dataset to illustrate.

dat <- mtcars
head(dat)

Rolling back a bad commit

So, you've committed some changes to your repository that you want to roll back. This will tell you how to roll that back. It assumes that you want to pretend that the offending commit(s) never happened. Note that this workflow is the simplest solution, and works well for us because we are a small team that communicates well, but it is generally considered bad practice because it rewrites history and can mess up collaborators.

  1. Find the commit that you want to roll back to, and copy the SHA (the unique alphanumeric string that identifies the commit).

  2. In git bash, type:

    git reset --hard theSHA
    
@ateucher
ateucher / self_union.R
Last active May 9, 2016 16:36
Faster version of raster::union for a single SpatialPolygonsDataFrame
self_union <- function(x) {
stopifnot(requireNamespace("rgeos"))
n <- length(x)
if (n < 2) {
return(x)
}
if (.hasSlot(x, 'data')) {
x <- as(x, 'SpatialPolygons')