Skip to content

Instantly share code, notes, and snippets.

library(tidytext)
library(ggplot2)
library(dplyr)
diamonds %>%
filter(cut %in% c("Fair", "Good")) %>%
count(price = ifelse(price < 2500, "lo", "hi"), cut, clarity) %>%
mutate(clarity = reorder_within(clarity, n, cut)) %>%
ggplot(aes(clarity, n)) +
geom_col() +
@tmastny
tmastny / open-files.sh
Last active April 22, 2020 22:04
Open files that were modified or are new to current branch
# also works with sublime (subl)
code $(git diff --name-only master)
# relative is also useful depending on your working directory
code $(git diff --relative --name-only master)
@tmastny
tmastny / transpose-summary-row.R
Created April 7, 2020 21:52
Use dplyr 1.0.0 rowwise to add summary rows
library(ggplot2)
library(tidyr)
library(dplyr, warn.conflicts = FALSE)
diamonds_sum <- diamonds %>%
mutate(across(cut, as.character)) %>%
group_by(cut) %>%
summarise(across(price, sum), across(carat, n_distinct), n = n())
diamonds_sum
library(rlang)
"select"(iris, Species)
fn_var <- "select"
expr((!!sym(fn_var))(iris, Species) %>% head) %>%
rlang::eval_tidy()
library(recipes)
library(embed)
library(dplyr)
library(mlbench)
set.seed(124)
data(PimaIndiansDiabetes)
d <- PimaIndiansDiabetes
@tmastny
tmastny / pivot_wider_exp.R
Created July 24, 2019 20:49
pivot_wider stuff
library(tidyr)
library(dplyr)
set.seed(324)
d <- tibble(
org = c(1, 1, 1, 2, 2, 3, 3, 3),
sport = c("fb", "sc", "bb", "fb", "bb", "fb", "sc", "bb"),
v1 = rnorm(8),
v2 = rnorm(8) * 2,
v3 = rnorm(8) * 10
@tmastny
tmastny / spread-pivot.R
Created June 4, 2019 21:02
Spread and pivot over multiple columns
library(tidyverse)
test <- crossing(
teamid = c(1, 2, 3),
action = c("a", "b", "c")
)
test <- test %>%
group_by(action) %>%
mutate(
day30 = c(10, 20, 30),
@tmastny
tmastny / caret-custom-model.R
Created February 14, 2019 21:37
Trying to get custom models for thresholds
library(caret)
library(dplyr)
threshold_method <- function(method) {
thresh_code <- getModelInfo(method, regex = FALSE)[[1]]
thresh_code$type <- c("Classification")
# https://unix.stackexchange.com/a/402398/286677
"\e[A": history-search-backward
"\e[B": history-search-forward
@tmastny
tmastny / meaning-chains.R
Created August 30, 2018 18:53
Meaning chains with word embeddings
# http://sappingattention.blogspot.com/2018/06/meaning-chains-with-word-embeddings.html
cpath = function(matrix, w1, w2, blacklist = c()) {
p1 = matrix[[w1]]
p2 = matrix[[w2]]
base_similarity = cosineSimilarity(p1, p2)
pairsims = matrix %*% t(rbind(p1, p2))
# Words closer to *either* candidate than the other word. These might be used eventually; mostly to save on computation in later steps.
decentsims = pairsims[apply(pairsims, 1, max) >= base_similarity[1],]