Skip to content

Instantly share code, notes, and snippets.

View trinker's full-sized avatar

Tyler Rinker trinker

View GitHub Profile
@trinker
trinker / datasci_install.R
Last active April 20, 2019 11:47
Install datasci packages
#' @param packages An optional vector of Campus Labs packages to install.
#' @param pattern An optional grep pattern of campus labs packages to install.
install_cl <- function(packages = NULL, pattern = '.', ...){
try_install_cran <- function(package){
if (!require(package, character.only = TRUE, quietly = TRUE)) {
message(sprintf('The "%s" package is missing; do you want me to install it?', package))
ans <- menu(c("Yes", "No"))
if (ans == "2") {
@trinker
trinker / topicmodeling_bit.R
Created February 13, 2019 17:21
Bit Topic Modeling
if (!require("pacman")) install.packages("pacman")
pacman::p_load(udpipe, BTM)
data("brussels_reviews_anno", package = "udpipe")
## Get and load the udpipe model
engmod <- udpipe_download_model(language = "english", udpipe_model_repo = "bnosac/udpipe.models.ud")
ud_engmod <- udpipe_load_model(engmod$file_model)
## Annotate the text data and merge back together
nr <- nrow(sentimentr::presidential_debates_2012)
@trinker
trinker / EulerPlot.R
Created February 13, 2019 15:07
Euler Plot Demo
pacman::p_load(venneuler, tidyverse, ggforce)
## Make some fake data
set.seed(10)
dat <- data.frame(
Person = paste0('Person_', 1:10),
setNames(as.data.frame(matrix(rbinom(50, size = 1, prob=c(1/(1:5))), ncol = 5)), paste0('Attribute_', 1:5)),
stringsAsFactors = FALSE
)
@trinker
trinker / secret_message.R
Created February 3, 2019 22:22
Secret Message
library(tidyverse)
map <- data_frame(
ins = c(LETTERS, letters),
outs = c(rev(LETTERS), rev(letters))
)
map
@trinker
trinker / scaling.R
Created February 1, 2019 12:21
Class demo scaling techniques
min_max <- function(x, ...) {
m <- min(x, na.rm = TRUE)
(x - m)/(max(x, na.rm = TRUE) - m)
}
standardization <- function(x, ...) {
if (length(stats::na.omit(unique(x))) > 1) {
scale(x)
} else {
@trinker
trinker / install_github.R
Last active January 25, 2019 20:59
install_github
## Taken from: http://news.mrdwab.com/install_github.R
## source("https://gist.githubusercontent.com/trinker/8a385892fac7d14a1f861f61394a2621/raw/95bdc672c632eff8dcfcd1f19233a0ed3cf85045/install_github.R")
## install_github("trinker/sentimentr")
#' install_github installs R packages directly from GitHub
#'
#' install_github is a very lightweight function with no dependencies
#' that installs R packages from GitHub.
#'
@trinker
trinker / fuzzy_map.R
Created January 10, 2019 13:54
Fuzzy Mapping Between Multi-Word Strings. GCreates a map between 2 vectors with strings that are close but not identically named entities.
fuzzy_map <- function(x, y, distance = 'cosine', cutoff = .40,
remove = c('the', 'a', 'an', 'at', 'of', 'in', '-', ' - CL', ' - OS'),
substitution = data.frame(
pattern = c('univ\\.', '\\bst\\.', '\\bmt\\.', '&'),
replacement = c('university', 'saint', 'mount', ' and '),
stringsAsFactors = FALSE
),
ngrams = 1, # choose 1 or 2 (2 is 2 words)
downweight.words = c('suny', 'cuny', 'college', 'university', 'state'),
# words that are less important - make smaller to make the words LESS important
# Portraits
tell_it_like_it_is <- function(){
message <- paste0(
sprintf(cow, sample(mess, 1)),
"\n\n\n\n"
)
cat(message)
}
cow <- "\n ---------------- \n%s! \n ----------------- \n \\ ^__^ \n \\ (oo)\\ ________ \n (__)\\ )\\ /\\ \n ||------w|\n || ||"
@trinker
trinker / guttenberg.R
Last active October 12, 2018 11:44
R Guttenberg API
## Originally taken from: https://juliasilge.github.io/ibm-ai-day/slides.html#1
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, gutenbergr)
## Scrape known books
titles <- c(
"Twenty Thousand Leagues under the Sea",
"The War of the Worlds",
"Pride and Prejudice",