R/str_relevel.R

#' Relevel a character vector
#'
#' Move the first occurrence of a value to the first position.
#'
#' @param x (character) vector
#' @param what (character) value to look for
#'
#' @examples
#' str_relevel(LETTERS, "J")
#' str_relevel(LETTERS, "foo")
#'
#' @export
str_relevel <- function (x, what) {
  i <- first(match(what, x))
  if (is.na(i)) stop(str_c("no elements are equal to '", what, "'"))
  c(x[i], x[-i])
}
BAAQMD/strtools documentation built on Nov. 23, 2023, 5:52 a.m.