Skip to content

Instantly share code, notes, and snippets.

View TimTaylor's full-sized avatar

Tim Taylor TimTaylor

View GitHub Profile
rectangles <- function(m, rowfirst = TRUE) {
if (rowfirst)
m <- t(m)
n <- length(m)
nrow <- nrow(m)
ncol <- ncol(m)
rowindex <- seq_len(nrow)
colindex <- seq_len(ncol)
h <- hashtab()
@TimTaylor
TimTaylor / rectangles.R
Created October 19, 2023 22:30
strict rectangles
library(data.table)
shapes <- function(m) {
n <- length(m)
nrow <- nrow(m)
index <- seq_len(n)
rows <- ((index - 1L) %% nrow) + 1L
cols <- ((index - 1L) %/% nrow) + 1L
@TimTaylor
TimTaylor / funz.R
Last active August 9, 2023 11:32
dput_raw ish type thing
funz <- function(x) {
if (!is.character(x) || length(x) != 1L || is.na(x))
stop("`x` must be a scalar character that's not NA.")
middle <- character()
go <- TRUE
i <- 1L
while(go) {
exploded <- c(")", middle, "\"")
delim <- paste(exploded, collapse = "")
if (grepl(delim, x, fixed = TRUE)) {
@TimTaylor
TimTaylor / data.table-joins.R
Created September 22, 2022 13:42 — forked from nacnudus/data.table-joins.R
How to do joins with data.table
library(data.table)
?`[.data.table`
DT <- data.table(x=rep(c("b","a","c"),each=3), y=c(1,3,6), v=1:9)
X <- data.table(x=c("c","b"), v=8:7, foo=c(4,2))
colnames(DT)
# [1] "x" "y" "v"
@TimTaylor
TimTaylor / bootstrap.R
Last active September 14, 2022 21:06
pop categories to ages - bootstrap approach
url <- "https://raw.githubusercontent.com/TimTaylor/census_pop_2021/main/output/census-2021-england-and-wales-total-population.csv"
(dat <- read.csv(url))
# add start and end columns
start <- sub("\\[([0-9]+), .+)", "\\1", dat$age_category)
end <- sub(".+, (.+))", "\\1", dat$age_category)
dat$start <- as.double(start)
dat$end <- as.double(end)
# function to bootstrap with a single category
@TimTaylor
TimTaylor / planning.R
Last active May 23, 2022 11:22
nerd-sniped on slack
# Note the following is not 100% foolproof as it assumes the table entries are all on one line.
# This will need refining to deal with when this is not the case but may be sufficient for the
# question.
# h/t to https://github.com/hrbrmstr/fish-stocking-pdf-data-wrangling/blob/main/main.R
# who's code I'd seen just the other day doing similarish type stuff
library(rvest)
library(pdftools)
@TimTaylor
TimTaylor / sir_script_refactored.R
Created March 5, 2021 13:44
How to test - SIR script example refactored
# parameters --------------------------------------------------------------
N <- 1000 # network size
avk <- 7 # desired average degree
gamma <- 0.25 # recovery rate
tau <- 0.27 # per-edge infection rate
max_time <- 50 # maximum simulation length
i0 <- 10 # initial number of infected individuals
# -------------------------------------------------------------------------
@TimTaylor
TimTaylor / sir_script.R
Last active March 5, 2021 13:43
How to test - SIR script example
# parameters
N <- 1000 # network size
avk <- 7 # desired average degree
gamma <- 0.25 # recovery rate
tau <- 0.27 # per-edge infection rate
max_time <- 50 # maximum simulation length
i0 <- 10 # initial number of infected individuals
# build erdos-renyi random-graph
network <- matrix(0L, nrow = N, ncol = N)
@TimTaylor
TimTaylor / quoted_args.R
Last active February 24, 2021 04:11
return quoted arguments of given expression
# Based on this stackoverflow answer (https://stackoverflow.com/a/57709169)
# from Valeri Voev
#' Return quoted arguments of given expression
#'
#' @param args Expression such `x`, `"x"`, `c(x, y)` or `c("x", "y")`.
#'
#' @return The quoted arguments.
#'
#' @examples
@TimTaylor
TimTaylor / _Nvim-R-Tmux.md
Created December 14, 2020 13:25 — forked from tgirke/_Nvim-R-Tmux.md
Nvim-R-Tmux: An Integrated Working Environment for R

Nvim-R-Tmux: Neovim-based IDE for R

  • Author: Thomas Girke
  • Last update: 18-Nov-2020

Table of Content

  1. Overview