-
Notifications
You must be signed in to change notification settings - Fork 3
/
validation-uniqueness.R
43 lines (39 loc) · 1.15 KB
/
validation-uniqueness.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#' Validate that a vector has only unique values
#'
#' @param values An array of values
#' @param na.rm Logical to decide if missing values should be removed from the
#' duplicate checking. Note that duplicate `NA` values are flagged if
#' `na.rm=FALSE`.
#'
#' @return
#'
#' - `hasOnlyDistinctValues` returns `TRUE` if all values are unique.
#'
#' - `validateHasOnlyDistinctValues()` returns `NULL` if only unique values
#' present, otherwise produces error.
#'
#' @examples
#'
#' hasOnlyDistinctValues(c("x", "y"))
#' hasOnlyDistinctValues(c("x", "y", "x"))
#' hasOnlyDistinctValues(c("x", NA, "y", NA), na.rm = FALSE)
#' hasOnlyDistinctValues(c("x", NA, "y", NA), na.rm = TRUE)
#'
#' validateHasOnlyDistinctValues(c("x", "y")) # NULL
#' # validateHasOnlyDistinctValues(c("x", "y", "x")) # error
#'
#' @export
hasOnlyDistinctValues <- function(values, na.rm = TRUE) {
if (na.rm) {
values <- values[!is.na(values)]
}
return(!any(duplicated(values)))
}
#' @rdname hasOnlyDistinctValues
#' @export
validateHasOnlyDistinctValues <- function(values, na.rm = TRUE) {
if (hasOnlyDistinctValues(values)) {
return()
}
stop(messages$errorDuplicatedValues())
}