Skip to content

Instantly share code, notes, and snippets.

View HorridTom's full-sized avatar

HorridTom HorridTom

  • London
View GitHub Profile
@HorridTom
HorridTom / autospc_example.R
Created April 22, 2023 17:19
Example of modifying x-axis labels on autospc plot
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))
@HorridTom
HorridTom / perturb_start_end_los
Created February 1, 2022 23:24
Perturb start and end times keeping duration within same "bins"
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),
@HorridTom
HorridTom / multiple_summaries.R
Created September 17, 2021 14:50
Examples of multiple summaries
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))
@HorridTom
HorridTom / user-tidy-eval.R
Last active September 16, 2021 11:17
Tidy evaluation with user-specified column names
# 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) {
# 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)
@HorridTom
HorridTom / gist:f17f171d8e14472e2bcf2d42a93f19ee
Created June 22, 2018 12:41
Hash columns of a dataframe
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)
}