Skip to content

Instantly share code, notes, and snippets.

View emhart's full-sized avatar

Edmund Hart emhart

View GitHub Profile
@emhart
emhart / arb_rescale.R
Last active August 29, 2015 14:12
Code to answer @LeahAWasser's question as follows: "Let's say I have an array of numbers. I can adjust the entire array of numbers to fall within a range of 0-1 with the same spread using the equation: (ValueInArray-minvalueArray) / (MaxValueArray - MinVallueArray) Where: ValueInArray = Nth value in the array (calculation performed for each valu…
#' @description Rescale a vector of numbers to an arbitrary range.
#' @param minVal The minimum value in the range to rescale your numbers to
#' @param maxVal The maximum value on the range to rescale your numbers to
#' @param vec The vector you want to rescale
#' @example /dontrun{
#' orig <- runif(10,1,30)
#' rescaled <- arb_rescale(2,5,orig)
#' cbind(orig,rescaled)
#' }
arb_rescale <- function(minVal,maxVal, vec){
@emhart
emhart / iucnThreats.R
Created December 1, 2014 18:27
Quick snippet for @TimDoherty_ on how to use reol to access IUCN data
## Installation
install.packages("devtools")
library(devtools)
devtools::install_github("ropensci/reol")
library("Reol")
res <- DownloadSearchedTaxa(c("Diceros bicornis","Anolis carolinensis", "Anolis garmani"), to.file=FALSE, exact=TRUE)
## Get IUCN status
GetIUCNStat(res)
### Extract the full page information.
@emhart
emhart / extract_coverage.R
Created September 24, 2014 23:15
Alpha testing coverage extraction
library(devtools)
install_github("ropensci/mdextract",dependencies = TRUE)
library(spocc)
saltest <- occ("Ambystoma maculatum",from="inat")
saltest <- occ2df(saltest)
## See a bounding box
scov <- coverage_extract(saltest,spatial=list(type="box",lat = "latitude", lon = "longitude",elevation = FALSE))
plot(scov)
### Now let's try poly
@emhart
emhart / LarsCode.R
Created August 29, 2014 21:16
for Iain
library(MASS)
library(lars)
library(lavaan)
####Code to wrap lasso and extra CV...
### Taken from: http://www4.stat.ncsu.edu/~boos/var.select/lasso.dr1.txt
lasso.dr1<-function(x,y,kfold=5,rep=10,plot=T,grid=seq(from=0,to=1,length=100)){
# wrapper for lasso in lars, with modifications to cv.lars
require(lars)
@emhart
emhart / ugly.R
Created August 26, 2014 21:14
sapply gone awry
# sapply gone terribly terribly wrong, or used perfectly, I can't tell.
# Use:
# input: a vector of words, and I want to know are any of the words in input are in the vector out
# out: a vector of words that I want to check against, a master list of words
# I then want to return an index of TRUE/FALSE to determine if a word is in in the master list
input <- c("Lorem","ipsum","testing")
out <- c("Loremipsum","qwerty","keyboard","dvorak")
dindex <- sapply(input, function(x,y){ind <- grep(x,y);ifelse(length(ind) > 0, for(i in ind){ifelse(x == y[i],return(TRUE),return(FALSE))},return(FALSE))},y=out)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@emhart
emhart / Bennet_over.R
Created July 3, 2014 22:36
For Bennet, overlaying species
library("sp")
library("maptools")
options(stringsAsFactors=F)
coords <- read.csv("~/Downloads/latlongcoord.csv")
biome<-readShapeSpatial("~/Downloads/official/wwf_terr_ecos.shp")
### Load subset of data just as a quick test
spobj <- SpatialPoints(as.matrix(coords[1:100,c('decimalLongitude','decimalLatitude')]))
@emhart
emhart / knnCont.R
Created June 5, 2014 21:12
A K-Nearest neighbor curve fitting function.
knnCont <- function(X, y, k = 4){
y <- y[order(X)]
X <- sort(X)
x.out <- vector()
y.out <- vector()
for(x in 1:(length(X)-(k-1))){
x.out <- c(x.out,mean(X[x:(x+(k-1))]))
y.out <- c(y.out,mean(y[x:(x+(k-1))]))
library(sp)
library(raster)
### Data source: http://nadp.sws.uiuc.edu/ntn/annualmapsByYear.aspx#2012
### Sites provided by sarah
# Load up our data
depF<-'NO3_dep_2012.tif'
concF<-'NO3_conc_2012.tif'
sites <- read.csv("neonsites.csv")
unprojDep <- raster(depF)