Skip to content

Instantly share code, notes, and snippets.

View njtierney's full-sized avatar
👷‍♂️
Have some work availability from October

Nicholas Tierney njtierney

👷‍♂️
Have some work availability from October
View GitHub Profile
library(tidyverse)
rule_data <- tibble(
  a_response = c(
    "1, 1 or 2 | 2, 3 or 4",
    "1, 1 to 5 months | 2, 6 months to a year",
    "1, 5 minutes or less | 2, 6-30 minutes"
  ),
  rule_single_response = c(FALSE, FALSE, FALSE),
  output = c(
date_vec_yyyy_mm_dd <- c(
  "2025-01-01", 
  "0000-00-00", 
  "2025-01-00", 
  "2025-00-01",
  "2025-00-00", 
  "0000-00-00"
)
library(lubridate)
add <- function(x) Reduce(`+`, x)
list(1,2,3)
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
#> [[3]]
library(tidyverse)
dat <- tibble(
  x = c(NA, NA, NaN, -Inf, Inf, -1, -0, 0, 1, 2), 
  x_NA = is.na(x), 
  # invert NA, convert to number
  x_NA_num = (!is.na(x))*1
)

dat
library(tidyverse)
df <- tibble(x = c(1,1,2,2))

df |> 
  group_by(x) |> 
  mutate(
    n = n(),
    rown = row_number(),
    max_row_n = max(row_number())
n_times <- 2
create_vec <- function(n_times) rep(as.character(1:4), each = n_times)
example_vec <- create_vec(n_times)


example_vec
#> [1] "1" "1" "2" "2" "3" "3" "4" "4"
example_lookup <- as.character(c(2, 4, 1, 3)) |> 
  setNames(1:4)
@njtierney
njtierney / str-count-vs-nchar.md
Created July 7, 2025 06:14
str-count-vs-nchar
vec <- sample(1:10000)
vec[sample(1:100)] <- NA

bm <- bench::mark(
  str_count = stringr::str_count(vec),
  nchar = nchar(vec)
)

summary(bm)
# differences btn tibble and data.frame
# tibbles are lazy, grumpy data.frames
library(tibble)
peng_tbl <- as_tibble(penguins)

# data.frames allow for partials names
penguins$species
#>   [1] Adelie    Adelie    Adelie    Adelie    Adelie    Adelie    Adelie   
#>   [8] Adelie    Adelie    Adelie    Adelie    Adelie    Adelie    Adelie   
library(purrr)
set.seed(2020)
means <- 1:4
bp <- bench::press(
  n = c(100, 1000, 10000, 100000),
  {
    bench::mark(
  map = {set.seed(2020);  map(means, rnorm, n = n, sd = 1)},
  lapply = {set.seed(2020); lapply(means, rnorm, n = n, sd = 1)}
@njtierney
njtierney / crossprod-speed.md
Created June 5, 2025 03:24
crossprod vs t() %*%
pi_mat <- matrix(c(0.5, 0.5))
z_mat <- matrix(c(32, 1))  

bm <- bench::mark(
  t = t(pi_mat) %*% z_mat,
  crossprod = crossprod(pi_mat, z_mat)
)

bm