R/utilities.R

Defines functions p nansd nanmean nanmax nanmin last first `%omits%` `%contains%`

Documented in first last nanmax nanmean nanmin nansd

# utilities.R

#' "Cat" %contains% "at" returns true
#'
#' @param s String to check
#' @param pattern Grep pattern to check
#' @return A logical
#' @export
`%contains%` = function(s, pattern) grepl(pattern, s)

#' "Cat" %omits% "dog" returns true
#'
#' @param s String to check
#' @param pattern Grep pattern to check
#' @return A logical
#' @export
`%omits%` = function(s, pattern) !grepl(pattern, s)

#' base "head" returning one item'
#'
#' @export
first = function(x) head(x, 1)

#' base "tail" returning one item
#'
#' @export
last = function(x) tail(x, 1)

#' base "min" ignoring nans
#' @export
nanmin = function(x, y) min(x, y, na.rm = TRUE)

#' base "max" ignoring nans
#' @export
nanmax = function(x, y) max(x, y, na.rm = TRUE)

#' base "mean" ignoring nans
#' @export
nanmean = function(x) mean(x, na.rm = TRUE)

#' base "sd" ignoring nans
#' @export
nansd = function(x) sd(x, na.rm = TRUE)

# lets you x %>% p instead of x %>% print
#' @export
p = function(x) print(x)
jlucasmckay/jlm documentation built on June 23, 2020, 11:24 p.m.