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
| # Round x up to the nearest multiple of n. | |
| roundup <- function(x, n){ceiling(x / n) * n} |
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
| # Remove unused factor levels in a data frame. | |
| # Base R's droplevels() does exactly this (drops unused levels from every factor | |
| # column) and is the recommended replacement for the original hand-rolled loop, | |
| # which errored on non-factor columns. | |
| dropall <- function(x){ | |
| droplevels(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
| #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. |
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
| #A likelihood function for GOMPERTZ, GOMPERTZ-MAKEHAM 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]) |
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
| # Rotate a matrix through 90 degrees clockwise. | |
| # drop = FALSE keeps the result a matrix even when m has a single row. | |
| m <- t(m[nrow(m):1, , drop = FALSE]) |
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
| #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. | |
| numValue = c(seq(1,30,length=333), # for red | |
| seq(30,50,length=333), # for yellow |
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
| # Rice on a chessboard: 1 grain on the first square, doubling each square. | |
| # Vectorised: square i holds 2^(i-1) grains. | |
| RiceOnASquare <- 2^(0:63) | |
| sum(RiceOnASquare) | |
| # NOTE: the true total is 2^64 - 1 = 18,446,744,073,709,551,615, which exceeds | |
| # R's exact double-precision integer range (2^53). For the exact value use gmp: | |
| # library(gmp); sum(as.bigz(2)^(0:63)) |
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
| #Stochastic geometric population growth rate | |
| #Simulation settings | |
| pgr = 1.05 | |
| var.pgr = 0.1 | |
| startPop = 10 | |
| nGen = 100 | |
| ntrials = 1000 | |
| pseudoExtinction = 1 |
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
| palettebuildr <- function(pathToJPEG = "logo.jpg", ncols = 3, dist.method = "euclidean", 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)) |
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
| # How to subset the COMADRE Animal Matrix Database (works with COMPADRE too) | |
| # | |
| # NOTE: this uses the legacy list-style COMADRE object (metadata / mat / matrixClass). | |
| # Modern workflows use Rcompadre: `db <- cdb_fetch("comadre")` then | |
| # `subset(db, Order %in% c(...))`. See https://github.com/jonesor/Rcompadre | |
| # Create a vector of indices to retain | |
| subsetID <- which(comadre$metadata$Order %in% c("Monotremata", "Didelphimorphia", | |
| "Paucituberculata", "Microbiotheria", | |
| "Dasyuromorphia", "Peramelemorphia", |
OlderNewer