Skip to content

Instantly share code, notes, and snippets.

View dwbapst's full-sized avatar

David Bapst dwbapst

  • College Station, TX
View GitHub Profile
@dwbapst
dwbapst / count words in PDFs.R
Created November 15, 2017 10:26
Ever need to get word counts for a directory of PDFs, using nothing but R? Have I got a solution for you!
library(pdftools)
# finds files in current directory
pdfs <- list.files(pattern = "pdf", full.names = TRUE)
# uses pdftools to convert pdfs to plain-text, replaces line breaks with spaces
# and then counts the words, ignoring non-word symbols
readCleanCount<-function(pdf){
txt<-pdf_text(pdf)
txt<-paste(gsub(txt,pattern="\r\n",replace=" "),collapse=" ")
@dwbapst
dwbapst / stepAIC forward example.R
Last active February 4, 2018 16:29
There is no good example code online of how to do forward selection with MASS::stepAIC and that's really annoying
#So here's an example of that
x<-rnorm(100)
y<-rnorm(100)
z<-rnorm(100)
xy<-x+2*y+rnorm(100)
# everything needs to be in a dataframe
data<-data.frame(xy,x,y,z)
@dwbapst
dwbapst / myBiPlot.R
Created February 5, 2018 02:42
Creating your own biplot for ordinations (e.g. ecological matrices with environmental data) isn't that hard - hardest part is figuring out how to get zero to line-up
# x is the score for the horizontal axis, xx is the loading for that axis
# y is the score for the vertical axis, yy is the loading for that axis
# and the labels for each can also be set
myBiPlot<-function(x,y,xx,yy
,xlab="",ylab=""
,xxlab="",yylab=""){
oldPar<-par(no.readonly=TRUE)
par(mar=c(5,5,5,5))
#
# for nest migrations - source functions, modify vectors of lists of nesting species, then run
# use kable to produce markdown table for pasting to Silph Road
#approximate ranges for each species in the nesting lists
getRangesForNestingSpecies<-function(nestingSpecies, arbMax=256){
nNesting<-length(nestingSpecies)
intLength<-arbMax/nNesting
lowBound<-((1:nNesting)-1)*intLength
upperBound<-lowBound+intLength
res<-cbind(lowBound,upperBound)
@dwbapst
dwbapst / Forced Nest Migration Script 09-14-18.R
Last active September 14, 2018 13:32
Forced Nest Migration Simulation for New Nesting Species Added with Kanto-2 Event 2018
# for nest migrations - source functions, modify vectors of lists of nesting species, then run
# use kable to produce markdown table for pasting to Silph Road
#approximate ranges for each species in the nesting lists
getRangesForNestingSpecies<-function(nestingSpecies, arbMax=256){
nNesting<-length(nestingSpecies)
intLength<-arbMax/nNesting
lowBound<-((1:nNesting)-1)*intLength
upperBound<-lowBound+intLength
res<-cbind(lowBound,upperBound)
@dwbapst
dwbapst / calculating_league_CPs_stats_01-08-18.R
Created January 8, 2019 22:13
Code for maybe getting best combo of IVs/level for each pokemon, given CP limitations of the three leagues 01-08-19
setwd("D:\\dave\\fun\\Pokemon\\analyses of pokemon stuff\\PvP Analysis")
# CP_multiplier
CP_multiplier<-c(0.094, 0.135137432, 0.16639787, 0.192650919, 0.21573247, 0.236572661,
0.25572005, 0.273530381, 0.29024988, 0.306057377, 0.3210876, 0.335445036, 0.34921268,
0.362457751, 0.37523559, 0.387592406, 0.39956728, 0.411193551, 0.42250001, 0.432926419,
0.44310755, 0.453059958, 0.46279839, 0.472336083, 0.48168495, 0.4908558, 0.49985844,
0.508701765, 0.51739395, 0.525942511, 0.53435433, 0.542635767, 0.55079269, 0.558830576,
0.56675452, 0.574569153, 0.58227891, 0.589887917, 0.59740001, 0.604818814, 0.61215729,
@dwbapst
dwbapst / cutting_early_divergences_from_trees_03-12-19.html
Created March 12, 2019 16:10
cutting trees to avoid plotting a very early divergence on a tree
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
@dwbapst
dwbapst / make_78taxon_phylopic_tree.R
Last active March 15, 2019 21:42
Code for Plotting a Nice 78-Taxon Phylogeny with Stratigraphic Ranges, Upward Orientation, Phylopics, White Foreground Color with Black Background, etc
devtools::install_github("dwbapst/paleotree", ref="dev_refinedphylopicfun")
library(paleotree)
taxaSeventyEight <- c(
"Archaeopteryx", "Pinus", "Procoptodon", "Olenellus", "Eldredgeops",
"Quetzalcoatlus", "Homo", "Tyrannosaurus", "Triceratops", "Giraffa",
"Bolivina", "Cancer", "Dicellograptus", "Dunkleosteus", "Solanum",
"Anomalocaris", "Climacograptus", "Halysites", "Cyrtograptus",
"Procoptodon", "Megacerops", "Moropus", "Dimetrodon", "Lingula",
@dwbapst
dwbapst / Cal3_minMax_problem.R
Created April 22, 2019 18:52
Script from Armin showing that "terminal branches" are being included in dating of trees
library(paleotree)
#Simulate some fossil ranges with simFossilRecord
set.seed(444)
record<-simFossilRecord(p=0.1, q=0.1, nruns=1,
nTotalTaxa=c(30,40), nExtant=0)
taxa<-fossilRecord2fossilTaxa(record)
#simulate a fossil record with imperfect sampling with sampleRanges
rangesCont <- sampleRanges(taxa,r=0.5)
@dwbapst
dwbapst / remove_pika_aipom_05-06-19.R
Created May 7, 2019 14:01
Nest Migration Script 05-06-19
filename <- "remove_pika_aipom"
# remove taxa
removeTaxa <- c("Pikachu", "Aipom")
####################################
###################################