Skip to content

Instantly share code, notes, and snippets.

View dfalster's full-sized avatar

Daniel Falster dfalster

View GitHub Profile
@dfalster
dfalster / load_baad.R
Last active August 29, 2015 14:20
Example code for downloading baad csv files and loading them in R
# first define a function that downloads and loads baad from
# Ecological archives
load_baad <- function(filename = "downloads/baad_data.zip") {
path <- dirname(filename)
# download
if(!file.exists(filename)){
dir.create(path, showWarnings = FALSE, recursive = TRUE)
@dfalster
dfalster / spp_code.R
Created January 3, 2014 04:41
Function to convert vector of binomial species names into a vector of 6 letter abbreviations, taking first 3 letters from first and second names. Example: spp_code(c("Quassia baileyana","Rhodomyrtus trineura","Rockinghamia angustifolia"))
spp_code <- function(species_names){
sapply(strsplit(species_names, " "), function(x) tolower(paste0(substr(x[1], 1,3), substr(x[2], 1,3))))
}
@dfalster
dfalster / Zotero_extract_journal_names.R
Created July 20, 2013 04:22
An R script to extract a list of journal names for the articles in my zotero library.
library("RSQLite")
# connect to zotero database
pathToZotero <- "~/Zotero" # change this for your setup
m <- dbDriver("SQLite")
con <- dbConnect(m, file.path(pathToZotero, "zotero.sqlite"))
# extract list of journals
@dfalster
dfalster / Glopnet.R
Last active February 10, 2023 09:02
Simple example of reproducible research, using global leaf traits database
library(xlsx)
library(smatr)
# get data, from publication Wright et al 2004. DOI: [10.1038/nature02403](http://doi.org/10.1038/nature02403)
download.file("http://www.nature.com/nature/journal/v428/n6985/extref/nature02403-s2.xls", "wright-2004.xls")
dat.Wright <- read.xlsx2("wright-2004.xls", sheetIndex=1, startRow=11, stringsAsFactors=FALSE, check.names=TRUE)
## Clean data
dat.Wright <- dat.Wright[names(dat.Wright) != " "] # Drop blank columns
for(v in c("log.LMA","log.LL"))
@dfalster
dfalster / addNewData.R
Last active February 19, 2023 00:29
The function addNewData.R modifies a data frame with a lookup table. This is useful where you want to supplement data loaded from file with other data, e.g. to add details, change treatment names, or similar. The function readNewData is also included. This function runs some checks on the new table to ensure it has correct variable names and val…
##' Modifies 'data' by adding new values supplied in newDataFileName
##'
##' newDataFileName is expected to have columns
##' c(lookupVariable,lookupValue,newVariable,newValue,source)
##'
##' Within the column 'newVariable', replace values that
##' match 'lookupValue' within column 'lookupVariable' with the value
##' newValue'. If 'lookupVariable' is NA, then replace *all* elements
##' of 'newVariable' with the value 'newValue'.
##'
@dfalster
dfalster / csv_metadata.R
Created January 3, 2014 01:27
Function to help write metadata for csv files. Assuming your headers are on line 1 of a file, this function takes the headers and transposes them, printing to a new file with suffix "metadata_" and with columns variable, units, description, notes
csv_metadata <- function(filename){
data <- read.csv(filename, stringsAsFactors=FALSE, fill=TRUE, header=TRUE, check.names = FALSE)
write.csv(data.frame(variable=names(data), units="",description="", notes=""), quote=TRUE, row.names=FALSE,
file = file.path(dirname(filename), paste0("metadata_",basename(filename))))
}