if (!require("prodlim")) {
install.packages("prodlim")
require("prodlim")
} # ensure installation and loading of package "prodlim"
ifelse(is.na(row.match(as.data.frame(dat), as.data.frame(lookup))),
"Voluntary",
"Default")
## [1] "Default" "Voluntary" "Voluntary" "Default" "Default" "Default"
## the function
## prodlim::row.match(as.data.frame(dat), as.data.frame(lookup))
## returns for each row in dat,
## the matching row number in lookup or
## NA if there is no match
##
## This resulting vector one can use to translate any non-NA to "Default" and
## any NA to "Voluntary" using the vectorized `ifelse`
А, я использовал в качестве примера следующие данные @Paul:
require(dplyr)
dat <- tibble(Age = c(50, 50, 49, 26, 32, 18), Units = c(3, 5, 5, 3, 4, 2))
lookup <- structure(list(Age = 18:69,
Units = c(2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L)),
row.names = c(NA,
-52L), class = c("tbl_df", "tbl", "data.frame"))