Skip to content

Return an ordered factor with fct_inorder and fct_infreq #54

Closed
@andrewheiss

Description

@andrewheiss

When using fct_inorder() or fct_infreq() to create a factor ordered by appearance or frequency, the functions create a factor with levels in the correct order, but the factor is not an official ordered factor, so the ordering does not carry over to ggplot plots, ordered logistic regression models, or any other situations where ordered factors matter.

Right now, these two functions have to be wrapped in ordered() to make them actual ordered factors. It would be nice if there were an ordered=TRUE/FALSE argument in fct_inorder() and fct_infreq() to avoid this extra step.

f <- factor(c("b", "b", "a", "c", "c", "c"))
f
#> [1] b b a c c c
#> Levels: a b c

# Unordered
fct_inorder(f)
#> [1] b b a c c c
#> Levels: b a c
fct_infreq(f)
#> [1] b b a c c c
#> Levels: c b a

# Ordered
ordered(fct_inorder(f))
#> [1] b b a c c c
#> Levels: b < a < c
ordered(fct_infreq(f))
#> [1] b b a c c c
#> Levels: c < b < a

# Would be nice
fct_inorder(f, ordered=TRUE)
#> Error in fct_inorder(f, ordered = TRUE): unused argument (ordered = TRUE)
fct_infreq(f, ordered=TRUE)
#> Error in fct_infreq(f, ordered = TRUE): unused argument (ordered = TRUE)

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