Convert vector to factor based on threshold of number of unique levels
Source:R/ds2dd_detailed.R
var2fct.Rd
This is a wrapper of forcats::as_factor, which sorts numeric vectors before factoring, but levels character vectors in order of appearance.