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 / 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 / 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 / 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)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
values <- c(2,5,3,6,7,
9,5,4,9,9,
1,5,4,8,1,
3,1,5,6,2,
2,9,4,7,4)
my.mat <- matrix(values, nrow = 5, byrow = TRUE)
library(dplyr)
library(tidyr)
## Empty list -- just use the empty environment for this.
nil <- function() {
emptyenv()
}
## Test if a list is the empty list:
is_empty <- function(lis) {
identical(lis, nil())
}
## via base ####
"%gcd%" <- function(u, v) {ifelse(u %% v != 0, v %gcd% (u%%v), v)}
n <- 20L
js <- 0:n
Jp1 <- js + 1L
Ns <- matrix(NA,nrow = n + 1, ncol = n + 1)
Ds <- Ns
Ns[1,] <- 1L

On Rosetta Code, the list of Tasks uncompleted in R contains the following challenge: print the Bernoulli numbers from B0 to B60. After seeing some solutions, including one in in Python , I wanted to try in R. There are examples of code on that website, and a nice description of the algorithm here.

 library(MASS)
@aammd
aammd / boot_reg.md
Last active August 31, 2017 03:12
bootstrapping regressions with `dplyr`.

Bootstrap confidence intervals for linear and nonlinear models

Another version of this gist with figures included is on Rpubs

Recently I was trying to put confidence intervals on a regression line, and I got some excellent advice from @davidjayharris on Twitter, who suggested the below method in an excellent

@aammd
aammd / boot.R
Created June 12, 2014 21:08 — forked from davharris/boot.R
set.seed(1)
# Create fake data
x = runif(100, 0, 5)
y = .25 * x^3 - x^2 + rnorm(length(x))
data = data.frame(x = x, y = y)
# Identify 500 points to include in the plots
x.sequence = seq(0, 5, length = 500)