Skip to content

Instantly share code, notes, and snippets.

View egouldo's full-sized avatar

Elliot Gould egouldo

View GitHub Profile
@egouldo
egouldo / bat_dat.csv
Last active May 18, 2023 04:45
Straka, T. M., Lentini, P. E., Lumsden, L. F., Wintle, B. A., & Ree, R. (2016). Urban bat communities are affected by wetland size, quality, and pollution levels. Ecology and Evolution, 6(14), 4761-4774.
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 83 columns, instead of 58. in line 1.
Site,Habitat,Season,Chgouldii,Chmorio,CG/Mormlp,MormII,Mschocean,Nyctspp,Mmacropus,Nspp/Mmacr,Scotspp.,Taustralis,Vdarlingtoni,Vregulus,Vvulturnus,Unidentified,BatSpecies,BatActivity,Col,Dip,Eph,Hem,Lep,Hym,Tri,Manto,Psoc,Orth,Neur,Derm,Bla,Iso,Odo,Plec,Insectabundance,TotalOrder,Moon,NatEx,Bioregion,Size,SQQ,Water Depth,Imp 5km,Imp2km,Imp1km,Imp500m,NDVI2km,NDVI1km,NDVI500m,Trees 5km,Trees2km,Trees1km,Trees500m,House500,House1km,House2km,House5km,Light500m,Light1km,Light2km,Light5km,Road500m,Road1km,Road2km,Road5km,Distriver,Distwetland,Distwater,Distbushland,Aqvegcov,EphemVeg,TreeCoverage,TreesLarge>20dbh.plot,TreesSmall<20dbh.plot,Trees total.plot,Shrubs.plot,Hollows.total,Otherroostsnumber.total,TotalRoost,Distance1sttree,Temp,Humidity
Albert Park,1,1,151,0,6,0,0,10,0,10,8,1,0,0,1,8,7,185,6,20,48,0,13,1,16,0,0,0,0,0,0,0,0,0,104,6,2,2,1,508500,6.352532434,4,307.4648102,319.4008886,451.1150505,1693.27663,0.084813599,0.306702905,0.418516578,154467,16004,8789,1749,13147478.69,52767347.28,135155830.3,542156685
@egouldo
egouldo / debugging pointblank.md
Created March 8, 2021 08:24
Rmarkdown reprex illustrating unexpected pointblank rendering behaviour
title author date output
Reprex: rendering fails or incomplete for list of pointblank tables in rmarkdown documents
Elliot Gould
08/03/2021
html_document keep_md
true
@egouldo
egouldo / debugging pointblank.Rmd
Created March 8, 2021 08:21
Rmarkdown reprex illustrating unexpected pointblank rendering behaviour
---
title: "Reprex: rendering fails or incomplete for list of pointblank tables in rmarkdown documents"
author: "Elliot Gould"
date: "08/03/2021"
output:
html_document:
keep_md: yes
---
```{r setup, include=FALSE}
@egouldo
egouldo / gam_fun_rabbits
Created February 21, 2017 00:05
Fit gam with dynamic df and choice of 2 rabbit variables -- example of using substitute and eval
gam_fun <- function(data, rabbit_var = c("rabbitwarrens", "warrenspretreatment"), df = integer(length = 1)){
if(rabbit_var == "rabbitwarrens"){
f <- substitute(gam(peren_chen_pc_cover ~ s(winterrain, df) + s(otherrain, df) + year + site + s(warrenspretreatment, df) + s(regionalroos, df) + landsystem, data=data, family=gaussian))
} else{
f <- substitute(gam(peren_chen_pc_cover ~ s(winterrain, df) + s(otherrain, df) + year + site + s(rabbitwarrens, df) + s(regionalroos, df) + landsystem, data=data, family=gaussian))
}
eval(f)
}
@egouldo
egouldo / numbers2words.R
Created September 13, 2016 00:54 — forked from psychemedia/numbers2words.R
R function to convert numbers to words
#https://github.com/ateucher/useful_code/blob/master/R/numbers2words.r
numbers2words <- function(x){
## Function by John Fox found here:
## http://tolstoy.newcastle.edu.au/R/help/05/04/2715.html
## Tweaks by AJH to add commas and "and"
helper <- function(x){
digits <- rev(strsplit(as.character(x), "")[[1]])
nDigits <- length(digits)
if (nDigits == 1) as.vector(ones[digits])
@egouldo
egouldo / create_latex.Rd
Last active August 29, 2015 14:26
creates a latex fragment (as opposed to standalone latex document) from an Rmarkdown file. Useful for including knitted latex into a bigger latex document.
create_latex <- function(f){
knitr::knit(f, 'tmp-outputfile.md');
newname <- paste0(tools::file_path_sans_ext(f), ".tex")
mess <- paste('pandoc -f markdown -t latex -p -o', shQuote(newname),"tmp-outputfile.md")
system(mess)}
# The function above takes an `Rmd` file as its input, knits the file to `md`, and then converts it to `TeX` by calling pandoc from the command-line. The secret ingredient lies in the pandoc call... When knitting using Rstudio, Rstudio calls the pandoc standalone `-s` flag when it compiles the pdf. This generates a 'standalone' document, i.e. one that contains latex preamble. This is obviously necessary when you want to view the PDF, but is problematic when you want to generate a latex fragment only from Rmd.
# I instead sought to generate a latex 'fragment' from `Rmd` with knitr, that can be later incorporated into a main TeX file. So the solution was simply to create a pandoc command line call that omits the `-s` standalone flag. I achieved this by calling pandoc from R with
@egouldo
egouldo / countsToCases.R
Last active July 24, 2017 11:14
Convert contingency table (counts) to cases
# Source: http://www.cookbook-r.com/Manipulating_data/Converting_between_data_frames_and_contingency_tables/#countstocases-function
# Convert from data frame of counts to data frame of cases.
# `countcol` is the name of the column containing the counts
countsToCases <- function(x, countcol = "Freq") {
# Get the row indices to pull from x
idx <- rep.int(seq_len(nrow(x)), x[[countcol]])
# Drop count column
x[[countcol]] <- NULL
@egouldo
egouldo / KfoldCASEFILE.Rd
Created July 23, 2015 15:11
This function is used to randomly split a Netica casefile into K splits for K-fold cross validation. K is specified by the user. Seed is also set by the user for reproducibility. Path1 and Path2 are character file paths to the directory of the write.paths for the training and holdout case files, respectively. File path arguments should end with …
## FUNCTION - KfoldCASEFILE
# splits an R object of a case file into 5 Folds (When K is set to 5)
# Outputs the object into five pairs (When K = 5) of training and test case files in .cas format
# The input case file object must contain a column called "IDnum"
KfoldCASEFILE<-function(CaseFile, seed,k, path1, path2){
# Renumber the IDnum column by order IF the case file is missing a number in a sequence
CaseFile<-IDnumOrdered(CaseFile)
#1. Setting the folds
#2. Join the folds to the casefile object by dplyr::innerjoin (by cols are index in folds and idnum in casefile object)
set.seed(seed)
##' 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'.
##'