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(map)
library(rucrdtw)
map(list(first = 33, second = 19, third = 7), ~ ucred_mv(synthetic_control[-.x,], synthetic_control[.x,], byrow= TRUE)) %>% map_dbl("distance") %>% tibble::enframe(.)
# consider also adding this to that shiny app
library(dplyr)
library(tidyr)
words_some_missing <- data_frame(one_word = c(NA, "M", NA),
second_word = c("F", NA, NA))
words_some_missing %>%
unite(sex, one_word, second_word)
## that sticks the NAs together, which I think is something you dont want? In
@aammd
aammd / is_continuous_categories.R
Created December 5, 2016 14:16
are a vector of range edges, separated by _, continuous?
## this function will print "TRUE" if the categories defined by the vector are
## continuous, e.g. c("1500_20000","20000_NA","0_1500" ) `
is_continuous_categories <- function(cat_vector){
cat_range <- cat_vector %>%
str_split("_") %>%
transpose %>%
map(unlist) %>%
{c(invoke(setdiff, .), invoke(setdiff, rev(.)))}
identical(cat_range, c("0", "NA"))
@aammd
aammd / matrix_to_data.frame.R
Last active January 4, 2017 01:31
cantrip to turn a matrix into a data.frame, assuming that the first row of the matrix contains a header row
matrix_to_df_firstline_header <- function(mat){
requireNamespace("purrr")
mat %>%
## cut columns into lists
apply(2, function(s) list(s)) %>%
flatten %>%
map(flatten_chr) %>%
## set names to the first element of the list
{set_names(x = map(., ~ .x[-1]),
@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 %>%
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@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