Skip to content

Instantly share code, notes, and snippets.

View gshotwell's full-sized avatar

Gordon Shotwell gshotwell

  • Halifax, Nova Scotia
View GitHub Profile
DF <- data.frame(Pr = runif(100, min = 0, max = 1),
Index = 1:100)
DF$rand<- runif(100, 0, 1)
DF$samp<- 1
DF[ DF$rand > (1- DF$Pr) & DF$rand < DF$Pr, "samp"] <- 2
@gshotwell
gshotwell / dygraphs
Created February 25, 2015 17:35
Seasonal Dygraph Question
library("dygraphs")
library("lubridate")
library("xts")
months <- data.frame(month = 1:12, year_1 = 401:412, year _2 = 501:512)
test$month<- paste("2000-", test$month, "-01")
test$month<- ymd(test$month)
dygraph(xts(test[, 2:3], test$month ))
# Tidy directed network
library(dplyr)
df <- data_frame( Var_1 = c("x", "y", "z", "z"),
Var_2 = c("y", "z", "x", "y"),
Weight= 1:4)
library(dplyr)
library(ggplot2)
library(microbenchmark)
x1 <- bind_rows(lapply(1:10, function(x)iris))
x2 <-data.frame();for(i in 1:10){x2<-rbind(x2,iris)}
all.equal(x1, x2)
f1 <- function(vector){
bind_rows(lapply(vector, function(x)iris))
}
@gshotwell
gshotwell / gist:351275a7df579f5b9be6
Created October 3, 2015 14:42
Idea to generate makefile from R dataframe of file dependencies, also produce dependency diagram.
library(dplyr)
library(DiagrammeR)
files <- list.files(recursive = TRUE)
file_df <- data_frame(files = files,
type = tools::file_ext(files))
file_df <- filter(file_df, type != "")
file_df$shape <- ifelse(file_df$type %in% c("R", "Rmd"), "circle", "square")
dependencies <- data_frame(
@gshotwell
gshotwell / gist:e17adcb97c4ee3747383
Created February 3, 2016 17:14
ggplot dotplot binned along x and stacked along the y.
library(dplyr)
library(ggplot2)
df <- mtcars %>%
group_by(cyl) %>%
mutate(n = 1: n())
ggplot(df, aes(x = cyl, y = n)) + geom_point()
@gshotwell
gshotwell / gist:89c02a7b06870b64b5a6
Created February 12, 2016 15:54
Bind Cols vs. reduce(list, c)
library(purrr)
library(dplyr)
library(microbenchmark)
list <- map(1:1000, function(x) rep("a", 100))
f1 <- function(list){
reduce(list, c)
}
@gshotwell
gshotwell / classify.R
Created December 16, 2016 14:46
I often want to match the classes between two data.frames, for instance when you lose column class information by moving between wide and long formats. Here is a convenience function to do that.
classify <- function(value, function_char){
fun <- get(paste0("as.", function_char))
fun(value)
}
mtcars_char[] <- map(mtcars_char, as.character)
map(mtcars_char, class)
mtcars_char[] <- map2(mtcars_char,
map_chr(mtcars, class),
~classify(.x, .y))
@gshotwell
gshotwell / gist:325632c1ba67e10e7951ecc220bc3da4
Created April 7, 2017 16:37
Women in Technology StackOverflow Sruvey
library(stacksurveyr)
library(dplyr)
library(tidyr)
library(purrr)
library(stringr)
library(ggplot2)
library(forcats)
df <- stack_survey %>%
select(respondent_id, gender, tech_do) %>%
`%d%` <- function(vars, list){
for(i in vars){
assign(i, list[[i]], envir = .GlobalEnv)
}
}
c("mpg", "cyl") %d% mtcars
test_list = list(cars = mtcars,
numbers = 1:500,