Skip to content

Commit

Permalink
Implement special case where everything is nested.
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed May 16, 2016
1 parent c011e4a commit 1a5d521
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# tidyr 0.4.1.9000

* `nest()` now returns correct output if every variable is nested (#186).

* `full_seq()` now preserve attributes for dates and date/times (#156).

* Sequences generated by `full_seq()` no now longer need to start at 0.
Expand Down
8 changes: 8 additions & 0 deletions R/nest.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ nest_.grouped_df <- function(data, key_col, nest_cols = character()) {

nest_impl <- function(data, key_col, group_cols, nest_cols) {
data <- dplyr::ungroup(data)

if (length(group_cols) == 0) {
df <- dplyr::data_frame(list(data))
names(df) <- key_col

return(df)
}

nest_cols <- setdiff(nest_cols, group_cols)

out <- dplyr::distinct_(dplyr::select_(data, .dots = group_cols))
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-nest.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ test_that("puts data into the correct row", {

expect_equal(out$data[[1]]$x, 1)
})

test_that("nesting everything yields a simple data frame", {
df <- dplyr::data_frame(x = 1:3, y = c("B", "A", "A"))
out <- nest(df, x, y)
expect_equal(out, dplyr::data_frame(data = list(df)))
})

0 comments on commit 1a5d521

Please sign in to comment.