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
library(rvest)
st_tab <- read_html("https://en.wikipedia.org/wiki/List_of_Star_Trek_characters") %>%
html_nodes(".wikitable td , .wikitable th") %>%
html_text()
library(stringr)
titles <- st_tab %>%
str_detect(regex("^\\n.*\\n$", perl = TRUE))
st_tab[titles]
@aammd
aammd / doesithaveadecimal.R
Created March 29, 2016 17:28
But does it have a decimal?
has_decimal <- function(x){ (x - floor(x)) > 0 }
## from ?is.integer
is.wholenumber <-
function(x, tol = .Machine$double.eps^0.5) abs(x - round(x)) < tol
## from http://stackoverflow.com/questions/3476782/how-to-check-if-the-number-is-integer/3477158#3477158
## from SO and also twitter https://twitter.com/polesasunder/status/700091075269005313
is_whole <- function(x) x%%1==0
@aammd
aammd / filter_rarities.R
Created April 4, 2016 04:38
how to filter out rarities in R
library(dplyr)
d <- data_frame(A =c(rep("a", 5), rep("c",2)))
d
## one way to do it
d %>%
group_by(A) %>%
filter(length(A) > 2)
@aammd
aammd / nudge.R
Created April 13, 2016 22:56
nudging that stat_summary()
library(dplyr)
library(tidyr)
library(ggplot2)
df <- data_frame(x = rnorm(n = 20, mean = 4),
y = rnorm(n = 20, mean = 7)) %>%
gather(XY, numbers)
ggplot(df, aes(x = XY, y = numbers)) +
geom_point(position = position_jitter(width = 0.05)) +
@aammd
aammd / new_column_purrr.R
Created June 8, 2016 15:25
experimenting with purr on gapminder
library(purrr)
library(gapminder)
gapminder %>%
transpose %>%
map(~ splice(., totalgdp = .$pop * .$gdpPercap)) %>%
transpose %>%
simplify_all() %>%
as.data.frame() %>%
head
@aammd
aammd / new_column_purrr.R
Created June 8, 2016 15:25
experimenting with purr on gapminder
library(purrr)
library(gapminder)
gapminder %>%
transpose %>%
map(~ splice(., totalgdp = .$pop * .$gdpPercap)) %>%
transpose %>%
simplify_all() %>%
as.data.frame() %>%
head
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@aammd
aammd / annonate_rect.R
Created November 1, 2016 14:41
annotating with rectangles
library(dplyr)
library(ggplot2)
dat <- data_frame(xs = runif(10, 2, 15),
ys = runif(10, 2, 15),
xe = rnorm(10, 2, 0.4),
ye = rnorm(10, 2, 0.4))
dat %>%