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 / 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 / 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 / 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 / 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 / subsetmatrix.R
Created May 7, 2015 00:44
A trio of functions to grab square subsets of a square matrix
## create the logical matrices for subsetting
## x matrix to subset
## n number of rows or columns in each subset (by subset I mean a small square from the larger matrix)
select_matrix <- function(x, n){
mod <- ncol(x) %% n
if(mod != 0) stop("not a divisor")
if(ncol(x) != nrow(x)) stop("not a square")
print_num <- function(num){
print(num)
}
print_num_x <- function(num = x){
print(num)
}
@aammd
aammd / swc_blog.R
Created March 13, 2015 18:37
Automatically create a file with the correct header and filename for SWC teacher instruction.
require("yaml")
require("lubridate")
#' Make a correct SWC blog post
#'
#' Automatically formats the necessary YAML header for a
#' Software Carpentry Teacher training blog post. Writes
#' this header to a file with the correct name. The file is
#' created in your current working directory.
#'
@aammd
aammd / fill_down.R
Last active August 29, 2015 14:16
An R function I wrote in the process of translating a list kept in a .docx file to a proper R dataframe. After moving it from .docx to .txt via pandoc, I needed to turn section headers into levels of a grouping factor
#' convert positional information to two columns
#'
#' Sometimes text is organized by position. This function
#' turns positional group labels (e.g headers ) into the levels of a grouping variable
#' @param x character vector containing group labels followed by group members
#' @param pattern regular expression that identifies the group labels
fill_down <- function(x, pattern){
## find matches of the pattern
x <- as.character(x)
value_matches <- grepl(pattern = pattern, x = x)
@aammd
aammd / errors.R
Created January 31, 2015 00:26
my sad sad adventures trying to install RTidyHTML
Installing package into ‘/home/andrew/R/x86_64-pc-linux-gnu-library/3.1’
(as ‘lib’ is unspecified)
* installing *source* package ‘RTidyHTML’ ...
cc -Iinclude -fPIC -c -o access.o access.c
cc -Iinclude -fPIC -c -o alloc.o alloc.c
cc -Iinclude -fPIC -c -o attrask.o attrask.c
cc -Iinclude -fPIC -c -o attrdict.o attrdict.c
cc -Iinclude -fPIC -c -o attrget.o attrget.c
cc -Iinclude -fPIC -c -o attrs.o attrs.c
cc -Iinclude -fPIC -c -o buffio.o buffio.c
@aammd
aammd / make-graph
Created December 16, 2014 20:25
Graphing a makefile: make2graph and circo
make -Bnd | make2graph -s | circo -Tpng -o out_circ.png