Skip to content

Instantly share code, notes, and snippets.

View ateucher's full-sized avatar

Andy Teucher ateucher

View GitHub Profile
@ateucher
ateucher / lsa_fix.R
Created January 31, 2014 21:49
Fix to lsa function
x <- 1:26
y <- LETTERS
z <- data.frame(x,y)
unrowname <- function(x) {
rownames(x) <- NULL
return(x)
}
lsa <- function ()
@ateucher
ateucher / foldText.R
Created May 8, 2014 22:43
Insert linebreaks in a string (for wrapping labels)
foldText <- function(x, n) {
x <- gsub(paste0('([^\n]{1,',n,'})(\\s|$)'), '\\1\n', x)
## Remove line-breaks (one or more) at first/last positions
x <- gsub('^(\n)+|(\n)+$', '', x)
x
}
## Examples
txt <- c("Hello my name is Andy",
"Oh when the Saints go marching in",
@ateucher
ateucher / aic_table.R
Last active August 29, 2015 14:04
Generating AIC table with model and variable names
require(survival)
require(kimisc) # has the nlist function to create a named list
require(AICcmodavg) # has the aictab function
require(dplyr)
require(ggplot2)
require(reshape2)
dat <- read.csv("data/obs_matched_pr.csv")
dat$strat_var <- factor(dat$strat_var)
@ateucher
ateucher / cars.R
Last active August 29, 2015 14:05
Compare fuel economy of cars
library(fueleconomy)
library(dplyr)
library(tidyr)
library(ggplot2)
comp_vehicles <- vehicles %>%
filter(cyl == 4,
grepl("Utility", class),
make %in% c("Honda", "Toyota","Subaru",
"Hyundai", "Kia", "Mazda",
@ateucher
ateucher / geo_json.R
Last active August 29, 2015 14:08
Testing to_geo_json and to_geojson
devtools::install_github("ropensci/togeojson@reworkapi")
library('togeojson')
library('maps')
data(us.cities)
to_geo_json(us.cities[1:2,], lat='lat', lon='long')
to_geojson(us.cities[1:2,], lat='lat', lon='long')
@ateucher
ateucher / foo.geojson
Last active August 29, 2015 14:08
Mixing geometry types in geojson
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
## 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 / 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