Checks, after the fact, that a recode did what you said it did. It cross-tabulates the original values against the recoded ones and stops with an error unless each original category went to exactly one new category, that category is the one in your declared mapping, no rows were lost or added, and no value became missing (or was filled in). Use it directly after any recode, including recodes done by other code or other software, to catch swapped groups before a model runs.
Value
Invisibly, when every check passes, the cross-tabulation as a data
frame with columns original, recoded (factors) and
Freq (integer), including the zero cells of the full table.
Otherwise an error.
Details
Factors are compared through their labels. The checks, in order:
originalandrecodedare atomic and the same length;is.na(original)equalsis.na(recoded)element by element;in the table of non-missing pairs, no original value has more than one recoded value (the recode is a function of the label);
each original value's recoded value equals
declared[old]; an original value that is not a name ofdeclaredmust be unchanged (identity).
The first failed check is an error naming the offending categories.
Examples
before <- c("W", "B", "W", NA)
after <- c("White", "Black", "White", NA)
tab <- verify_recode(before, after, c(W = "White", B = "Black"))
tab[tab$Freq > 0, ]
#> original recoded Freq
#> 1 B Black 1
#> 4 W White 2
# a value not in the declared mapping must be unchanged
verify_recode(c("W", "X"), c("White", "X"), c(W = "White"))
# swapped groups are caught
try(verify_recode(c("W", "B"), c("Black", "White"), c(W = "White", B = "Black")))
#> Error : verify_recode: ‘B’ was mapped to ‘White’ but the declared mapping says ‘Black’. THIS is how groups get swapped; fix the recode before any model runs.
# a value that became NA is caught
try(verify_recode(c("W", "B"), c("White", NA), c(W = "White", B = "Black")))
#> Error : verify_recode: missingness changed during the recode (values silently became NA, or NAs were filled).
