Skip to content

Commit

Permalink
add label_lut()
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand committed Oct 7, 2024
1 parent 6e5e4c3 commit aad1da2
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions R/label-lut.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

label_lut <- function(lut = character(), nomatch = NULL) {

if (!is.character(lut)) {
cli::cli_abort("The {.arg lut} argument must be a character vector.")
}
if (!is_named2(lut)) {
cli::cli_abort("The {.arg lut} argument must have names.")
}
names <- names(lut)
values <- unname(lut)

force(nomatch)

function(x) {
i <- match(x, names, nomatch = NA_integer_)
out <- values[i]
missing <- is.na(i)
out[missing] <- if (is.null(nomatch)) x[missing] else nomatch
out
}
}

0 comments on commit aad1da2

Please sign in to comment.