Skip to content

Instantly share code, notes, and snippets.

View arcaravaggi's full-sized avatar

Anthony Caravaggi arcaravaggi

View GitHub Profile
@arcaravaggi
arcaravaggi / simpleCap.R
Last active November 13, 2017 11:48
Capitalise the first letter of each word in a string
# Capitalise the first letter of each word in a string
# From https://stackoverflow.com/questions/6364783/capitalize-the-first-letter-of-both-words-in-a-two-word-string
#
# e.g.
# name <- c("great shearwater", "black-browed albatross", "southern giant petrel")
# sapply(name, simpleCap)
simpleCap <- function(x) {
s <- strsplit(x, " ")[[1]]
paste(toupper(substring(s, 1,1)), substring(s, 2),
sep="", collapse=" ")
@arcaravaggi
arcaravaggi / maxDup_NAs.R
Created January 31, 2018 11:51
Split data by focal column and remove duplicates from a span to columns, keeping the one with the most data
# Create a dataframe of example data (here derived from raptor growth metrics)
df <- matrix(c(24,NA,365,1,NA,NA,6,33,NA,10,59,NA,37,300,477,NA,233,312,NA,NA,450,4,28,49),byrow = T, 8,3)
colnames(df) <- c("age","wing","mass")
df <- as.data.frame(df)
df$loc=c("Flev","Flev","Flev","Ters","Ters","Schi","Schi","Schi")
df$yr=c("2004","2004","2004","2007","2007","2004","2004","2008")
df
# split data by focal column and remove duplicates from a span to columns, keeping the one with the most data
@arcaravaggi
arcaravaggi / power_analysis.R
Created February 14, 2018 17:14
Simple power analysis in R
install.packages("pwr")
library(pwr)
# pwr.anova.test(k = , n = , f = , sig.level = , power = )
# k = number of groups
# n = number of samples
# f = effect size
# sig.level = significance level
# power = power (0-1)
#
@arcaravaggi
arcaravaggi / presabRas.R
Last active February 14, 2018 17:21
Create species presence/absence raster
# Create species presence/absence raster
# mask.raster = raster of focal area
# species.data = presence points of focal species
# raster.label = label for raster (e.g. species name)
#
# example
# pa.ras <- presabRa(mask.raster = raster1, species.data = hares, raster.label = "Lepus sp.")
# See https://amywhiteheadresearch.wordpress.com/2016/01/25/extracting-raster-data-using-a-shapefile/#more-918 for more info]
presabRas <- function (mask.raster,species.data,raster.label="") {
require(raster)
@arcaravaggi
arcaravaggi / euDist.R
Last active March 14, 2018 15:08
Calculate Euclidean distance between groups of ecological point data
# Function to calculate Euclidean distance between n sets of Principal Components
# Ensure all data are in data.matrix format
#
# x = data matrix 1
# y = data matrix 2
# d = distance method (pdist = unequal sets; dist = equal sets)
# e.g.
# distance <- euDist(dat1, dat2, dist = "dist")
# mean(distance)
# sd(distance)
@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
@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 / 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 / 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 / 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')