Skip to content

Instantly share code, notes, and snippets.

View eachanjohnson's full-sized avatar

Eachan Johnson eachanjohnson

View GitHub Profile
@eachanjohnson
eachanjohnson / slidR.R
Last active July 7, 2017 15:12
Quick and simple sliding window function, compatible with dplyr::mutate
sliding_window <- function(x, # numeric vector
width, # window size
callback=mean, # summary function to return a single value
fill=FALSE) { # make output as long as input? Set TRUE for adding data.frame columns
x_length <- length(x)
stopifnot(x_length > width) # check if this is appropriate
starts <- seq_len(x_length - width) # start points
ends <- starts + width # end points
@eachanjohnson
eachanjohnson / errR.R
Created October 28, 2016 15:01
Error handling binary operator in R
`%except%` <- function (try.expression, catch.expression) {
tryCatch(try.expression,
error=function(e) {
if ( inherits(catch.expression, 'function') ) {
catch.expression(e)
} else {
catch.expression
}
})