R/remove_duplicates.R

Defines functions remove_duplicates

Documented in remove_duplicates

#' For use with tables
#'
#' @description Turns duplicate rows into NAs
#'
#' @param x Character vector
#' @param keepLast Logical indicating whether the last incidence should be kept
#'                instead of the first (which is the default).
#'
#' @return A character string/vector
#'
#' @examples
#'     x <- c(rep("a", 5), rep("c", 2), rep("y", 7))
#'     remove_duplicates(x)
#'
#' @export
remove_duplicates <- function(x, keepLast = FALSE) {

  if (is.factor(x)) {
    x <- as.character(x)
  }

  x[duplicated(x, fromLast = keepLast)] <- NA

  return(x)
}
EstherHerbert/Useful.functions documentation built on Jan. 31, 2025, 10:41 a.m.