This file contains 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(digest) | |
library(stringr) | |
library(readr) | |
hashed_id <- function(x, salt){ | |
y <- paste(x, salt) | |
y <- sapply(y, function(X) digest(X, algo = "sha1")) | |
as.character(y) | |
} |
This file contains 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
# Advent of Code Day 3 Puzzle 1 | |
# Team Reindeer! (Tom and Mable) | |
# | |
# Read the puzzle first: https://adventofcode.com/2018/day/3 | |
# Then to understand how this solution works, it is best | |
# to start with the last function in this file and work up! | |
# The example_claim_vector is the same example as from the | |
# puzzle website. | |
library(stringr) |
This file contains 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
# Passing user-specified column names to tidyverse functions inside your own functions | |
# Based on: https://dplyr.tidyverse.org/articles/programming.html | |
library(tidyverse) | |
str(mtcars) | |
# How you might think you write a function that takes a column name as argument | |
# and uses tidyverse functions to do something based on that column name... | |
my_first_group_summarise_fn <- function(df, col_name) { | |
This file contains 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) | |
# Examples of multiple summaries... | |
# Based on examples here: https://dplyr.tidyverse.org/reference/across.html | |
# one summary across multiple columns | |
mtcars %>% | |
group_by(cyl) %>% | |
summarise(across(where(is.numeric), mean)) |
This file contains 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
perturb_start_end_los <- function( | |
df, | |
max_shift_size_hours = 24, | |
min_binsize_mins = 15 | |
){ | |
df <- df %>% | |
dplyr::mutate(los = spell_end - spell_start, | |
binstart = 15*floor(as.numeric(los, units = "mins")/15), | |
binend = 15*(floor(as.numeric(los, units = "mins")/15) + 1), |
This file contains 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(lubridate) | |
library(autospc) | |
set.seed(20230422L) | |
df <- tibble(x = seq(from = as.Date("2023-01-01"), | |
to = as.Date("2023-01-31"), | |
by = 1L), | |
y = rpois(31L, lambda = 20L)) |