As you probably guessed, the value that didn't match the defined levels was "secretly" converted to
NA by R.
We would like to know about this before it happens! With a small vector, it's not much of a problem, but we don't often work with just small vectors. If we were using a large vector, maybe we wouldn't have noticed the change.
Fortunately, we have some help: we have forcats and the
parse_factor() function can create a factor, but it also gives us a warning if the values in the factor do not match its levels. The advantage is that we know about all possible
NAs before the factor is created. This is what it looks like:
fct_age <- parse_factor(c("senior", "adult"), levels = age_levels)
You can set the
levels argument to
NULL to tell R to deduce the factor levels from the unique values of the vector you passed in:
fct_age <- parse_factor(c("senior", "adult"), levels = NULL)