Skip to content

Instantly share code, notes, and snippets.

View jmbarbone's full-sized avatar

Jordan Mark Barbone jmbarbone

View GitHub Profile
@jmbarbone
jmbarbone / merge-list.md
Created May 17, 2023 16:16
merge lists in R
merge_list <- function(x, y, keep = c("right", "left")) {
  keep <- match.arg(keep)
  stopifnot(is.list(x), is.list(y))
  x <- Filter(Negate(is.null), as.list(x))
  y <- Filter(Negate(is.null), as.list(y))
  c(x, y)[!duplicated(c(names(x), names(y)), fromLast = keep == "right")]
}
do.call(rbind, lapply(
  list(
    integer      = 1L,
    integerish   = 1.0,
    double       = 1.1,
    factor       = factor("a"),
    date_double  = structure(19486, class = "Date"),
    date_integer = structure(19486L, class = "Date"),
    time_double  = structure(1683663687, class = c("POSIXct", "POSIXt")),
@jmbarbone
jmbarbone / rowwise-operations-r.md
Created March 1, 2023 18:33
rowwise operations in R
df <- palmerpenguins::penguins

bench::mark(
  rowwise = df |> 
    dplyr::rowwise() |> 
    dplyr::mutate(sum = sum(dplyr::c_across(c(bill_length_mm, bill_depth_mm, flipper_length_mm, body_mass_g)))) |> 
    dplyr::ungroup(),
  rowSums = df |> 
@jmbarbone
jmbarbone / reference-class-private-env.md
Last active February 3, 2023 22:45
"private" methods in ReferenceClass objects
fooReferenceClass <- setRefClass(
  "fooReferenceClass",
  fields = list(
    private = "environment",
    n = "integer"
  )
)

# create a list of values that will be transfered to private.  alternatively,
@jmbarbone
jmbarbone / do-call.R
Last active December 2, 2022 16:35
`do.call()` but with duplicated
do_call <- function(fun, ...) {
fun <- match.fun(fun)
forms <- as.list(formals(fun))
params <- list(...)
nms <- names(params)
o <- order(match(nms, names(forms)))
params <- params[o]
nms <- nms[o]
@jmbarbone
jmbarbone / regex-examples.R
Created November 30, 2022 01:59
Example of the differences from the regular expression matches
x <- c("apple", "banana", "orange", "pear", "grape")
p <- "[aeoiu][^(aeiou)]"
regexpr(p, x) # integer vector
#> [1] 1 2 1 3 3
#> attr(,"match.length")
#> [1] 2 2 2 2 2
#> attr(,"index.type")
#> [1] "chars"
#> attr(,"useBytes")
@jmbarbone
jmbarbone / datarow.R
Created November 10, 2022 18:13
base version of tibble::tribble()
datarows <- function(...) {
ls <- list(...)
w <- which(vapply(ls, inherits, NA, "formula"))
n <- length(w)
cols <- as.character(ls[w])
cols <- substr(cols, 2, nchar(cols))
ls <- ls[-w]
sa <- seq_along(ls)
sa <- (sa - 1) %% n
res <- lapply(split(ls, sa), unlist)
@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,