Skip to content

Instantly share code, notes, and snippets.

View kgturner's full-sized avatar

Kathryn G Turner kgturner

View GitHub Profile
@kgturner
kgturner / Extract.climate.data.R
Last active May 8, 2020 22:23
Extracting climate data from WorldClim.org gridded climate data files (geoTIFFs) for specific locations, and converting the climate information into a useful data table. See http://wp.me/p1Ye5e-aD for more info. To beautify resulting dataframe, see https://gist.github.com/kgturner/6644150.
#Extract climate data from WorldClim.org tiles for several locations and make data table
#Kathryn Turner Sept 16, 2013
#download and unzip all relevant WorldClim geoTIFF files into a single directory.
#I used the highest resolution (~1km2), but should work for other resolutions too.
#load packages: raster, rgdal, foreach
library(rgdal)
library(raster)
library(foreach)
@kgturner
kgturner / getGBIFdatas.R
Created March 27, 2019 17:08
Get large amounts of GBIF.org occurence data
#Download occurence data from GBIF - for few to many spp.
#3/25/2019 KG Turner with assistance from S. Chamberlain, rOpenSci.org
#Register at gbif.org. You will need to use this email address, user name, and password in the following script.
#NB: DON'T COMMIT YOUR PASSWORDS.
# R version 3.5.3 (2019-03-11)
library(rgbif) #1.2.0
@kgturner
kgturner / setCompositionRandomizer.R
Last active March 30, 2018 21:59
Generate list of vectors with limited overlap. Useful for randomly assigning individuals from a pool to subsets (such as experimental blocks) with limited similarity between subsets..
#generate random number sets with limited overlap
#3/28/2018
#with em-bellis
####generate random integers from a pool####
#from 1-60, sample 10
sample.int(60, 10)
#[1] 34 42 45 54 3 5 20 25 53 40
#such as IDs of populations or individuals from a list
@kgturner
kgturner / Squishr.func.R
Last active December 23, 2015 13:48
Squish multiple columns with non-overlapping values into a single column, and get rid of NAs. Also, squish multiple groups of columns together, over a whole data frame. See http://wp.me/p1Ye5e-aJ for more information. Useful following data extraction from WorldClim.org geoTIFF files (https://gist.github.com/kgturner/6643334)
#Functions to squish groups of columns together
#Kathryn Turner Sept 20, 2013
#With help from Andrew MacDonald @polesasunder
#given data that have many NAs and non-overlapping values
#across several columns, with similarly named columns within a group
> head(clim)
alt_07.tif alt_11.tif alt_12.tif alt_15.tif alt_16.tif alt_17.tif bio1_07.tif bio1_11.tif bio1_12.tif bio1_15.tif bio1_16.tif bio1_17.tif bio10_07.tif
BG001 NA NA NA NA 61 NA NA NA NA NA 121 NA NA
CA001 NA 24 NA NA NA NA NA 100 NA NA NA NA NA
@kgturner
kgturner / Minima.mer.func.R
Last active December 22, 2015 06:19
Find two minima around a peak, given data structured like an output kmer counting histogram table from jellyfish. Requires 'zoo' and 'ggplot2' packages. More information at http://alienplantation.wordpress.com/2013/09/04/nuts-and-bolts-finding-minima-around-a-peak-in-r/
#####Minima.mer.func.R#####
#Kathryn Turner June 21, 2013
#Given an output kmer counting histogram table from jellyfish,
#find two minima around a peak
#input table from jellyfish
dat <- read.table("data.from.jellyfish.hist", head=T)
head(dat)
frequency multiplicity
@kgturner
kgturner / ModernMapEurasia.R
Created June 17, 2013 19:55
Lambert azimuthal equal area projection of the northern hemisphere, with species range color coded by country, and points for collection locations. See http://wp.me/p1Ye5e-a5 for relevant blog post.
#ModernMapEurasia.r
#Kathryn Turner, June 17, 2013
#With extensive help from Andy South
#to create a reprojected map of the Northern Hemisphere
#color code countries for range
#and add collection sites
#in a Lambert azimuthal equal area projection, with the North Pole in the center
library(rgdal) # Commands for reprojecting the vector data.
@kgturner
kgturner / lat_long.R
Created April 22, 2015 22:01
Converting between different forms of latitude and longitude (ddm, dds, dd) in R. *Messy version direct from my code, sorry*
#latitude and longitude
#convert types and add to design files
popgps<- read.table(file.choose(), header=T, sep=",",quote='"', row.names=1) #csv of pop, lat, long
popgps<-popgps[,1:3]
popgps$Longitude<-as.character(popgps$Longitude)
popgps$Latitude<-as.character(popgps$Latitude)
#add two pops to df
popgps["SAND",1]<-"48:12.055"
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.