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 / 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 / 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 / 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
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]
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"))
@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
@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 / 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 / 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 / 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