R/not_set.R

#' not-in and Friends
#'
#' @name not-in
#' @seealso \link{match}
NULL

#' @export
#' @rdname not-in
`%!in%` <- function(x, table) match(x, table, nomatch = 0L) == 0L

#' @export
#' @rdname not-in
`%not_in%` <- `%!in%`

#' @export
#' @rdname not-in
`%without%` <- function(x, y) x[match(x, y, nomatch = 0L) == 0L]


#' @export
#' @rdname not-in
without <- function(x, y)  x[match(x, y, nomatch = 0L) == 0L]




#' all_identical
#'
#' Are all elements of a vector identical? Test for equality is done with
#' \code{`==`}.
#'
#' Vectors of length 1 and 0 (potentially after removing \code{NA} elements)
#' return \code{TRUE}.
#'
#' @param allNA should vectors where \code{all(is.na(v)))} return \code{TRUE}.
#'   If set to \code{TRUE}, \code{na.rm} is ignored.
#' @param na.rm ignore NA elements for the comparison. ignored if \code{allNA}
#'   is \code{TRUE}
#'
#'
#' @export
all_identical <- function(v, allNA = FALSE, na.rm = FALSE) {

  if (length(v) <= 1L) return(TRUE)

  if (any(is.na(v))){
    if(allNA) return(all(is.na(v)))
    if(na.rm) {
      v <- v[!is.na(v)]
      # need to check for length again after na.rm
      if (length(v) <= 1L) return(TRUE)
    } else return(FALSE)
  }

  all(v[1] == v[-1])
}

#' @export
#' @rdname all_identical
not_all_identical <- function(v, ...) !all_identical(v, ...)
t-kalinowski/notr documentation built on May 31, 2019, 12:51 a.m.