Skip to content

Instantly share code, notes, and snippets.

View arcaravaggi's full-sized avatar

Anthony Caravaggi arcaravaggi

View GitHub Profile
@arcaravaggi
arcaravaggi / IOCmasteR.R
Created April 3, 2023 10:28
A wrapper for download and cleaning of IOC bird Master Lists
# 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.
@arcaravaggi
arcaravaggi / autonoRm.R
Last active July 21, 2022 13:04
Generate a set of autocorrelated random normal variates
##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))
@arcaravaggi
arcaravaggi / vecQ.R
Last active July 24, 2023 15:02
Generate dispersion (including plot) and summary metrics for a given vector
# 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
@arcaravaggi
arcaravaggi / binRC.R
Last active July 21, 2022 13:10
Reduce multiple columns to single binary vector
# 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),
@arcaravaggi
arcaravaggi / ipak.R
Created April 26, 2020 09:08
Function to load a list of packages and install package if absent
# 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)
@arcaravaggi
arcaravaggi / confNet.R
Last active April 17, 2020 09:21
Function to create network data objects
# 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')
@arcaravaggi
arcaravaggi / facetR2.R
Created October 2, 2018 10:17
Label ggplot facet plots with facet group name and R^2
# 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));
}
@arcaravaggi
arcaravaggi / explodeR.R
Created June 19, 2018 14:24
Copy row duplicating data Xi times
# 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
@arcaravaggi
arcaravaggi / extractCoords.R
Created April 18, 2018 12:21
Extract coordinates from SpatialPolygon object
# 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))
{
@arcaravaggi
arcaravaggi / tnorm.R
Created March 21, 2018 16:33
Function for the truncation of normal distribution
# 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