Skip to content

Instantly share code, notes, and snippets.

View trinker's full-sized avatar

Tyler Rinker trinker

View GitHub Profile
# R Function to Draw Edward Tufte-style Slopeplots
# TODO:
## deal with overlapping numeric values: if more than one label falls at a position, offset based on rank() of the subsequent column values for those rows
## overlapping rownames (same algorithm as above, but hard code for 1st and 2nd value)
slopegraph <-
function(
df,
@trinker
trinker / gist:90a60e9ee32a7e825cf5
Last active August 29, 2015 14:08
Analysis of Dason's analysis of Lil Wayne
pstat <- require(pacman)
bostat <- require(BreakoutDetection)
if (!pstat) devtools::install_github("trinker/pacman")
if (!bostat) devtools::install_github("twitter/BreakoutDetection")
p_load(BreakoutDetection, dplyr, XML, qdap)
doc <- htmlParse("http://dasonk.com/2014/07/18/Lil-Jon-Analysis/")
text <- read.transcript(text = head(sapply(getNodeSet(doc, "//p"), xmlValue), -1))
sents <- sent_detect(text[[1]])
@trinker
trinker / gist:c4f7f17a0f2e027b1c08
Last active August 29, 2015 14:09
Reading docx files
A function developed by Bryan Goodrich for reading in .docx files:
```{r}
read_docx <- function (file, skip = 0) {
tmp <- tempfile()
if (!dir.create(tmp))
stop("Temporary directory could not be established.")
unzip(file, exdir = tmp)
xmlfile <- file.path(tmp, "word", "document.xml")
doc <- XML::xmlTreeParse(xmlfile, useInternalNodes = TRUE)
@trinker
trinker / knitr
Last active August 29, 2015 14:15 — forked from pdparker/knitr
# Makefile to use knitr for presentations
SLIDES := Day1Part1-Introduction.html Day1Part1-session2.html Day1Part1-session3.html Day1Part2-session1.Rmd
all: $(SLIDES)
clean:
rm -rf *.md mplus.*
%.html: %.Rmd
function(package, name, qpath, path, ...){
message(sprintf("Attempting to tweak %s...", package))
if (TRUE) message(sprintf("%s has been tweaked!", package))
}
@trinker
trinker / name baby
Created March 19, 2015 01:53
An R script to search through baby names
if (!require("pacman")) install.packages("pacman")
pacman::p_load(babynames, dplyr)
lapply(seq(1, 50000, by=35), function(n){
babynames %>%
filter(year > 1940 & year < 1980) %>%
group_by(name, sex) %>%
summarize(n=sum(n))%>%
ungroup %>%
@trinker
trinker / ggplot2 slopegraph
Created April 13, 2015 18:45
Slopegraph: ggplot2
if (!require("pacman")) install.packages("pacman"); library(pacman)
p_load(dplyr, qdap, qdapRegex, qdapDictionaries, ggplot2, scales)
nms <- unlist(qdapRegex::TC(sample(NAMES[[1]], 26)))
dat <- data_frame(
year = rep(1997:2000, each = length(nms)),
observation = rep(nms, length(1997:2000)),
score = sample(50:100, length(nms)*length(1997:2000), TRUE)
)
L1 <- list(
a = c(2, 15, 23, 19, 3, 2, 3, 27, 20, 11, 27, 10, 19, 10, 13, 10),
b1 = c(22, 9, 5, 10, 5, 1, 24, 2, 10, 9, 7, 3, 12, 24, 10, 9)
)
L2 <- list(
i = c(16, NA, 17, NA, 2, NA, 2, NA, 10, NA, 15, NA, 6, NA, 9, NA),
b2 = c(11, 27, 14, 5, 5, 7, 8, 24, 8, 3, 6, 15, 22, 6, 1, 1)
)
#L3 <- list( #bad wheel
# j = c(3, NA, 8, NA, 10, NA, 14, NA, 11, NA, 8, NA, 12, NA, 11, NA),
@trinker
trinker / gist:4d43800e73c3a678b374
Last active September 3, 2015 13:18
Plot Pies on a Map
load(url("http://dl.dropbox.com/u/61803503/nycounty.RData"))
head(ny); head(key) #view the data set from my drop box
if (!require("pacman")) install.packages("pacman")
p_install_version("ggtree", '1.0.14')
p_load(ggplot2, ggtree, dplyr, tidyr, sp, maps, pipeR, grid, XML, qdapRegex, magrittr)
getLabelPoint <- function(county) {Polygon(county[c('long', 'lat')])@labpt}
df <- map_data('county', 'new york') # NY region county data
@trinker
trinker / topicmodels_json_ldavis.R
Created September 28, 2015 19:39 — forked from christophergandrud/topicmodels_json_ldavis.R
Convert the output of a topicmodels Latent Dirichlet Allocation model to JSON for use with LDAvis
#' Convert the output of a topicmodels Latent Dirichlet Allocation to JSON
#' for use with LDAvis
#'
#' @param fitted Output from a topicmodels \code{LDA} model.
#' @param corpus Corpus object used to create the document term
#' matrix for the \code{LDA} model. This should have been create with
#' the tm package's \code{Corpus} function.
#' @param doc_term The document term matrix used in the \code{LDA}
#' model. This should have been created with the tm package's
#' \code{DocumentTermMatrix} function.