Skip to content

Instantly share code, notes, and snippets.

View jmbarbone's full-sized avatar

Jordan Mark Barbone jmbarbone

View GitHub Profile
@jmbarbone
jmbarbone / run-length-encoding.R
Last active November 5, 2022 02:47
rle() with some enhancements. Repurposed from a separate script
# function has been repurposed
# a few edits from the original have been made without testing
#' Run length encode
#'
#' Encodes a run length and returns the start and stop
#'
#' @param x A vector of values for compute the length of the run
#' @param times If `NULL` will use the position of the start and stop runs,
#' otherwise will return the values returned; if not `NULL`, must be equal
@jmbarbone
jmbarbone / equate-sat-act.R
Last active October 19, 2022 20:47
example of `{equate}` with SAT ACT data from `{psych}`
x <- psych::sat.act$ACT
y <- psych::sat.act$SATQ + psych::sat.act$SATV
ft <- equate::freqtab(data.frame(x, y))
eq0 <- equate::equate(
x = ft,
type = "equipercentile",
spearman_ci <- function(x, y, alpha = 0.95, method = c("norm", "t")) {
# modified from https://stats.stackexchange.com/questions/18887/how-to-calculate-a-confidence-interval-for-spearmans-rank-correlation/506367#506367
method <- mark::match_param(method)
ok <- stats::complete.cases(x, y)
x <- x[ok]
y <- y[ok]
r <- cor(x, y, method = "spearman")
n <- sum(ok)
q <- switch(
method,
@jmbarbone
jmbarbone / line-breaks-returns-regex.R
Created September 30, 2022 17:59
Dealing with \n in text
text <- {
"
Line 1 \n info
Line 2 \n info
Line 3
Line 4
"
}
print(text)
#> [1] "\n Line 1 \n info\n Line 2 \n info\n Line 3\n Line 4\n "
@jmbarbone
jmbarbone / detach-recursive.R
Last active August 3, 2022 17:09
Recursive detach
detach_recursive <- function(name, ...) {
stopifnot(!missing(name), is.character(name))
params <- list(...)
params$name <- name
params$character.only <- TRUE
repeat {
if (inherits(try(do.call(detach, params), silent = TRUE), "try-error")) {
break
}
@jmbarbone
jmbarbone / quarto-render2.R
Last active July 12, 2022 19:46
alternative quarto rendering function
#' Render quarto files
#'
#' Render quarto files
#'
#' @details
#' The `.qmd` input file is copied as a temporary file, which is then used for
#' rendering. The output of this is then copied over to the intended output
#' file.
#'
@jmbarbone
jmbarbone / jira-table-paste.R
Last active June 14, 2022 19:34
paste data.frame as a Jira table
``` r
jira_table_paste <- function(data, quiet = FALSE) {
stopifnot(is.data.frame(data), all(!sapply(data, is.list)))
data[] <- lapply(data, function(i) paste0("{{", i, "}}"))
data <- rbind(matrix(colnames(data), nrow = 1L), as.matrix(data))
data[] <- apply(data, 2L, function(i) format(paste0(" ", i, " ")))
res <- c(
# the headers come out as a little annoying
paste0("||", paste0(data[1L, ], collapse = "||"), "||"),
paste0("|", apply(data[-1L, ], 1L, paste0, collapse = "|"), "|")
@jmbarbone
jmbarbone / prepare-commit-msg-github
Last active December 2, 2022 19:38
Prepend all commits with number of JIRA-like issue (from branch)
#! /usr/bin/env sh
# Based on https://serebrov.github.io/html/2019-06-16-git-hook-to-add-issue-number-to-commit-message.html
#
# This hook works for branches named such as "123-description" and will add "#123 " to the commit message.
#
# 123-description >> #123
#
# Example:
#
# ```
# original ----------------------------------------------------------------
new <- list() # construct as list -- data.frames are fancy lists
cols <- c(1, 5, 3) # use a vector of column indices
for (i in seq_along(cols)) {
# append the list at each column
new[[i]] <- mtcars[, cols[i], drop = FALSE]
}
new <- as.data.frame(new) # make list into data.frame
@jmbarbone
jmbarbone / ranks-and-stuff.R
Last active April 15, 2022 15:17
ranking stuff
rank_limited <- function(x, method = c("auto", "shell", "quick", "radix")) {
u <- sort(unique(x), method = method)
match(x, u)
}
rank_limited2 <- function(x, method = c("auto", "shell", "quick", "radix")) {
u <- unique(sort(x, method = method))
match(x, u)
}