Skip to content

Commit

Permalink
Preserve ordered attribute in expand.
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed May 16, 2016
1 parent 2876b5d commit fd505e0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# tidyr 0.4.1.9000

* `expand()` (and hence `complete()`) preserves the ordered attribute of
factors (#165).

* `complete()` preserves grouping created by dplyr (#168).

* `nest()` now returns correct output if every variable is nested (#186).
Expand Down
2 changes: 1 addition & 1 deletion R/spread.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ ulevels <- function(x) {
if (is.factor(x)) {
x <- addNA(x, ifany = TRUE)
levs <- levels(x)
factor(levs, levels = levs)
factor(levs, levels = levs, ordered = is.ordered(x))
} else {
sort(unique(x))
}
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-expand.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ test_that("expand respects groups", {
expect_equal(out$data[[2]], dplyr::data_frame(b = 1L, c = 1L))
})

test_that("preserves ordered factors", {
df <- dplyr::data_frame(a = ordered("a"))
out <- expand(df, a)

expect_equal(df$a, ordered("a"))
})

0 comments on commit fd505e0

Please sign in to comment.