A gist to collect other gists.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(purrr) | |
# Other ideas: https://stackoverflow.com/questions/21335947/compound-interest-calculation-on-changing-balance-for-data-table | |
compound_interest <- function(principle, interest, periods = NULL, haircut = NULL) { | |
if (is.null(periods)) { | |
nseq <- length(interest) | |
} else { | |
nseq <- periods | |
interest <- rep(interest, nseq) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(colorspace) | |
lighten_by <- function (x, ..., group_colors = c("red", "blue"), adjust = 0.9) { | |
color_groups <- enquos(...) | |
split_x <- x %>% | |
arrange(!!color_groups[[1]]) %>% | |
split(x[, quo_name(color_groups[[1]])]) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# From ggpoindensity | |
count_neighbors_r <- function(x, y, r2, xy) { | |
yx <- 1 / xy | |
sapply(1:length(x), function(i) { | |
sum((yx * (x[i] - x) ^ 2) + (xy * (y[i] - y) ^ 2) < r2) | |
}) | |
} | |
# Others: | |
# * dbscan::pointdensity |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(broom) # alt to par(mfrow(2,2)); plot(mod) | |
library(rsample) | |
library(dplyr) | |
library(ggplot2) # alt to par(mfrow(2,2)); plot(mod) | |
set.seed(1492) | |
ifrac <- initial_split(iris) | |
iris_test <- testing(ifrac) | |
iris_trian <- training(ifrac) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# From ?image and expanded upon | |
pat_circles <- function (n = 27) { | |
x <- seq(-4*pi, 4*pi, length.out = n) | |
sqrt(outer(x^2, x^2, "+")) | |
} | |
eq1 <- function (x) { | |
cos(x^2) * exp(-x/6) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(pals) | |
library(purrr) # in this order | |
library(ggplot2) | |
library(colorspace) | |
library(magrittr) | |
# Invert color | |
invert_color <- function (pal, n = 256) { | |
rgb_mat <- colorspace::hex2RGB(pal(n)) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
`[.hmmm` <- function (x, i, ...) { | |
isub <- substitute(i) | |
is_seq <- identical(as.list(isub)[[1L]], as.name(":")) | |
if (is_seq) { | |
end_seq <- as.list(isub)[[3L]] | |
is_end <- identical(end_seq, as.name("end")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Taken from: https://twitter.com/dirk_sch/status/1151080975952228352 | |
"[[.ll" <- function(x, i) { | |
rlang::eval_tidy(x$call_list[[i]]) | |
} | |
lazylist <- function(...) { | |
calls <- rlang::enexprs(...) | |
structure(list(call_list = calls), class = "ll") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(rlang) | |
# Underneath the hood of a quosure ------------------------------------ | |
v <- "a variable" | |
vquo <- quo(v) | |
print(vquo) | |
#> <quosure> | |
#> expr: ^v |