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
# 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=" ") |
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
# 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 |
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
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) | |
# |
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
# 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) |
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 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) |
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 |
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
# 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
# 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
# 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') |