R/nums_in_strings.R

Defines functions f extract_sci_exp extract_nums_all extract_nums_tgthr remove_nums

Documented in extract_nums_all extract_nums_tgthr extract_sci_exp remove_nums

#'Dealing with strings with numbers
#'
#'@param x character vector, or other vector which will be coerced to character
#'@param trimws logical. whether to call `base::trimws()`, trimming all whitespace
#' at start of end of string before returning.
#'@describeIn nums_in_strings remove all the numbers from a string
#'@export
remove_nums <- function(x, trimws = FALSE) {
  out <- gsub("[-0-9.-]+", "", as.character(x))
  if(trimws) trimws(out) else out
}

#'@describeIn nums_in_strings extract the numbers from a string all together
#' (i.e. `12aaa34` -> `1234`)
#'@export
extract_nums_tgthr <- function(x) {
  as.numeric(gsub("[^0-9.-]+", "", as.character(x)))
}

#'@describeIn nums_in_strings extract all the separate numbers from a string.
#' returns list of numeric vectors. (i.e., 12aaa34 -> `[1] 12 [2] 34`)
#'@export
extract_nums_all <- function(x) {
  x <- as.character(x)
  gg <- gregexpr("-?[0-9]+(.[0-9]+)?", x)
  ss <- strsplit(x, "")

  stopifnot(length(ss) == length(gg))
  lapply(seq_along(ss), function(i) f(ss[[i]], gg[[i]]))

}

#' Extract scientific notation exponent
#'
#' @param x string of numbers in scientific notation
#'
extract_sci_exp <- function(x) {
  gsub(".*e", "", as.character(x))
}




# helpers ------------------------------------------------------------------

f <- function(s, g) {
  st <- g
  ml <- attr(g, "match.length")
  vapply(seq_along(st), FUN.VALUE = numeric(1), function(i) {
    as.numeric(paste(s[st[i]:(st[i]+ml[i]-1)], collapse = ""))
  })

}
WhiteJP/jpw documentation built on April 17, 2025, 5:47 a.m.