Skip to content

Instantly share code, notes, and snippets.

View jebyrnes's full-sized avatar

Jarrett Byrnes jebyrnes

View GitHub Profile
@jebyrnes
jebyrnes / addZeroes.R
Last active August 29, 2015 13:56
A demonstration of how to add rows with zeroes using melt and dcast. There are other ways, but this is pretty simple.
################################
# Problem: You have a ragged
# data frame where species that have not
# been seen as a site simply don't have a
# line in your data frame. You have a long
# data frame, but you want a long data frame
# where missing species have proper zeroes
#
# Solution: a combination of dcast and melt
# from the reshape2 package
@jebyrnes
jebyrnes / germany_biodepth_zissou.R
Created March 31, 2014 21:25
A multifunctionality analysis using the threshold effect for Biodepth in Germany using the Zissou color scheme from Karthik's wesanderson package for color palates.
library(multifunc)
library(wesanderson)
data(all_biodepth)
allVars<-qw(biomassY3, root3, N.g.m2, light3, N.Soil, wood3, cotton3)
germany<-subset(all_biodepth, all_biodepth$location=="Germany")
vars<-whichVars(germany, allVars)
#re-normalize N.Soil so that everything is on the same sign-scale (e.g. the maximum level of a function is the "best" function)
library(multifunc)
data(all_biodepth)
allVars<-qw(biomassY3, root3, N.g.m2, light3, N.Soil, wood3, cotton3)
germany<-subset(all_biodepth, all_biodepth$location=="Germany")
vars<-whichVars(germany, allVars)
#re-normalize N.Soil so that everything is on the same sign-scale (e.g. the maximum level of a function is the "best" function)
germany$N.Soil<- -1*germany$N.Soil +max(germany$N.Soil, na.rm=TRUE)
@jebyrnes
jebyrnes / dcov.test
Created October 23, 2014 19:09
misbehaving_dcov.Rnw
\documentclass{article}
\begin{document}
<<echo=FALSE>>=
activityDF <- structure(list(julian_day = 1:365, food = c(92.6182247978374,
199.361820535986, 128.619308542186, 131.048504694701, 44.6344539529983,
139.02542881751, 118.815437972886, 85.5963235153311, 73.9459098960665,
85.6671795660157, 195.954639346317, 192.942941724749, 193.341526021202,
142.419581610027, 130.23002915561, 232.843857188757, 105.14817987285,
131.58751463704, 123.30056211349, 150.447279408636, 138.40775613945,
@jebyrnes
jebyrnes / scatterPlot3d.R
Last active August 29, 2015 14:10
Plotting in 3D in R
#just create a range of values
grdVals <- function(x, bound=0.01) seq(range(x)[1]-bound, range(x)[2]+bound, length.out=50)
#instantiate a new persp object for a scatterplot
makeScatterBox <- function(x,y,z, bound=0, ...){
x <- grdVals(x, bound=bound)
y <- grdVals(y, bound=bound)
z <- grdVals(z, bound=bound)
z <- matrix(rep(z, 50), nrow=50)
@jebyrnes
jebyrnes / leafletMapWithLegend.R
Last active August 29, 2015 14:19
A demo of leaflet using a color legend
#devtools::install_github("rstudio/leaflet", ref="feature/color-legend")
library(leaflet)
library(RColorBrewer)
set.seed(100)
pdf <- data.frame(Latitude = runif(100, -90,90), Longitude = runif(100, -180,180))
#make a property with colors
pdf$Study <- rep(1:10,10)
#need to create a pal using colorbin
@jebyrnes
jebyrnes / projects.csv
Created November 13, 2011 00:23
Code to scrape RocketHub's xml feeds for the #SciFund projects, add it to a log file, then plot it. Run the data scrape without appending the first time to generate a log file.
We can make this file beautiful and searchable if this error is corrected: Unclosed quoted field in line 9.
"ID","Title","Name","Project.Link","Goal"
1,"Why is this Dolphin's Fin on Backwards!?","Matthew S. Leslie","http://www.rockethub.com/projects/3754-why-is-this-dolphin-s-fin-on-backwards","5000"
2,"Tracking the migration of the Atlantic Puffin","Robin Freeman","http://www.rockethub.com/projects/3818-tracking-the-migration-of-the-atlantic-puffin","5000"
3,"Cancer? Yeast has the answers","Marisa Alonso-N??ez","http://www.rockethub.com/projects/3753-cancer-yeast-has-the-answers","2500"
4,"Athlete's Foot in Worms?","Rebecca Rashid Achterman","http://www.rockethub.com/projects/3703-athlete-s-foot-in-worms","1000"
5,"Support Zombie Research!","Kelly Weinersmith","http://www.rockethub.com/projects/3737-support-zombie-research","3500"
6,"Doctor Zen and the Amazon Crayfish","Zen Faulkes","http://www.rockethub.com/projects/3695-doctor-zen-and-the-amazon-crayfish","1000"
7,"C-Cilia in Motion!!","Aditya Rao","http://www.rockethub.com/projects/3792-c-cilia-in-motion","5000"
8,"Force of Duck: Measuring explosive erection","
@jebyrnes
jebyrnes / allPredPrey.r
Created December 7, 2011 23:06
Simulations of all predators and prey applying the p(eaten) formula for a probabilistic approach to food webs
###############FUNCTIONS
#If you find the 0 predators remaining formulation more intuitive
pEaten2<-function(Sp.max, E, prey.vec) {
1-mean(dhyper(0,prey.vec, Sp.max-prey.vec, Sp.max-E))
}
#vectorize it
pEaten2<-Vectorize(pEaten2, vectorize.args="E")
#generate all permutations of prey being eaten
@jebyrnes
jebyrnes / iworkfortheinternet.R
Created December 14, 2011 01:26 — forked from sckott/iworkfortheinternet.R
Code for searching Twitter using the twitteR #rstats package.
require(plyr); require(stringr); require(ggplot2); require(lubridate); require(twitteR)
##need to figure out why there are two datout objects and not just 1...
twitterPlot<-function(myString, n=1500, since=NULL, until=NULL){
since2<-as.character(strptime(since, '%Y-%m-%d')+2*24*60*60)
until2<-as.character(strptime(until, '%Y-%m-%d')+2*24*60*60)
datout_1 <- searchTwitter(myString, n = n, since=since, until=until)
datout_2 <- searchTwitter(myString, n = n, since=since2, until=until2)
@jebyrnes
jebyrnes / goal_success_scifund.R
Created February 28, 2012 23:16
Analysis code for looking at the relationship between project goal and % success from #SciFund. Note, I did it with my own private data set that has a lot of merged fields in it, but you can run this analysis or something like it with Zen's data at http:
source("./sciFundFunctions.r")
library(gridExtra)
#################################################
#######HISTOGRAMS
#################################################
#total raised histogram distribution
raised<-qplot(total, data=projects) +
theme_bw(base_size=16) +
xlab("\nTotal Raised") +