Skip to content

Instantly share code, notes, and snippets.

View ateucher's full-sized avatar

Andy Teucher ateucher

View GitHub Profile
## convert back and forth between -180/180 and 0/360 systems
to_lon_360 <- function(lon_180) {
(lon_180 + 360) %% 360
}
to_lon_180 <- function(lon_360) {
((lon_360 + 180) %% 360) - 180
}
srctxt <- "http://data.giss.nasa.gov/gistemp/tabledata_v3/GLB.Ts+dSST.txt"
# Import as a character vector first so can get info about length etc.
aslines <- readLines(srctxt)
# Store the first occurrence of the header row as a character vector
header <- strsplit(aslines[8], "\\s+")[[1]]
# Count the lines of data (any lines starting with 4 digits)
nlines <- length(grep("^\\d{4}", aslines))
@ateucher
ateucher / col2hex.R
Created January 6, 2015 21:37
Convert R colours and rgb values to hex
col2hex <- function(colour) {
col_rgb <- t(col2rgb(colour))
rgb2hex(red = col_rgb[,1], green = col_rgb[,2], blue = col_rgb[,3])
}
rgb2hex <- function(red, green, blue, rgbvec = NULL) {
if ((missing(red) || missing(green) || missing(blue)) && is.null(rgbvec)) {
stop("You must supply red, green, and blue OR rgbvec")
}
@ateucher
ateucher / fill_dates.R
Last active August 29, 2015 14:13
Fill dates
#'Fill gaps in a date sequence
#'
#'Given a dataframe with one column as a date sequence, fill gaps in the dat sequence.
#' @param df Dataframe
#' @param date_col the column containing dates
#' @param interval The interval in the date sequence. If \code{NULL}, calculated automatically.
#' @param fill_cols Columns to fill with the value in the column (should be columns where value is same in every row, such as an ID.)
#' @export
#' @return dataframe with filled in dates
#' @examples \dontrun{
@ateucher
ateucher / roxygen_template.R
Last active August 29, 2015 14:13
roxygen_template
#' Populate the boilerplate roxygen template at the top of the function.
#'
#' Inspired by Karthik Ram's RTools Sublime Text 2 plugin:
#' https://github.com/karthik/Rtools
#' @param funfile path to the .R file containing the function
#' @param func the name of the function you want to document
#' @param export Is the function exported (default \code{TRUE})? If \code{FALSE}
#' keyword 'internal' is added
#' @export
#' @return nothing, but adds the roxygen template to the top of the file
@ateucher
ateucher / gensub.sh
Last active August 29, 2015 14:15
gawk - gensub
gawk -F, '
BEGIN { OFS = ",";
print "id,name,date_time,val,val2,val3"}
{
$3=gensub(/(^[0-9]{2})([A-Z]{3})([0-9]{4}):([0-9]{2}:[0-9]{2}:[0-9]{2})$/,
"\\3-\\2-\\1 \\4 PST","g",$3)
print $0
}' testfile.csv
@ateucher
ateucher / gist:6bae26e608b996b065d1
Created April 28, 2015 20:14
pmax_with_factors.md
```r
l <- c("a", "b", "c", "d")
a <- factor(c("a", "c", "d"), levels = l, ordered = TRUE)
b <- factor(c("c", "b", "a"), levels = l, ordered = TRUE)
pmax(a, b)
```
```
l <- c("a", "b", "c", "d")

a <- factor(c("a", "c", "d"), levels = l, ordered = TRUE)
b <- factor(c("c", "b", "a"), levels = l, ordered = TRUE)

pmax(a, b)
@ateucher
ateucher / cowsay_errors.R
Last active August 29, 2015 14:20
cowsay errors
## Makes a random animal deliver the error message
## Fun to put in a .Rprofile :)
# devtools::install_github("sckott/cowsay")
library("cowsay", quietly = TRUE)
options(error = function() {
animal <- say("", by = "random", type = "string")
## remove the top "-------" message delimiter from the cowsay character
message(strsplit(animal, "\n\\s*-{5,20}\\s*\n\\s*\n")[[1]][2])
})
@ateucher
ateucher / exif-focal-length.R
Created May 1, 2015 18:27
exif-focal-length.R
flengths <- system('exiftool -T -r -FocalLength -ext .NEF "."', intern=TRUE)
flengths.num <- as.numeric(strsplit(flengths, split=" mm"))
hist(flengths.num,breaks=20, main="Frequency of focal lengths used by Andy\nfor 1631 photos in the Galapagos Islands", xlab="Focal Length")