From fd505e08712cf416e7bbc1cd849e4ae1e42c0683 Mon Sep 17 00:00:00 2001 From: hadley Date: Mon, 16 May 2016 15:39:07 -0500 Subject: [PATCH] Preserve ordered attribute in expand. Fixes #165 --- NEWS.md | 3 +++ R/spread.R | 2 +- tests/testthat/test-expand.R | 6 ++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 93fcc8e65..fadb67121 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). diff --git a/R/spread.R b/R/spread.R index ba6f84d5e..e14090bc2 100644 --- a/R/spread.R +++ b/R/spread.R @@ -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)) } diff --git a/tests/testthat/test-expand.R b/tests/testthat/test-expand.R index d3377c1ba..50ac92716 100644 --- a/tests/testthat/test-expand.R +++ b/tests/testthat/test-expand.R @@ -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")) +})