Skip to content

Instantly share code, notes, and snippets.

View PhillRob's full-sized avatar
🌐
At the office (for most of the time)

Philipp R PhillRob

🌐
At the office (for most of the time)
View GitHub Profile
@PhillRob
PhillRob / rbind.keep.columns
Last active August 29, 2015 14:24
rbind data frames with miss matching columns
rbind.keep.columns <- function(x, y) {
x.dif <- setdiff(colnames(x), colnames(y))
y.dif <- setdiff(colnames(y), colnames(x))
x[, c(as.character(y.dif))] <- NA
y[, c(as.character(x.dif))] <- NA
return(rbind(x, y))
}
@PhillRob
PhillRob / reverseGeo
Last active August 29, 2015 14:24
use google API to get match lon/lat data to country and state
reverseGeo <- function(latlng) {
require("XML")
require("httr")
latlng <- as.numeric(latlng)
latlngStr <- gsub(" ", "%20", paste(round(latlng, 2), collapse = ","))
url <- "http://maps.google.com"
path <- "/maps/api/geocode/xml"
query <- list(sensor = "false", latlng = latlngStr)
response <- GET(url, path = path, query = query)
if (response$status != 200) {
@PhillRob
PhillRob / point.2.poly
Last active August 29, 2015 14:24
match point data to country/state/region boundary polygon
# Matching point records to polygones boundaries of a country, state or
# region polygones for regions of NZ
require(maptools)
library(maps)
library(SDMTools)
# obtain lon/lat boundaries for north and south island of NZ
nz.n.poly <- cbind(map("nz", regions = "North.Island")$x, map("nz", regions = "North.Island")$y)
nz.s.poly <- cbind(map("nz", regions = "South.Island")$x, map("nz", regions = "South.Island")$y)
# match to points where d[(6:5)] are the columns of a dataframe containing lon/lat data
@PhillRob
PhillRob / stratifiedDT.R
Last active August 29, 2015 14:27 — forked from mrdwab/stratifiedDT.R
Attempt to rewrite stratified for `data.table`. The `data.frame` version can be found at https://gist.github.com/mrdwab/6424112
stratifiedDT <- function(indt, group, size, select = NULL,
replace = FALSE, keep.rownames = FALSE,
bothSets = FALSE) {
require(data.table)
if (is.numeric(group)) group <- names(indt)[group]
if (!is.data.table(indt)) indt <- as.data.table(
indt, keep.rownames = keep.rownames)
if (is.null(select)) {
indt <- indt
} else {
@PhillRob
PhillRob / nat.year.nzpcn.R
Last active September 1, 2015 11:44
this runs through nzpcn.org.nz and gets native range and naturalised year. used to run: w<-matrix(rep(1:7700)) test <- sapply(w, function(x) nat.year.nzpcn(x))
nat.year.nzpcn <- function(x) {
sp <- getURL(paste0("http://nzpcn.org.nz/flora_details.aspx?ID=", x))
if (length(grep("Error: Cannot find species with ID =", sp))) {
return("Not_found")
} else {
flush.console()
sp.st <- unlist(gregexpr(pattern = "<title>", sp, ignore.case = F,
fixed = T))
sp.end <- unlist(gregexpr(pattern = " | New Zealand Plant Conservation Network",
sp, ignore.case = F, fixed = T))
@PhillRob
PhillRob / getGeoCode.R
Last active September 17, 2015 08:21
long/lat data from location description
getGeoCode <- function(gcStr, countryID) {
#library("RJSONIO") #Load Library
print(gcStr)
apik<-"AIzaSyCAaXX1jrJoD19uHBUnL97xpegOUxsDeSw"
apik1<-"AIzaSyA1rQIGrUfizL6E4mE22RhUh_X5saYPMU0"
gcStr <- gsub(' ','%20',gcStr) #Encode URL Parameters
#Open Connection
data.json <- fromJSON(paste(getURL(paste('https://maps.google.com/maps/api/geocode/json?sensor=false&address=',gcStr, '&components=country:',countryID,sep=""))))
#'&key=',apik1
#Flatten the received JSON
@PhillRob
PhillRob / get.eol.trait.R
Last active September 24, 2015 06:58
returns the first 8 trait values for any given species in EOL relying on the reol, traints and dplyr packages.
get.eol.trait <- function(sp,trait)
{
if (length(trait)==0)
{
print("need trait")
stop()
}
library("reol"); library("traits"); library("dplyr")
pages<-DownloadSearchedTaxa(sp, to.file=F, MyKey=NULL, verbose=TRUE)
pages1<-as.numeric(gsub("eol","",colnames(as.data.frame(pages))))
@PhillRob
PhillRob / issg-check.R
Last active October 21, 2015 10:28
checks if species is included in issg.org
isgg<- function(x) {
species<-gsub(" ","+",x)
sp <- getURL(paste0("http://www.issg.org/database/species/search.asp?sts=sss&st=sss&fr=1&x=16&y=13&sn=", species,"&rn=&hci=-1&ei=-1&lang=EN"))
if (length(grep("No invasive species currently recorded",sp))) {
return("Not_found")
} else {return("Found")}
}
@PhillRob
PhillRob / kew.sid.scraping.R
Last active December 31, 2015 07:24
scraping for dispersal data on the kew/sid website.
disp.sp <- function(x) {
print(x)
x.genus <- unlist( strsplit(x," "))[1]
x.species <- unlist( strsplit(x," "))[2]
sp.test <- getURL(paste0("http://data.kew.org/sid/SidServlet?Clade=&Order=&Family=&APG=off&Genus=", x.genus,"&Species=",x.species,"&StorBehav=0&DsFlag=on"))
if (grepl("0 records found.", sp.test))
{
#return("Not_found")
results<-c(x,"Not_found")
@PhillRob
PhillRob / harvesine.dist.R
Created January 2, 2016 05:50
test the distance between two strata within a species.
ha <- function(x){
x$Lat<-(as.numeric(paste(x$Lat)))
x$Lon<-(as.numeric(paste(x$Lon)))
pre<-x[(x[,17]==0),]
post<-x[(x[,17]==1),]
if(nrow(pre)<2| nrow(post)<2){results<-paste("not enought records per phase")}else{
early <- x[(which.min(x$Year)),]
e1 <- early[1,]
predist<-apply(pre, 1, function(x) distHaversine(as.numeric(e1[4:3]),as.numeric(x[4:3]), r=6378137))
postdist<-apply(post, 1, function(x) distHaversine(as.numeric(e1[4:3]),as.numeric(x[4:3]), r=6378137))