Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return an ordered factor with fct_inorder and fct_infreq #54

Closed
andrewheiss opened this issue Nov 8, 2016 · 1 comment
Closed

Return an ordered factor with fct_inorder and fct_infreq #54

andrewheiss opened this issue Nov 8, 2016 · 1 comment

Comments

@andrewheiss
Copy link

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)
@hadley hadley closed this as completed in cb9fe87 Dec 30, 2016
@jzadra
Copy link

jzadra commented Dec 4, 2019

Wondering the same since I'm trying to included an ordered factor in a linear model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants