Skip to content

Instantly share code, notes, and snippets.

View aammd's full-sized avatar

Andrew MacDonald aammd

  • Université de Sherbrooke
  • Montreal, Canada
View GitHub Profile
@aammd
aammd / filgenus
Created July 17, 2015 17:36
some genera, where repeated genera have been annoyingly abbreviated
BEGIN {FS = ","
genus = "0"}
$1 ~ /[A-Z][a-z]/ {genus = $1}
{
$2 = genus
printf "%s %s\n", $1, $2
}
@aammd
aammd / 5yearslater.R
Created July 20, 2015 15:56
One of the oldest .R scripts of mine I can find, 5y old
source("fn.incidence.R")
insect<-read.csv("insect.community.csv",sep=";")
brom<-read.csv("bromeliads.csv")
insect.pres <- insect
insect.pres[,3:27] <- as.numeric(insect.pres[,3:27]>0)
taxo.insect <- read.csv("Cardoso.insects.csv",sep=";")
insect.common <- commonize()
a.b <- sapply(1:dim(insect.common)[1],log.reg,simplify=FALSE)
a.b <- do.call(cbind,a.b)
@aammd
aammd / gawk_ep
Created July 20, 2015 16:08
separates text into lines, when lines are delimited by speaker's names in all caps, eg "SPEAKERNAME: hurr durr dee ANOTHERSPEAKER: beep boop"
BEGIN {
FS = "\t"
}
## should be possible to catch logs & other extras by finding unsplit lines
{
n = split($2, lines, /[A-Z]'?[A-Z][A-Za-z'\[\] ]*:/, speaker)
if (n == 1) {
printf "%s\t%s\t%s\t%s\n", $1, 1, "extra", $2
@aammd
aammd / slideplot.R
Created September 23, 2015 03:46
plot something so that it is pretty in R for LIbreoffice slides.
png(filename = "Figures/web.R.png", width = 28, height = 21, units = "cm", res = 100)
#plotweb(mat, text.rot = 55, col.high = NULL, col.low = NULL, low.lablength = 7, labsize = 2)
dev.off()
@aammd
aammd / log_assertr.R
Created October 7, 2015 17:05
logging output of assertr
library(loggr)
library(assertr)
library(magrittr)
log_file("thelog.log")
mtcars %>%
insist(within_n_mads(2), mpg:qsec) %>%
something
@aammd
aammd / anscombe.R
Created November 1, 2015 20:45
Anscombe's quartet
library(rvest)
quartet <- html("https://en.wikipedia.org/wiki/Anscombe's_quartet") %>%
html_table() %>%
.[[2]]
for (i in 1:length(names(quartet))) {
if (is.na(names(quartet)[i])) {
names(quartet)[i] <- names(quartet)[i - 1]
}
@aammd
aammd / functions.r
Created November 12, 2015 20:32
A quick rewrite of Shaun Jackman's original `make` lesson, written for Rich Fitzjohn's `remake`
make_plot <- function(mydata){
qplot(Length, Freq, data=mydata)
}
@aammd
aammd / getpubs.R
Created November 23, 2015 19:31
quick idea for getting country publication info
library(rvest)
library(magrittr)
pubs <- read_html("http://www.scimagojr.com/countryrank.php") %>%
html_node(".tabla_datos") %>%
html_table(row.names = FALSE)
library(tidyr)
library(dplyr)
@aammd
aammd / dynamic_forloop.R
Created January 12, 2016 19:18
simple example of modifying variables with a for loop in R
library(gapminder)
names(gapminder) %>% dput
div_mean <- function(x) x/mean(x)
myvarnames <- c("pop", "gdpPercap")
gapminder2 <- gapminder
library(pageviews)
library(dplyr)
library(ggplot2)
bowie <- article_pageviews(article = "David_Bowie", end = "2016011200")
since_dec <- bowie %>%
mutate(timestamp = lubridate::ymd_h(timestamp)) %>%
filter(timestamp > lubridate::ymd("2015-12-01"))