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 labelled list of all active dataframes | |
my.list <- mget(ls(pattern="*_UMAC")) | |
# Use write.csv in a for loop, creating files for each and naming | |
# according to label (i.e. df name) | |
for (i in seq_along(my.list)){ | |
write.csv(my.list[i], paste(names(my.list)[i], ".csv"), col.names = 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
# Split column into multiple based on spaces, extra characters, etc. | |
df <- data.frame(x= c("crust (5)","things (3)","stuff (8)")) | |
library(tidyr) | |
df2 <- extract(df, x, into = c("pet", "sampSize"), "([^(]+)\\s+\\(([0-9]+).") | |
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
##arrange df vars by position | |
# from https://stackoverflow.com/questions/5620885/how-does-one-reorder-columns-in-a-data-frame | |
##'vars' must be a named vector, e.g. c("var.name"=1) | |
# e.g. | |
# table <- data.frame(Time=c(1,2), In=c(2,3), Out=c(3,4), Files=c(4,5)) | |
# arrange.vars(table, c("Out"=2)) | |
# arrange.vars(table, c("Out"=2, "Files"=1, "Time"=4)) | |
arrange.vars <- function(data, vars){ | |
##stop if not a data.frame (but should work for matrices as well) |
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/1401904/painless-way-to-install-a-new-version-of-r | |
# Run in the old version of R (or via RStudio) | |
setwd("C:/Temp/") | |
packages <- installed.packages()[,"Package"] | |
save(packages, file="Rpackages") | |
# INSTALL NEW R VERSION | |
if(!require(installr)) { install.packages("installr"); require(installr)} #load / install+load installr | |
# See here for more on installr: https://www.r-statistics.com/2013/03/updating-r-from-r-on-windows-using-the-installr-package/ |
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
# Read in all csv files in directory | |
files <- list.files(pattern = "*.csv") | |
# Create dfs for each csv then create list | |
for (i in 1:length(files)) | |
assign(files[i], | |
read.csv(files[i])) | |
lst <- mget(ls(pattern = "*.csv")) |
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
# if tweetnow is set to TRUE, sends the tweets directly from R to Twitter | |
# this function requires installing the rtweet package and setting up the relevant | |
# Twitter user credentials | |
# otherwise, the tweetstorm outputs to the console; the user can copy/paste to Twitter manually | |
# | |
# From https://sites.tufts.edu/emotiononthebrain/2017/08/12/time-for-a-tweetstorm/ | |
tweetstorm <- function(s, tweetnow = FALSE) { | |
#if you'll be tweeting directly from R, update the user name and token variables |
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
# E.g. qDat.corr <- as.data.frame(cor.test.p(x)) | |
cor.test.p <- function(x){ | |
FUN <- function(x, y) cor.test(x, y)[["p.value"]] | |
z <- outer( | |
colnames(x), | |
colnames(x), | |
Vectorize(function(i,j) FUN(x[,i], x[,j])) | |
) | |
dimnames(z) <- list(colnames(x), colnames(x)) |
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
# The modOverlap function calculates three metrics: | |
# Shoener’s D for niche overlap | |
# Hellinger distance between probability distributions | |
# Warren's I similarity statistic | |
# | |
# This function is also found in the fuzzysim package | |
# | |
# From https://modtools.wordpress.com/2015/10/30/modoverlap/ | |
modOverlap <- function (pred1, pred2, na.rm = 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
# published on http://www.r-statistics.com/2010/07/visualization-of-regression-coefficients-in-r | |
# originally written by "<a href="http://statmath.wu.ac.at/~zeileis/">Achim Zeileis</a>" | |
# GPL-2 | |
# | |
# E.g. coefplot(glm, parm = -1) | |
coefplot <- function(object, df = NULL, level = 0.95, parm = NULL, | |
labels = TRUE, xlab = "Coefficient confidence intervals", ylab = "", | |
xlim = NULL, ylim = NULL, | |
las = 1, lwd = 1, lty = c(1, 2), pch = 19, col = 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
# Calculate total line length and density for a given set of polygons | |
# | |
# Intersects SpatialLine and SpatialPolygon objects, calculates individual line length, | |
# appends to dataframe extracted from intersect object and summarises by polygon ID. | |
# If density is not required, do not specify area parameter `a`. | |
# | |
# The function assumes a spatial projection system where distance is given in metres. | |
# | |
# E.g. | |
# sp1 = object of class SpatialLine/SpatialLineDataFrame |
OlderNewer