My data has levels that are theoretically possible but not present in the data. I can easily represent this in base
R:
factor(c("test","test1","test2"), levels = c("test","test1","test2","test3"))
If I table it, I see that test3
is 0. This is great and allows for the possibility that I can write functions assuming these levels include all possible outcomes in case data is eventually added that includes this level.
I can not replicate this in forcats
. First off, the as_factor
function does not accept any additional arguments:
forcats::as_factor(c("test","test1","test2"), levels = c("test","test1","test2","test3"))
The above produces an error.
The following works with a warning (which I would prefer to accomplish my goal without warnings, if possible):
forcats::as_factor(c("test","test1","test2")) %>% forcats::fct_recode(`test` = "test", `tests` = "test1", `tests` = "test2", `tests` = "test3")
Warning message:
Unknown levels in `f`: test3
Is there any way in forcats
to play with levels that theoretically exist but are not necessarily in the data at that moment?