This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mm <- matrix(0, 10, 5) | |
m <- 10 | |
n <- 5 | |
> system.time( replicate(1000, apply(mm, c(1,2), function(x) sample(c(0,1),1)) ) ) # @recology_ | |
user system elapsed | |
0.414 0.015 0.429 | |
> system.time( replicate(1000, matrix(rbinom(10*5,1,.5),ncol=5,nrow=10) ) ) # @distribecology | |
user system elapsed | |
0.012 0.000 0.012 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> # Load | |
require(motmot); require(geiger); require(picante) | |
source("http://anolis.oeb.harvard.edu/~liam/R-phylogenetics/phylosig/v0.3/phylosig.R") | |
source("http://anolis.oeb.harvard.edu/~liam/R-phylogenetics/fastBM/v0.4/fastBM.R") | |
# Make tree | |
tree <- rcoal(10) | |
# Transform branch lengths | |
> system.time( replicate(1000, transformPhylo(tree, model = "lambda", lambda = 0.5)) ) # motmot |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ggplot network graphics functions | |
# g = an igraph graph object, any igraph graph object | |
# vplace = type of vertex placement assignment, one of rnorm, runif, etc. | |
gggraph <- function(g, vplace = rnorm) { | |
require(ggplot2) | |
g_ <- get.edgelist(g) | |
g_df <- as.data.frame(g_) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(inline); library(compiler); library(rbenchmark) | |
n <- 500 | |
k <- 100 | |
scott <- function(N, K) { | |
mm <- matrix(0, N, K) | |
apply(mm, c(1, 2), function(x) sample(c(0, 1), 1)) | |
} | |
scottComp <- cmpfun(scott) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example of new phylomatic | |
install_github('taxize_', 'ropensci', branch="phylomatic_fix") | |
library(taxize) | |
dat_ <- c("Amylotheca_subumbellata","Impatiens_davidi","Gonocarpus_leptothecus","Calochortus_argillosus","Gagea_sarmentosa","Lilium_lankongense","Cephalanthera_rubra","Bicuiba_oleifera","Guatteria_dolichopoda","Iteadaphne_caudata") | |
tree <- get_phylomatic_tree(taxa = dat_) | |
> tree | |
Phylogenetic tree with 10 tips and 9 internal nodes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(httr); library(XML) | |
url <- "http://data.gbif.org/ws/rest/occurrence/list?scientificname=Accipiter%20erythronemius&coordinatestatus=TRUE&maxresults=100" | |
tt <- content(GET(url), as="text") | |
out <- xmlTreeParse(tt)$doc$children$gbifResponse | |
d> getNodeSet(out, "//to:TaxonOccurrence") # throws errors even though the output is what I want | |
Namespace prefix to on TaxonOccurrence is not defined | |
Namespace prefix to on catalogNumber is not defined | |
Namespace prefix to on country is not defined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install_github('rgbif', 'schamberlain') # my branch is the newest | |
library(rgbif) | |
providers(name="UK National Biodiversity Network") # get numeric key for the NBN | |
> occurrencelist(sciname="Ursus", dataproviderkey=172, latlongdf=T) # should give you a data.frame like below | |
taxonName country decimalLatitude decimalLongitude catalogNumber earliestDateCollected latestDateCollected | |
1 Abietinaria abietina GB 59.01977 -2.9031110 13389886 1995-06-20 1995-06-20 | |
2 Abietinaria abietina GB 60.84050 -0.9006389 13389540 1988-06-02 1988-06-02 | |
3 Abietinaria abietina GB 56.55398 -5.4213445 13389523 1989-06-18 1989-06-18 | |
4 Abra alba GB 49.91424 -6.3364116 13392162 1988-09-19 1988-09-19 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Clustal doesn't like when there are spaces in species names, so `write_fasta` fixes that in case you forget. | |
write_fasta <- function (sequences, file.out) { | |
outfile <- file(description = file.out, open = "w") | |
write.oneseq <- function(sequence, name) { | |
if(grepl("\\s", name)){name<-gsub("\\s", "_", name)} else{name<-name} | |
writeLines(paste(">", name, sep = ""), outfile) | |
writeLines(sequence[[1]], outfile) | |
} | |
sapply(seq_len(length(sequences)), function(x) write.oneseq( | |
sequence=as.character(sequences[[x]]), name=names(sequences[x]))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install.packages("devtools") # need devtools to use install_github() | |
require(devtools) | |
install_github("ritis", "ropensci") # need for the function searchbyscientificname below | |
require(ritis) | |
install_github("taxize_", "ropensci", ref="downstream_fix") # Installs packages in development directly from Github | |
library(taxize) | |
tsn1 <- searchbyscientificname("Osteichthyes") # get TSN for Osteichthyes (bony fish) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# From Jeff Joy | |
par(bg=1) | |
plot(c(0,0), cex=0, xlim=c(-1,1), ylim=c(-1,1)) | |
X <- runif(100,-1,1) | |
Y <- runif(100,0,1) | |
M <- rchisq(100,1)/20 | |
points(Y~X, cex=M, pch=19, col=colors()[1]) | |
points(0,-0.5, pch=19, col=colors()[498],cex=20) | |
for (i in 1:4){polygon(locator(3),col=7)} |