Skip to content

Feature request: reorder factor levels using a user-supplied sorting function #117

@russHyde

Description

@russHyde

Hi,
I was wondering whether you might consider adding a function to forcats that sets up factor levels according to a numerically-aware string-sorting method like gtools::mixedsort.
There's a number of use cases: eg, when you end up with genomes ordered like "chr1", "chr10", "chr11" ... "chr2" rather than "chr1", "chr2", ..., in a plot if you don't hack your factors correctly

eg,

fct_alphameric/fct_alnum/fct_mixedsort <- function(x, ...) {
    factor(x, levels = gtools::mixedsort(unique(x), ...))
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancementtidy-dev-day 🤓Tidyverse Developer Day rstd.io/tidy-dev-day

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions