Skip to content

Instantly share code, notes, and snippets.

View JosiahParry's full-sized avatar
💻
on the 'puter

Josiah Parry JosiahParry

💻
on the 'puter
View GitHub Profile
@JosiahParry
JosiahParry / focal-mean.R
Last active November 28, 2024 18:44
focal mean using base R is faster than using terra
library(terra)
library(Matrix)
# create a sample raster
r <- rast(ncols=10, nrows=10, ext(0, 10, 0, 10))
x <- 1:ncell(r)
values(r) <- x
# why is there NaN in here????
adj_raw <- adjacent(r, 1:ncell(r), "queen")
cumsum_cut <- function(x, cuts) {
  x[cuts-1] <- 0
  res <- lapply(split(x, cumsum(x == 0)), \(.x) {
    cumsum(.x)
  }) |> 
    unlist() |>
    unname()
  n <- length(res)
 to_fill &lt;- numeric(n)
@JosiahParry
JosiahParry / game-of-life-spdep.R
Created October 16, 2024 15:56
The game of life written in R with spdep
library(spdep)
n <- 25
geo <- sf::st_make_grid(
cellsize = c(1, 1),
offset = c(0, 0),
n = n
)
# create contiguity neighbors for the grid
str_replace_many <- function(string, ...) {
replacements <- rlang::list2(...)
patterns <- rlang::names2(replacements)
if (anyNA(patterns) || any(!nzchar(patterns))) {
cli::cli_abort("All arguments passed to {.arg ...} must be named")
}
for (pattern in replacements) {
rlang:::check_string(pattern, arg = pattern)
}
@JosiahParry
JosiahParry / Cargo.toml
Last active September 30, 2024 23:12
Blackwell's N=2
[package]
name = "my-hands"
version = "0.1.0"
edition = "2021"
[dependencies]
rand = "0.8.5"
rand_distr = "0.4.3"
@JosiahParry
JosiahParry / r67.R
Created September 7, 2024 16:24
Experimentation with R6 and S7 hybrid. Allows for the creation of mutable objects with type-safe properties as well as self-referential methods.
# What do i want from an object oriented R class system?
# opt-in public immutability - neither. Accomplished with private property with active binding in R6
# interior mutability - R6
# type safety - S7
# self-referential methods - R6
# private methods don't have any type safety they can be whatever you want.
# immutables can only be set at creation and class doesn't matter
# Each .public & .private element must be named
@JosiahParry
JosiahParry / gist:ad3d591738b3e284c590f8b35f10bd5a
Created September 7, 2024 14:08
spatial lag categorical development
# remotes::install_github("simonpcouch/forested")
library(dplyr)
library(sfdep)
library(spdep)
trees <- forested::forested |>
sf::st_as_sf(coords = c("lon", "lat"), crs = 4326)
k <- ceiling(nrow(trees)^(1/3))
@JosiahParry
JosiahParry / Cargo.toml
Created September 6, 2024 20:08
testthat-cli R
[package]
name = "testthat-cli"
version = "0.1.0"
edition = "2021"
[dependencies]
argh = "0.1.12"
#' @export
new_schedule_builder <- function() {
builder <- list(
sec = "0",
min = "0",
hour = "0",
dom = "*",
month = "*",
dow = "*",
year = "*"
@JosiahParry
JosiahParry / harris-trump-polls-538.R
Created July 24, 2024 21:08
A short script to read in all 2024 polling data from 534 and make a quick visualization.
library(dplyr)
library(tidyr)
library(ggplot2)
url <- "https://projects.fivethirtyeight.com/polls/president-general/2024/national/polls.json"
# read in the raw json path
polls_raw <- RcppSimdJson::fload(url)
# filter to the IDs that contain Harris