Skip to content

Instantly share code, notes, and snippets.

View jonesor's full-sized avatar

Owen Jones jonesor

View GitHub Profile
@jonesor
jonesor / roundup.R
Created December 4, 2012 23:13
R function to round up to a number divisible by n.
roundup <- function(x, n){ceiling(ceiling(x) / n) * n}
@jonesor
jonesor / dropall.R
Created July 3, 2014 11:37
Remove unused factor levels in a data frame
dropall <- function(x){
isFac = NULL
for (i in 1:dim(x)[2]){isFac[i] = is.factor(x[ , i])}
for (i in 1:length(isFac)){
x[, i] = x[, i][ , drop = TRUE]
}
return(x)
}
@jonesor
jonesor / circ.mean.R
Last active November 1, 2022 14:57
Calculate a circular mean
#Circular means are useful if you are dealing with data that are inherently "circular" such as the day or month of the year, or direction.
#For example, imagine your data consists of the month in which an event occurs, and you want to report the average month. If you had 3 observations in December, and 3 in February, the average should be in January (1) whereas the more conventional arithmetic mean would tell you the answer was 7. The trick to dealing with this issue is to convert the data into radians, and do a bunch of trigonometry.
#This is how you might approach it in R:
#You have 3 observations in December (12), and 3 in February.
m = c(12,12,12,2,2,2)
#First you convert these values to an angle, then to radians. There are 12 (approximately) equally spaced points in the year for month, which we specify with np.
@jonesor
jonesor / MortalityModels.R
Last active July 13, 2021 20:32
An R function and code to estimate parameters of mortality models with maximum likelihood.
#A likelihood function for GOMPERTZ, GOMPERTZ-MAKEHANM and SILER models.
likeLT <- function(lifetable,pars,type="GO"){
# Extract data from life table
Dx = lifetable$Dx
Nx = lifetable$Nx
StartInt = lifetable$StartAge
EndInt = lifetable$EndAge
LT.Type = as.character(lifetable$Type[1])
@jonesor
jonesor / matrixRotate.R
Created July 16, 2014 22:40
Rotate a matrix through 90 degrees clockwise
m = (t(m[nrow(m):1,]))
@jonesor
jonesor / color palette.R
Last active August 29, 2015 14:05
Manipulate a color palette for "heat map" type colour schemes.
library(RColorBrewer)
#Assume your data are in a data frame.
df1 <- data.frame(x = 1:100)
#Define the basic palette (this case goes from red - yellow - green, but you could make it any 3 colours.)
colCode <- colorRampPalette(c("red", "yellow", "green"))(n = 999)
#Make a vector (of length 999), but break apart the parts of the sequence that should be red yellow and green.
#This allows you to alter where the yellow "pivot point" is.
Squares <- 1:64
RiceOnASquare = 1
for (i in 2:64){
RiceOnASquare = append(RiceOnASquare,RiceOnASquare[i-1]*2)
}
sum(RiceOnASquare)
#Stochastic geometric population growth rate
#Simulation settings
pgr = 1.05
var.pgr = 0.1
startPop = 10
nGen = 100
ntrials = 1000
pseudoExtinction = 1
@jonesor
jonesor / palettebuildr.R
Last active August 29, 2015 14:15
Extract colours from a JPG file to use as a (divergent) colour palette.
palettebuildr <- function(pathToJPEG = "logo.jpg", ncols = 3, dist.method = "euclidian", clust.method = "complete"){
require(jpeg)
#Read in the jpeg file
img <- readJPEG(pathToJPEG)
#Using the whole image is overkill, especially for large files.
#Therefore, create a grid from which extract the colors
xgrid <- ceiling(seq(1, dim(img)[1], length.out = 50))
ygrid <- ceiling(seq(1, dim(img)[2], length.out = 50))
@jonesor
jonesor / subsetComadre.R
Created August 6, 2015 14:15
How to subset COMADRE Animal Matrix Database
#How to subset the COMADRE Animal Matrix Database (works with COMPADRE too)
#Create a vector of indices to retain
subsetID <- which(comadre$metadata$Order %in% c("Monotremata","Didelphimorphia","Paucituberculata","Microbiotheria","Dasyurormorphia","Peramelemorphia","Diprotodontia"))
#Subset entire COMADRE object to JUST the above subset
#First make a copy
subset.comadre <- comadre