Skip to content

Commit

Permalink
Ensure grouped_df methods drop grouping vars when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed May 16, 2016
1 parent fd505e0 commit 3dbcbf7
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 13 deletions.
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

* `unite()` and `separate()` now automatically drop removed variables from
grouping (#159, #177).

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

Expand Down
2 changes: 1 addition & 1 deletion R/complete.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ complete_.data.frame <- function(data, cols, fill = list(), ...) {

#' @export
complete_.grouped_df <- function(data, cols, fill = list(), ...) {
dplyr::grouped_df(NextMethod(), dplyr::groups(data))
regroup(NextMethod(), data)
}
4 changes: 2 additions & 2 deletions R/extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ extract_.tbl_df <- function(data, col, into, regex = "([[:alnum:]]+)",

#' @export
extract_.grouped_df <- function(data, col, into, regex = "([[:alnum:]]+)",
remove = TRUE, convert = FALSE, ...) {
dplyr::grouped_df(NextMethod(), dplyr::groups(data))
remove = TRUE, convert = FALSE, ...) {
regroup(NextMethod(), data, if (remove) col)
}
2 changes: 1 addition & 1 deletion R/gather.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ gather_.tbl_df <- function(data, key_col, value_col, gather_cols,
#' @export
gather_.grouped_df <- function(data, key_col, value_col, gather_cols,
na.rm = FALSE, convert = FALSE, factor_key = FALSE) {
regroup(data, NextMethod(), gather_cols)
regroup(NextMethod(), data, gather_cols)
}

# Functions from reshape2 -------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion R/separate.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ separate_.tbl_df <- function(data, col, into, sep = "[^[:alnum:]]+",
separate_.grouped_df <- function(data, col, into, sep = "[^[:alnum:]]+",
remove = TRUE, convert = FALSE,
extra = "warn", fill = "warn", ...) {
dplyr::grouped_df(NextMethod(), dplyr::groups(data))
regroup(NextMethod(), data, if (remove) col)
}


Expand Down
2 changes: 1 addition & 1 deletion R/spread.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ spread_.tbl_df <- function(data, key_col, value_col, fill = NA,
#' @export
spread_.grouped_df <- function(data, key_col, value_col, fill = NA,
convert = FALSE, drop = TRUE) {
regroup(data, NextMethod(), except = c(key_col, value_col))
regroup(NextMethod(), data, c(key_col, value_col))
}

split_labels <- function(df, id, drop = TRUE) {
Expand Down
3 changes: 2 additions & 1 deletion R/unite.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ unite_.tbl_df <- function(data, col, from, sep = "_", remove = TRUE) {

#' @export
unite_.grouped_df <- function(data, col, from, sep = "_", remove = TRUE) {
dplyr::grouped_df(NextMethod(), dplyr::groups(data))
regroup(NextMethod(), data, if (remove) from)
}

2 changes: 1 addition & 1 deletion R/unnest.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,5 @@ unnest_.tbl_df <- function(data, unnest_cols, .drop = NA) {

#' @export
unnest_.grouped_df <- function(data, unnest_cols, .drop = NA) {
dplyr::grouped_df(dplyr::ungroup(data), dplyr::groups(data))
regroup(unnest_(dplyr::ungroup(data), unnest_cols, .drop = .drop), data)
}
11 changes: 6 additions & 5 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ list_indices <- function(x, max = 20) {

`%||%` <- function(x, y) if (length(x) == 0) y else x

regroup <- function(x, y, except) {
group_vars <- vapply(dplyr::groups(x), as.character, character(1))
group_vars <- setdiff(group_vars, except)
group_vars <- lapply(group_vars, as.name)
regroup <- function(x, y, except = NULL) {
groups <- dplyr::groups(y)
if (!is.null(except)) {
groups <- setdiff(groups, lapply(except, as.name))
}

dplyr::grouped_df(y, group_vars)
dplyr::grouped_df(x, groups)
}

# Allows tests to work with either dplyr 0.4 (which ignores value of
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-separate.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@ test_that("preserves grouping", {
expect_equal(class(df), class(rs))
expect_equal(dplyr::groups(df), dplyr::groups(rs))
})

test_that("drops grouping when needed", {
df <- dplyr::data_frame(x = "a:b") %>% dplyr::group_by(x)
rs <- df %>% separate(x, c("a", "b"))

expect_equal(rs$a, "a")
expect_equal(dplyr::groups(rs), NULL)
})
9 changes: 9 additions & 0 deletions tests/testthat/test-unite.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ test_that("unite preserves grouping", {
expect_equal(class(df), class(rs))
expect_equal(dplyr::groups(df), dplyr::groups(rs))
})


test_that("drops grouping when needed", {
df <- dplyr::data_frame(g = 1, x = "a") %>% dplyr::group_by(g)
rs <- df %>% unite(gx, g, x)

expect_equal(rs$gx, "1_a")
expect_equal(dplyr::groups(rs), NULL)
})
1 change: 1 addition & 0 deletions tests/testthat/test-unnest.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ test_that("grouping is preserved", {
df <- dplyr::data_frame(g = 1, x = list(1:3)) %>% dplyr::group_by(g)
rs <- df %>% unnest(x)

expect_equal(rs$x, 1:3)
expect_equal(class(df), class(rs))
expect_equal(dplyr::groups(df), dplyr::groups(rs))
})

0 comments on commit 3dbcbf7

Please sign in to comment.