Anomaly in Factor Order from As_Factor Function

I am trying to use as_factor() to create factor levels in the order where they appear in the underlying data. The function works fine where the underlying data are character, but not where they are numeric.

I tried the sample code given with the as_factor documentation. It uses an underlying character variable, and gives the same order as the underlying variable; that is what as_factor is supposed to do. But for a numeric variable the order is sorted, and as.factor and as_factor give the same order.

# anomaly with as_factor -- is this a feature or a bug?
# Bill Anderson  August 2019
require(tidyverse)
#> Loading required package: tidyverse
#> Warning: package 'tidyverse' was built under R version 3.6.1
#> Warning: package 'dplyr' was built under R version 3.6.1
# example from as_factor documentation
x <- c("a", "z", "g")
as_factor(x) # preserves input order, as desired
#> [1] a z g
#> Levels: a z g
as.factor(x) # factor levels obtained by sorting data
#> [1] a z g
#> Levels: a g z
# numeric example 
y <- c(1, 3, 2)
as_factor(y) # factor levels obtained by sorting data -- not what I expected
#> [1] 1 3 2
#> Levels: 1 2 3
as.factor(y) # factor levels obtained by sorting data
#> [1] 1 3 2
#> Levels: 1 2 3
identical(as_factor(y), as.factor(y))
#> [1] TRUE
# explicit character conversion
z <- as.character(y)
as_factor(z) # preserves input order, as desired
#> [1] 1 3 2
#> Levels: 1 3 2
as.factor(z) # factor levels obtained by sorting data
#> [1] 1 3 2
#> Levels: 1 2 3
# one can also put everything into a data frame, 
# so we can see the impact of the factor order is clearly visible
mtcars %>% group_by(cyl) %>%
  summarize(meandisp = mean(disp)) # cylinder order is sorted
#> # A tibble: 3 x 2
#>     cyl meandisp
#>   <dbl>    <dbl>
#> 1     4     105.
#> 2     6     183.
#> 3     8     353.
mtcars %>% group_by(as_factor(cyl)) %>%
  summarize(meandisp = mean(disp)) # cylinder order is still sorted
#> # A tibble: 3 x 2
#>   `as_factor(cyl)` meandisp
#>   <fct>               <dbl>
#> 1 4                    105.
#> 2 6                    183.
#> 3 8                    353.
mtcars %>% group_by(as_factor(as.character(cyl))) %>%
  summarize(meandisp = mean(disp)) # cylinder order follows data
#> # A tibble: 3 x 2
#>   `as_factor(as.character(cyl))` meandisp
#>   <fct>                             <dbl>
#> 1 6                                  183.
#> 2 4                                  105.
#> 3 8                                  353.

Created on 2019-08-15 by the reprex package (v0.3.0)

There are no error messages. The issue is simply that I don't get the order of the underlying data, unless I explicitly convert to character.

I am not sure if the situation is a feature or a bug. But if it is a feature I suggest that it should show up in the function documentation.

1
David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article