Skip to content

Commit

Permalink
Merge branch 'r-0.7.6' (early part)
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Jun 30, 2018
2 parents 7bcc0df + a8dc325 commit ea3820e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ To be released as 0.8.0
* The MASS package is explicitly suggested to fix CRAN warnings on R-devel
(#3657).

* Set operations like `intersect()` and `setdiff()` reconstruct groups metadata (#3587).

* Using namespaced calls to `base::sort()` and `base::unique()` from C++ code
to avoid ambiguities when these functions are overridden (#3644).

Expand Down
33 changes: 28 additions & 5 deletions R/dataframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,42 @@ anti_join.data.frame <- function(x, y, by = NULL, copy = FALSE, ...) {
# Set operations ---------------------------------------------------------------

#' @export
intersect.data.frame <- function(x, y, ...) intersect_data_frame(x, y)
intersect.data.frame <- function(x, y, ...) {
out <- intersect_data_frame(x, y)
reconstruct_set(out, x)
}

#' @export
union.data.frame <- function(x, y, ...) union_data_frame(x, y)
union.data.frame <- function(x, y, ...) {
out <- union_data_frame(x, y)
reconstruct_set(out, x)
}

#' @export
union_all.data.frame <- function(x, y, ...) bind_rows(x, y)
union_all.data.frame <- function(x, y, ...) {
out <- bind_rows(x, y)
reconstruct_set(out, x)
}

#' @export
setdiff.data.frame <- function(x, y, ...) setdiff_data_frame(x, y)
setdiff.data.frame <- function(x, y, ...) {
out <- setdiff_data_frame(x, y)
reconstruct_set(out, x)
}

#' @export
setequal.data.frame <- function(x, y, ...) equal_data_frame(x, y)
setequal.data.frame <- function(x, y, ...) {
out <- equal_data_frame(x, y)
reconstruct_set(out, x)
}

reconstruct_set <- function(out, x) {
if (is_grouped_df(x)) {
out <- grouped_df_impl(out, group_vars(x), group_drop(x), FALSE)
}

out
}

#' @export
distinct.data.frame <- function(.data, ..., .keep_all = FALSE) {
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-sets.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ test_that("set operations reconstruct grouping metadata (#3587)", {
df1 <- tibble(x = 1:4, g = rep(1:2, each = 2)) %>% group_by(g)
df2 <- tibble(x = 3:6, g = rep(2:3, each = 2))

expect_equal(setdiff(df1, df2), filter(df1, x < 3))
expect_equal(intersect(df1, df2), filter(df1, x >= 3))
expect_equal(union(df1, df2), tibble(x = 1:6, g = rep(1:3, each = 2)) %>% group_by(g))

expect_equal( setdiff(df1, df2) %>% group_rows(), list(1:2))
expect_equal( intersect(df1, df2) %>% group_rows(), list(1:2))
expect_equal( union(df1, df2) %>% group_rows(), list(5:6, 3:4, 1:2))
Expand Down

0 comments on commit ea3820e

Please sign in to comment.