Sometimes you want to drop one or two specific unused factor levels but you don't want to drop all of the unused factor levels. For example, you might have the following factor from survey data
vote_intention <- factor(x = c("Democrat", "Republican"),
levels = c("Democrat", "Republican",
"Independent", "Undecided"))
where you'd be interested in removing the 'Undecided' level but not the 'Independent' level, even though both are unused. In such a case, you would essentially want to use a version of fct_drop() that doesn't drop every unused level but instead allows finer control over which levels are dropped.
A simple example of such a function would be the following:
fct_drop_specific <- function(f, l) {
factor(x = f,
levels = setdiff(levels(f), l),
ordered = is.ordered(f))
}
fct_drop_specific(f = vote_intention, l = "Undecided")
Forcats seems like the best package to have a function that would clearly accomplish this. Unless this is already implemented elsewhere, perhaps this could be implemented using arguments to fct_drop() or as a new function in forcats.
Sometimes you want to drop one or two specific unused factor levels but you don't want to drop all of the unused factor levels. For example, you might have the following factor from survey data
where you'd be interested in removing the 'Undecided' level but not the 'Independent' level, even though both are unused. In such a case, you would essentially want to use a version of
fct_drop()that doesn't drop every unused level but instead allows finer control over which levels are dropped.A simple example of such a function would be the following:
Forcats seems like the best package to have a function that would clearly accomplish this. Unless this is already implemented elsewhere, perhaps this could be implemented using arguments to
fct_drop()or as a new function in forcats.