When data is grouped with the argument .drop = FALSE and the grouping variable is an ordered factor, the summarise() function converts the ordered factor to an unordered factor in the resulting data frame. The same is true when using count(..., .drop = FALSE).
Is there some way to preserve the original class of the grouping variable, or is this an ordered factor bug? Thank you!
library(dplyr, warn.conflicts = FALSE)
#> Warning: package 'dplyr' was built under R version 4.0.2
packageVersion("dplyr")
#> [1] '1.0.1'
packageVersion("vctrs")
#> [1] '0.3.2'
mtcars %>%
as_tibble() %>%
mutate(cyl = factor(cyl, ordered = TRUE)) %>% # making cyl ordered factor
group_by(cyl, .drop = FALSE) %>%
summarize(n = n())
#> `summarise()` ungrouping output (override with `.groups` argument)
#> # A tibble: 3 x 2
#> cyl n
#> <fct> <int>
#> 1 4 11
#> 2 6 7
#> 3 8 14
Created on 2020-08-02 by the reprex package (v0.3.0)
Came here to post the same thing, my reprex was a lot less minimal than yours so I'll just say, ditto.
Somewhat more minimal reprex:
library(dplyr, warn.conflicts = FALSE)
#> Warning: replacing previous import 'vctrs::data_frame' by 'tibble::data_frame'
#> when loading 'dplyr'
df <- tibble(x = ordered("x"))
df %>% group_by(x) %>% group_data()
#> # A tibble: 1 x 2
#> x .rows
#> * <ord> <list<int>>
#> 1 x [1]
df %>% group_by(x, .drop = FALSE) %>% group_data()
#> # A tibble: 1 x 2
#> x .rows
#> * <fct> <list<int>>
#> 1 x [1]
Created on 2020-08-28 by the reprex package (v0.3.0.9001)
Most helpful comment
Somewhat more minimal reprex:
Created on 2020-08-28 by the reprex package (v0.3.0.9001)