From c2af07847a357396515461c887952298d9974e35 Mon Sep 17 00:00:00 2001 From: Josh Katz Date: Sat, 23 Jan 2016 16:19:29 -0500 Subject: [PATCH 1/4] fix nest sorting --- R/nest.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/nest.R b/R/nest.R index 4c1821f3d..ea16b2ee3 100644 --- a/R/nest.R +++ b/R/nest.R @@ -72,7 +72,7 @@ nest_impl <- function(data, key_col, group_cols, nest_cols) { out <- dplyr::distinct_(dplyr::select_(data, .dots = group_cols)) idx <- dplyr::group_indices_(data, .dots = group_cols) - out[[key_col]] <- unname(split(data[nest_cols], idx)) + out[[key_col]] <- unname(split(data[nest_cols], idx))[order(unique(idx))] out } From 966a20c485e5313e035bb6dc33a2cf56c0d43794 Mon Sep 17 00:00:00 2001 From: Josh Katz Date: Sat, 23 Jan 2016 18:30:43 -0500 Subject: [PATCH 2/4] fix sorting --- R/nest.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/nest.R b/R/nest.R index ea16b2ee3..ab17e5f7b 100644 --- a/R/nest.R +++ b/R/nest.R @@ -72,7 +72,7 @@ nest_impl <- function(data, key_col, group_cols, nest_cols) { out <- dplyr::distinct_(dplyr::select_(data, .dots = group_cols)) idx <- dplyr::group_indices_(data, .dots = group_cols) - out[[key_col]] <- unname(split(data[nest_cols], idx))[order(unique(idx))] + out[[key_col]] <- unname(split(data[nest_cols], idx))[unique(idx)] out } From a9657b8f2e1fb529443fe2574930826d0af2f84d Mon Sep 17 00:00:00 2001 From: Josh Katz Date: Sun, 24 Jan 2016 12:34:38 -0500 Subject: [PATCH 3/4] add unit test --- tests/testthat/test-nest.R | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/testthat/test-nest.R b/tests/testthat/test-nest.R index 89139ba6d..4fd6b2481 100644 --- a/tests/testthat/test-nest.R +++ b/tests/testthat/test-nest.R @@ -29,3 +29,10 @@ test_that("can restrict variables in grouped nest", { expect_equal(names(out$data[[1]]), "y") }) +test_that("puts data into the correct row", { + df <- dplyr::data_frame(x = 1:3, y = c("B", "A", "A")) + out <- nest(df, x) %>% + dplyr::filter(y == "B") + + expect_equal(out$data[[1]]$x, 1) +}) From 678b8e7f8548022638ef9c63b5e947ff38f688a5 Mon Sep 17 00:00:00 2001 From: Josh Katz Date: Wed, 3 Feb 2016 13:51:37 -0500 Subject: [PATCH 4/4] add NEWS entry --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 5bc6fec17..e94cbe977 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # tidyr 0.4.0.9000 +* Fixed bug in `nest()` where nested data was ending up in the wrong row (#158). + # tidyr 0.4.0 ## Nested data frames