Skip to content

Instantly share code, notes, and snippets.

@dmi3kno
dmi3kno / col_proxy.R
Created October 16, 2017 22:48
Implementing column name proxy for custom tibble class
library(dplyr)
library(rlang)
library(purrr)
x <- tibble(year=2011:2015,
value=runif(5))
# extending the tibble class
class(x) <- unique(c("tbl_cf", class(x)))
attr(x, ".index") <- "year"
@dmi3kno
dmi3kno / snowflake.R
Created December 6, 2017 14:51
Animate ggplot snowflakes
library(tidyverse)
library(tweenr)
library(magick)
m <- 2e2
min_s <- 4
k <- 2e2/(min_s)
img <- image_graph(600, 600, res = 96)
plot_snowflakes <- function(data){
# Data collected from the following link:
# https://us.teamblind.com/article/Stop-asking-for-tech-company-offer-numbers-Read-this-and-thank-me-later-sN8uYpop
comp <- data.frame(stringsAsFactors=FALSE,
company = c("Google", "Google", "Google", "Google", "Google", "Google",
"Google", "Google", "Google", "Google", "Google", "Google",
"Google", "Google", "Google", "Google", "Google", "Google",
"Google", "Facebook", "Facebook", "Facebook", "Facebook",
"Facebook", "Facebook", "Facebook", "Facebook", "Facebook",
"Facebook", "Facebook", "Facebook", "Facebook", "Facebook",
set.seed(1)
tibble::tibble(year = seq(1900,1999) *
sample(c(1, NA), 100, replace = TRUE),
value=runif(100),
gap_id=inverse.rle(structure(list(lengths=rle(is.na(year))$lengths,
values=cumsum(rle(is.na(year))$values)*
rle(is.na(year))$values), class="rle")))
#> # A tibble: 100 x 3
#> year value gap_id
#> <dbl> <dbl> <int>
---
title: "Presentation Ninja"
subtitle: "⚔<br/>with xaringan"
author: "Yihui Xie"
date: "2016/12/12 (updated: `r Sys.Date()`)"
output:
xaringan::moon_reader:
lib_dir: libs
css:
- default
@dmi3kno
dmi3kno / topogeo.R
Last active December 7, 2018 20:59
# create geometry matrix
geom <- cbind(x=c(4,4,2,5,2,3,3,3,4,2),
y=c(6,2,3,4,5,2,4,7,8,0))
# create data frame with special topology list column
df <- tibble::tibble(name="John", age=32,
topo=list( # list column containing polygon(s) and inner/outer flag
list(poly = list(c(10, 5, 8, 9, 4, 2),
c(3, 6, 1, 7)),
type = c(1L, 0L)))
@dmi3kno
dmi3kno / lundaloppet_plot.R
Created April 18, 2019 19:09
plotting running time distribution from
library(readxl)
library(tidyverse)
library(hms)
library(hrbrthemes)
library(extrafont)
read_excel("lundaloppet2018.xlsx", col_names = F, col_types = "text") %>%
janitor::clean_names() %>%
mutate(x2=parse_number(x2),
x5n = parse_number(x5),
set.seed(42) # The answer to life, the universe and everything
## This is what the expert said
p10 <- 8
p50 <- 12
p90 <- 25
# I can approximate it with Myerson distribution defined by quantiles
prior_predictive_elicited <- tidyear::rMyerson(1e5, p10, p50, p90, tl=0.2)
compute_hpdi_TJ <- function(xs, prob = .9) {
x_sorted <- sort(xs)
n <- length(xs)
num_to_keep <- ceiling(prob * n)
num_to_drop <- n - num_to_keep
possible_starts <- seq(1, num_to_drop + 1, by = 1)
# Just count down from the other end
possible_ends <- rev(seq(from = n, length = num_to_drop + 1, by = -1))
@dmi3kno
dmi3kno / babynames.R
Last active June 23, 2019 23:08
Ukrainian baby names in Lviv Region
library(tidyverse)
library(rvest)
robotstxt::paths_allowed("http://ukrcensus.gov.ua")
urlencode_utf_win1251 <- function(x){
d <- "0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11, 12: 12, 13: 13, 14: 14, 15: 15, 16: 16, 17: 17, 18: 18, 19: 19, 20: 20, 21: 21, 22: 22, 23: 23, 24: 24, 25: 25, 26: 26, 27: 27, 28: 28, 29: 29, 30: 30, 31: 31, 32: 32, 33: 33, 34: 34, 35: 35, 36: 36, 37: 37, 38: 38, 39: 39, 40: 40, 41: 41, 42: 42, 43: 43, 44: 44, 45: 45, 46: 46, 47: 47, 48: 48, 49: 49, 50: 50, 51: 51, 52: 52, 53: 53, 54: 54, 55: 55, 56: 56, 57: 57, 58: 58, 59: 59, 60: 60, 61: 61, 62: 62, 63: 63, 64: 64, 65: 65, 66: 66, 67: 67, 68: 68, 69: 69, 70: 70, 71: 71, 72: 72, 73: 73, 74: 74, 75: 75, 76: 76, 77: 77, 78: 78, 79: 79, 80: 80, 81: 81, 82: 82, 83: 83, 84: 84, 85: 85, 86: 86, 87: 87, 88: 88, 89: 89, 90: 90, 91: 91, 92: 92, 93: 93, 94: 94, 95: 95, 96: 96, 97: 97, 98: 98, 99: 99, 100: 100, 101: 101, 102: 102, 103: 103, 104: 104, 105: 105, 106: 106, 107: 107, 108: 108, 109: 109, 110: 110,