This file contains 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 wrapper for download and cleaning of IOC bird lists | |
# This function downloads a given list, defined by the URL, | |
# completes rows with relevant information, and outputs a data frame. | |
# Further functions may be added and a small package developed, in time. | |
# | |
# There's almost certainly a more elegant way of doing this. | |
# | |
# earl = URL of IOC master List (see https://www.worldbirdnames.org/new/ioc-lists/master-list-2); currently defaults to v13.1 | |
# | |
# E.g. |
This file contains 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
##Generates a set of autocorrelated random normal variates | |
# | |
#INPUT | |
# n: number of variates to generate | |
# mean, sd: mean and standard deviation of the normal distribution | |
# r: the autocorrelation coefficient (between 0 and 1) | |
rautonorm <- function(n,mean=0,sd=1,r){ | |
ranfunc <- function(i,z,r) sqrt(1-r^2) * sum(z[2:(i+1)]*r^(i-(1:i))) + z[1]*r^i | |
z <- rnorm(n) | |
mean + sd*c(z[1], sapply(1:(n-1), ranfunc, z, r)) |
This file contains 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
# Plot histogram with density curve, and measures of dispersion from the mean | |
# | |
# dispersion x axis: | |
# V = Sample variance | |
# IQR = inter-quartile range (balanced) | |
# SD = standard deviation | |
# SE = Standard error | |
# CI = Confidence interval (see inputs) | |
# | |
# CV = coefficent of variation |
This file contains 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
# Function to reduce multiple columns to single binary vector | |
# c = data frame or columns of interest | |
binRC <- function(c){ | |
d <- as.integer(rowSums(c)>0) | |
return(d) | |
} | |
# Here's an example, using a randomly generated data frame | |
set.seed(42) | |
df <- data.frame(col1 = rbinom(n=100, size=1, prob=0.05), |
This file contains 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
# Function to load a list of packages | |
# Installs if package is absent | |
# | |
# Pass character vector | |
# E.g. ipak(c("beepr", "dplyr", "adehabitat")) | |
# | |
ipak <- function(pkg){ | |
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])] | |
if (length(new.pkg)) | |
install.packages(new.pkg, dependencies = TRUE) |
This file contains 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
# There's probably a much neater way to do this but if it works, it works. And this? It works. | |
set.seed(24) | |
df <- data.frame(col1 = rep(LETTERS[1:4], 10), | |
col2 = rep(1:10, 4)) | |
# Function to create network data objects #### | |
# - a data frame of usernames that interact with >1 hashtag ('dataframe') | |
# - edges and nodes used in creating networks ('edges', 'nodes') | |
# - the network object ('routes') |
This file contains 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 https://stackoverflow.com/questions/17022553/adding-r2-on-graph-with-facets | |
# Function for calculating R^2 | |
# Adjust the lm call as required | |
lm_eqn = function(df){ | |
m = lm(min_t ~ max_t, df); | |
eq <- substitute(r2, | |
list(r2 = format(summary(m)$r.squared, digits = 3))) | |
as.character(as.expression(eq)); | |
} |
This file contains 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
# Copy all rows in a dataframe according to their maximum value. | |
# Copy all other integers an appropriate number of times, filling the last cell with 0. | |
# | |
# By StackOverflow user, CPak: https://stackoverflow.com/a/50930471/9962100 | |
# | |
# Example | |
# df <- data.frame(A1 = c(0,2,0,3), A2 = c(1,0,2,0), A3 = c(0,1,3,2)) | |
# ans <- data.frame(A1 = c(0,2,2,0,0,0,3,3,3), A2 = c(1,0,0,2,2,0,0,0,0), A3 = c(0,1,0,3,3,3,2,2,0)) | |
# library(magrittr) | |
# test <- do.call(rbind, lapply(seq_len(nrow(df)), function(x) explodeR(df[x, ]))) %>% as.data.frame |
This file contains 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
# By Stack user forlooper | |
# https://stackoverflow.com/a/37332382 | |
# | |
# e.g. | |
# p <- extractCoords(SST_start) | |
extractCoords <- function(sp.df) | |
{ | |
results <- list() | |
for(i in 1:length(sp.df@polygons[[1]]@Polygons)) | |
{ |
This file contains 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
# Function for truncation of normal distribution | |
# n = number of iterations (default = 1000) | |
# m = mean | |
# s = SD/SE/CI | |
# l = lower bounds (default = -100) | |
# u = upper bounds (default = 100) | |
# r = round to x integers (default = 5) | |
tnorm <- function(n = 1000, m, s, l = -100, u = 100, r = 5) { | |
tdist <- round(rnorm(n, m, s), r) | |
tdist[tdist < l] <- l |
NewerOlder