Skip to content

Instantly share code, notes, and snippets.

View rBatt's full-sized avatar

Ryan Batt rBatt

  • TRM Labs
  • New York, New York
View GitHub Profile
@rBatt
rBatt / sub_clean.neus_jho.R
Created June 16, 2016 21:43
subset neus for jho
library("data.table")
load("clean.neus.RData")
s2c <- function (x, type = "list")
{
as.call(lapply(c(type, x), as.symbol))
}
colsuwant <- c("depth","btemp","stemp","bsalin","ssalin")
jho_cneus <- clean.neus[spp=="Paralichthys dentatus", structure(lapply(eval(s2c(colsuwant)),meanna, na.rm=TRUE),.Names=colsuwant),by=c("reg","datetime","year","stratum","lon","lat","haulid","season","station","spp","common")]
setkey(jho_cneus, datetime, year, stratum, station)
@rBatt
rBatt / urchin_meanShift.R
Created April 20, 2016 00:29
detect a breakpoint
# ============
# = Packages =
# ============
# to do everything in this script, make sure the following packages are installed
# install.packages(c("rgenoud", "strucchange"))
# =============
# = Functions =
@rBatt
rBatt / read_jho.R
Last active April 4, 2016 23:13
How to read a data file with 2 dimensional data interspersed with comments, in R
#' Read JHo
#'
#' Read in a file formatted like Jennifer's data sets
#'
#' @param file a character indicating the file name (and possibly path)
#' @param n_nrows integer indicating number of rows for each matrix
#' @param n_matrices integer indicating number of matrices in the file
#' @param n_comment integer indicating the number of comment lines preceding each matrix of data; should include blank lines
#' @param n_top_comment integer indicating the number of comment lines that are unique to the start of the file; default is 0 lines
#' @param ... arguments to be passed to \code{scan}, such as \code{what=numeric()} and \code{sep=","}
@rBatt
rBatt / read_jho.R
Created March 31, 2016 18:07
How to read a data file with 2 dimensional data interspersed with comments, in R
#' Read JHo
#'
#' Read in a file formatted like Jennifer's data sets
#'
#' @param file a character indicating the file name (and possibly path)
#' @param n_nrows integer indicating number of rows for each matrix
#' @param n_matrices integer indicating number of matrices in the file
#' @param n_comment integer indicating the number of comment lines preceding each matrix of data; should include blank lines
#' @param n_top_comment integer indicating the number of comment lines that are unique to the start of the file; default is 0 lines
#' @param ... arguments to be passed to \code{scan}, such as \code{what=numeric()} and \code{sep=","}
df <- data.frame(x = rnorm(20, mean=20, sd = 10))
threshold <- 25
replace_val <- 0
df[,"x2"] <- df[,"x"] # make them the same to start
df[df[,"x"] >= threshold, "x2"] <- replace_val # replace values at or above threshold with replacement value
# for Eva (foreva eva)
eg <- structure(c(608416320, 608416320, 608416320, 608416320, 608416320,
608416320, 608416320, 608416320, 608416320, 608416320, 608416320,
608416320, 608416320, 608416320, 608416320, 608416320, 608416320,
608416320, 608416320, 608416320, 608416320, 608416320, 608416320,
608416320, 608416320, 608416320, 608416320, 608416320, 608416320,
608416320, 608416320, 608416320, 608416320, 608416320, 608416320,
608416320, 608416320, 608416320, 608416320, 608416320, 608416320,
608416320, 608416320, 608416320, 608416320, 608416320, 608416320,
@rBatt
rBatt / trawlData.yml
Last active December 15, 2015 00:43
trawlData yml
## All R files that are used must be listed here:
packages:
- data.table
- LaF
- bit64
- stringr
- stringi
- PBSmapping
- devtools
export PATH=$HOME/bin:$PATH
source ~/.bashrc
export PATH=$PATH:/usr/include
source ~/.bashrc
echo $PATH
cd
mkdir src
plot(crop(subset(soda.annMax, 5:51), extent(c(-76, -65, 36, 46))), maxnl=200, bigplot=bgplt, smallplot=smplt, axis.args=axargs, col=tim.colors(), zlim=range(values(crop(subset(soda.annMax, 5:51), extent(c(-76, -65, 36, 46)))),na.rm=T))
@rBatt
rBatt / zCol
Created November 23, 2015 22:04
add heat map colors to plot
zCol <- function(nCols, Z){
cols <- colorRampPalette(c("#000099", "#00FEFF", "#45FE4F", "#FCFF00", "#FF9400", "#FF3100"))(nCols)
colVec_ind <- cut(Z, breaks=nCols)
colVec <- cols[colVec_ind]
}
# Example
X <- rnorm(1E3)
Y <- rnorm(1E3)
Z <- X*Y