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

summarize on a grouped data frame drops order from ordered factors #2200

Closed
ateucher opened this issue Oct 26, 2016 · 4 comments
Closed

summarize on a grouped data frame drops order from ordered factors #2200

ateucher opened this issue Oct 26, 2016 · 4 comments
Assignees
Labels
bug an unexpected problem or unintended behavior
Milestone

Comments

@ateucher
Copy link
Contributor

It seems that summarize on a grouped data frame doesn't respect ordered factors. Possibly similar to #1112

library(dplyr)

data <- tibble(id = rep(letters[23:26], 3), 
               val = runif(12, 0, 12))

# Function that returns an ordered factor
make_levels <- function(x) {
  cut(x, breaks = c(2,4,6,8,10), labels = LETTERS[1:4], ordered_result = TRUE)
}

is.ordered(make_levels(3))
## [1] TRUE
ungrouped_summary <- data %>%
  summarize(mean_val = mean(val), 
            level = make_levels(mean_val))

## Keeps ordered
is.ordered(ungrouped_summary$level)
## [1] TRUE
grouped_summary <- data %>% group_by(id) %>% 
  summarize(mean_val = mean(val), 
            level = make_levels(mean_val))

## Keeps as factor, but not ordered any more:
is.ordered(grouped_summary$level)
## [1] FALSE
is.factor(grouped_summary$level)
## [1] TRUE
@krlmlr
Copy link
Member

krlmlr commented Nov 7, 2016

Confirmed. Would you like to contribute a testthat test?

@krlmlr krlmlr added data frame bug an unexpected problem or unintended behavior labels Nov 7, 2016
@ateucher
Copy link
Contributor Author

ateucher commented Nov 7, 2016

Great, thanks! Yes I'd be happy to

@ateucher
Copy link
Contributor Author

ateucher commented Nov 7, 2016

@krlmlr I added two tests, as I also found that min and max on an existing ordered factor also drops order. Hope they do the trick.

@krlmlr
Copy link
Member

krlmlr commented Nov 7, 2016

Awesome, thanks!

ateucher added a commit to ateucher/dplyr that referenced this issue Nov 8, 2016
krlmlr pushed a commit that referenced this issue Dec 6, 2016
, @ateucher)

* Add failing tests for preserving ordered factors (#2200)

* Simplify testthat message

* Simplify tests, use expect_s3_class

* Add skip() to failing tests  (#2200)

* Indenting

* Indenting
@krlmlr krlmlr self-assigned this Feb 10, 2017
@krlmlr krlmlr modified the milestone: data frame 2 Feb 20, 2017
@lock lock bot locked as resolved and limited conversation to collaborators Jun 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants