Skip to content

Instantly share code, notes, and snippets.

Daniel Falster dfalster

Block or report user

Report or block dfalster

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@dfalster
dfalster / load_baad.R
Last active Aug 29, 2015
Example code for downloading baad csv files and loading them in R
View load_baad.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 / Glopnet.R
Last active May 13, 2019
Simple example of reproducible research, using global leaf traits database
View Glopnet.R
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 / spp_code.R
Created Jan 3, 2014
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"))
View spp_code.R
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 / csv_metadata.R
Created Jan 3, 2014
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
View csv_metadata.R
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))))
}
@dfalster
dfalster / Zotero_extract_journal_names.R
Created Jul 20, 2013
An R script to extract a list of journal names for the articles in my zotero library.
View Zotero_extract_journal_names.R
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 / addNewData.R
Last active Jan 27, 2020
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…
View addNewData.R
##' 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'.
##'
You can’t perform that action at this time.