Skip to content

Instantly share code, notes, and snippets.

@statzhero
statzhero / reassign-assign-operator.R
Created December 19, 2023 19:38
Reassign `<-` so that it prints the result
# 🎵 Welcome to the 🔥 Danger Zone 🎶
`<-` <- function(lhs, rhs) {
assign(deparse(substitute(lhs)), rhs, envir = parent.frame())
print(rhs)
}
@statzhero
statzhero / aoc2023-day10.r
Created December 12, 2023 15:22
Advent of Code day 10
library(tidyverse)
d <- read_table("./inst/input10.txt", col_names = FALSE) |>
separate_longer_position(X1, 1) |>
mutate(dist = if_else(X1 == "S", 0, NA)) |>
mutate(x = if_else(X1 == "S", "L", X1)) # eyeballing
NCOL <- sqrt(nrow(d))
top <- c("7", "|", "F")
@statzhero
statzhero / aoc2023-day03.r
Last active December 4, 2023 15:47
Advent of Code day 3 solution
library(tidyverse)
n_hood <- c(141,140,139,1,-1,-139,-140,-141)
get_nhood <- function(x, lags = n_hood, fill = "x"){
collapse::flag(x, lags, fill = fill) |>
as_tibble() |>
unite("concat", sep = "") |>
pull(concat)
}
loc <- function(x) { log(m^2 / sqrt(s^2 + m^2)) }
shape <- function(x) { sqrt(log(1 + (s^2 / m^2))) }
# Simple charts
library(magrittr)
d <- rnorm(1000, 1, 1)
density(d) %>% plot
d2 <- rlnorm(1000, 1, 1)
density(d2) %>% plot
@statzhero
statzhero / list_named_ranges.vba
Created December 11, 2018 20:53
Excel VBA list named ranges in sheet
Sub Test_NamedRanges()
For Each nm In ThisWorkbook.Names
If nm.RefersToRange.Parent.Name = Sheet10.Name Then MsgBox nm.Name
Next nm
End Sub
table()
prop.table(mytable) # cell percentages
sort(table(x), decreasing = TRUE)
# Show missing data in table as default
table = function (..., useNA = 'ifany') base::table(..., useNA = useNA)
## SETUP AND FUNCTIONS -------------------------------------------------
load_packages <- function(x) {
# Install CRAN packages (if not already installed)
.inst <- x %in% installed.packages()
if(length(x[!.inst]) > 0) install.packages(x[!.inst])
# Load packages into session
sapply(x, require, character.only = TRUE)
}
no_missing <- function(x) sum(is.na(x))
@statzhero
statzhero / csv-ssl
Created February 7, 2014 14:50
An R function for downloading CSV-files over SSL
read.url <- function(url, ...){
temporaryFile <- tempfile()
download.file(url, destfile = temporaryFile, method = "curl")
url.data <- read.csv(temporaryFile, ...)
return(url.data)
}