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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@aammd
aammd / karaoke.R
Created October 10, 2014 18:57
Comparing a list of dates to a list of intervals
library(dplyr)
library(lubridate)
## when will the two friends be in the same town?
visits <- data.frame(am_in_town = c(
ymd(20140501) %--% ymd(20140520),
ymd(20140615) %--% ymd(20140620),
ymd(20140701) %--% ymd(20140710),
ymd(20141101) %--% ymd(20141112)
@aammd
aammd / character_data.R
Last active August 29, 2015 14:08
This function, based on `rvest`, extracts the Gender, Species, Affilation and Rank for a Star Trek character from the [Star Trek Wiki](http://en.memory-alpha.org/wiki/Portal:Main)
library(rvest)
library(magrittr)
library(tidyr)
library(dplyr)
library(stringr)
character_data <- function(chname){
paste0("http://en.memory-alpha.org/wiki/", chname) %>%
html %>%
html_nodes(".wiki-sidebar") %>%
@aammd
aammd / demolition.R
Created December 3, 2014 21:09
This demonstrates web-scraping in order to generate a list of links on a site, in order to download them all. The example used is the Municipality of Burnaby's list of demolition permits
library("rvest")
library("XML")
links_list <- html("http://www.burnaby.ca/City-Services/Building/Permits-Issued.html") %>%
html_nodes("#ctl15_nestedList a")
## Where do links lead? This is encoded in the attribute "href". First get all the attributes
links_attr <- sapply(links_list, xmlAttrs)
## We have the attributes! each link just became a single named character
@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
@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 / 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 / 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.
#'
print_num <- function(num){
print(num)
}
print_num_x <- function(num = x){
print(num)
}
@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")