Skip to content

Feature Suggestion - Function to Remove Specific Unused Levels #69

Description

@bschneidr

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions