keyCrossRef.RdChecks a key for dangerous matches of old and new values in a key for different levels.
keyCrossRef(key, ignoreClass = NULL, verbose = FALSE, lowercase = FALSE)A variable key, ideally a long key. If a wide key is provided it is converted to long.
Classes that should be excluded from check. Useful when many integer variables are being reverse- coded. Takes a string or vector.
Should a statement about the number of issues detected be returned? Defaults to FALSE.
Should old and new values be passed through tolower function? Defaults to FALSE.
Presents a warning for potentially problematic key sections. Return is dependent on verbose argument.
Positions in a long key are referred to as levels. If a value is mismatched at levels 1 and 3, this means that issues are in rows 1 and 3 of the section of the given variable in a long key.
dat <- data.frame(x1 = sample(c("a", "b", "c", "d"), 100, replace = TRUE),
x2 = sample(c("Apple", "Orange"), 100, replace = TRUE),
x3 = ordered(sample(c("low", "medium", "high"), 100, replace = TRUE),
levels = c("low", "medium", "high")),
stringsAsFactors = FALSE)
key <- keyTemplate(dat, long = TRUE)
## No errors with a fresh key.
kutils:::keyCrossRef(key, verbose = TRUE)
#> [1] "No potentially problematic value matches across levels detected."
key[1:2, "value_new"] <- c("b", "a")
key[5, "value_new"]
#> [1] "."
key[7:9, "value_new"] <- c("high", "medium", "low")
kutils:::keyCrossRef(key)
#> Warning: x1: The value of "b" is at level 1 in value_new, but "b" is also the value at level 2 in value_old.
#> Warning: x1: The value of "a" is at level 2 in value_new, but "a" is also the value at level 1 in value_old.
kutils:::keyCrossRef(key, ignoreClass = c("ordered", "character"), verbose = TRUE)
#> [1] "No potentially problematic value matches across levels detected."