Skip to content

Instantly share code, notes, and snippets.

View timcdlucas's full-sized avatar

Tim Lucas timcdlucas

View GitHub Profile
@timcdlucas
timcdlucas / gist:4c44c0b4fe59592d385a
Created February 15, 2016 17:48
Compare slopes bewteen two datasets.
# Make up some data
data1 <- data.frame(length = rnorm(20, 8), weight = 1:20)
data2 <- data.frame(length = rnorm(30, 8), weight = 1:30)
# add a dummy variable and combine the data
data1$dummy <- 'data1'
@timcdlucas
timcdlucas / Mathematica2R.R
Created February 4, 2016 14:58
Convert Mathematica array text to R array
#' Turn a string copied from Mathematica into an R array
#'
#'@param text Text representation of a mathematica array.
#'@param drop Logical. If TRUE, 1 and 2 dimensional arrays will be returned as vectors and matrices.
#' If FALSE, 1 and 2 dimensional arrays will be returned.
#'
#'@return An array
#'@export
@timcdlucas
timcdlucas / r2math.R
Last active January 26, 2016 15:23
R to Mathematica
# Print an R matrix in a form that can be copied straight into mathematica
MathematicaPaste.matrix <- function(m){
charmat <- apply(m, 2, as.numeric)
charmat[, -NCOL(charmat)] <- paste0(charmat[, -NCOL(charmat)], ',')
charmat <- rbind('{', t(charmat), '},')
charmat[dim(charmat)[1], dim(charmat)[2]] <- '}'
@timcdlucas
timcdlucas / cloglog.R
Created January 20, 2016 11:37
equations for predicting cloglog models
d <- data.frame(x = rnorm(100) + c(1, 2), y = c(0, 1))
m <- glm(y ~ x, data = d, family = binomial(link = cloglog))
newx <- seq(-5, 5, length.out = 100)
predict.line <- predict(m, newdata = data.frame(x = newx), type = 'response')
plot(y ~ x, d)
@timcdlucas
timcdlucas / scrapeCitations.R
Last active October 27, 2015 21:04
Scrape pubmed or scholar
library(dplyr)
# Web scraping.
library(rvest)
# For synonym list
library(taxize)
library(ggplot2)
x <- data.frame(y = rnorm(100), g = rep(letters[1:4], 25))
ggplot(x, aes(y = y, x = g, fill = g)) +
geom_boxplot() +
ylim(c(-15, 15)) +
scale_fill_manual(values = grey(c(0, 0.5, 0.9, 1))) +
data(shorebird)
shorebird <- comparative.data(shorebird.tree, shorebird.data, Species, vcv=TRUE, vcv.dim=3)
mod1 <- pgls(log(Egg.Mass) ~ log(M.Mass) * log(F.Mass), shorebird)
print(mod1)
mod1.sum <- summary(mod1)
print(mod1.sum)
mod1.aov <- anova(mod1)
print(mod1.aov)
@timcdlucas
timcdlucas / pubmedCites.R
Created July 25, 2015 10:40
Scrape the number of citations on pubmed
scrapePub <- function(sp){
Sys.sleep(2)
spString <- tolower(gsub(' ', '+', sp))
url <- paste0('http://www.ncbi.nlm.nih.gov/pubmed/?term=%22', spString, '%22')
page <- html(url)
@timcdlucas
timcdlucas / scholarCites.R
Created July 23, 2015 21:56
Scrape the number of references found on google scholar.
library(rvest)
library(magrittr)
# Our search string
sp <- "Myotis myotis"
spString <- tolower(gsub(' ', '+', sp))
url <- paste0('https://scholar.google.co.uk/scholar?hl=en&q=%22',
@timcdlucas
timcdlucas / test.html
Created April 22, 2015 21:07
test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script src="lib/htmlwidgets-0.3.2/htmlwidgets.js"></script>
<script src="lib/jquery-1.11.1/jquery.min.js"></script>
<link href="lib/leaflet-0.7.3/leaflet.css" rel="stylesheet" />
<script src="lib/leaflet-0.7.3/leaflet.js"></script>
<link href="lib/leafletfix-1.0.0/leafletfix.css" rel="stylesheet" />
<script src="lib/leaflet-binding-0.0.11/leaflet.js"></script>