Skip to content

Instantly share code, notes, and snippets.

View dgrtwo's full-sized avatar

David Robinson dgrtwo

View GitHub Profile
library(tidyverse)
library(lubridate)
questions <- readr::read_csv("~/Repositories/stacklite/questions.csv.gz")
question_tags <- readr::read_csv("~/Repositories/stacklite/question_tags.csv.gz")
r_questions <- question_tags %>%
filter(Tag == "r")
r_questions_by_month <- questions %>%
@dgrtwo
dgrtwo / separate_steps.R
Last active September 29, 2016 09:58
separate_steps.R
#' Convert a dplyr expression to a list of step objects
separate_steps <- function(expr, iscall=FALSE) {
if (iscall) {
call <- expr
} else {
call <- match.call()[["expr"]]
}
len <- length(call)
if (len == 1) {
@dgrtwo
dgrtwo / grouply.R
Last active August 21, 2016 16:09
grouply <- function(f, ...) {
groups <- lazyeval::lazy_dots(...)
function(tbl, ...) {
dplyr::group_by_(tbl, .dots = groups) %>%
f(...) %>%
dplyr::ungroup()
}
}
@dgrtwo
dgrtwo / add-tally.R
Last active August 22, 2016 02:13
add_tally and add_count
#' Add a column counting or tallying observations within groups
#'
#' \code{add_tally} adds a column named "n" (or similar) to a table based on the number
#' of items within each group. These functions are to \code{tally} and
#' \code{count} as \code{mutate} is to \code{summarise}: they add an additional
#' column. They tally within the groups of the current data, and do not change them.
#'
#' @param x A table
#' @param wt (Optional) If omitted, will count the number of rows. Otherwise, use a weighted tally
#' @param sort Whether to sort the result in descending order of n
# data cleaning based on http://blog.dominodatalab.com/r-in-ecology/
library(dplyr)
library(tidyr)
library(readr)
library(stringr)
library(auriel)
infolder <- "night_files/"
masterdat2 <- data_frame(file = list.files(infolder)) %>%
group_by(file) %>%
@dgrtwo
dgrtwo / keybase.md
Last active February 8, 2018 15:57

Keybase proof

I hereby claim:

  • I am dgrtwo on github.
  • I am drobinson (https://keybase.io/drobinson) on keybase.
  • I have a public key whose fingerprint is 2BFD FAC9 CDA9 669F FE2C 6973 C0B7 A65C CA30 6675

To claim this, I am signing this object:

@dgrtwo
dgrtwo / theme-blank.R
Created February 5, 2016 06:10
theme_blank() for ggplot2
theme_blank <- function(...) {
ret <- theme_bw(...)
ret$line <- element_blank()
ret$rect <- element_blank()
ret$strip.text <- element_blank()
ret$axis.text <- element_blank()
ret$plot.title <- element_blank()
ret$axis.title <- element_blank()
ret$plot.margin <- structure(c(0, 0, -1, -1), unit = "lines", valid.unit = 3L, class = "unit")
ret
library(readr)
library(dplyr)
start <- read_delim("col3 col2 col4 col1
h a t t
i v i g
k s g n
n g n i", delim = " ")
start %>%
@dgrtwo
dgrtwo / party_df_tidiers.R
Created November 10, 2015 23:55
Tidy distributed models on a partitioned data frame from dplyr
## helper and setup functions
wrap_party_df <- function(func) {
function(x, object, ...) {
n <- col_name(substitute(object))
# have to create an expression since we cannot rely on local
# variables
args <- list(...)
expr <- substitute(do.call(func, c(list(.[[colname]][[1]]), args)),
# somewhat hackish solution to:
# https://twitter.com/EamonCaddigan/status/646759751242620928
# based mostly on copy/pasting from ggplot2 geom_violin source:
# https://github.com/hadley/ggplot2/blob/master/R/geom-violin.r
library(ggplot2)
library(dplyr)
"%||%" <- function(a, b) {