R/str-distinct.R

#' @title Extract characters that differ between two strings
#' @param a character vector
#' @param b character vector
#' @return character vector with strings in a that are not in b
#' @examples
#' \dontrun{
#' if(interactive()){
#'  str_distinct("abc", "cde")
#'  str_distinct("AERRRTX", "TRRA")
#'  }
#' }
#' @rdname str_distinct
#' @export
str_distinct <- function(a, b) {
  y <- table(strsplit(b, "")[[1]])
  x <- table(strsplit(a, "")[[1]])
  res <- substr(
    setdiff(
      x = paste0(names(x), x),
      y = paste0(names(y), y)
    ),
    start = 1,
    stop = 1
  )
  if(length(res) == 0) res <- NA
  return(res)
}
lhmet/rinmetxls documentation built on May 28, 2019, 11:33 a.m.