diff --git a/NEWS.md b/NEWS.md index d33323dc80..ba4cd217f2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # dplyr (development version) +* `nest_join()` now preserves the type of `y` (#6295). + * `*_join()` now error if you supply them with additional arguments that aren't used (#6228). diff --git a/R/join.r b/R/join.r index 6ea0d3f3c8..5522b5df72 100644 --- a/R/join.r +++ b/R/join.r @@ -513,7 +513,7 @@ nest_join.data.frame <- function(x, # changing the key vars because of the cast new_cols <- vec_cast(out[names(x_key)], vec_ptype2(x_key, y_key)) - y_out <- set_names(y_in[vars$y$out], names(vars$y$out)) + y_out <- set_names(y[vars$y$out], names(vars$y$out)) new_cols[[name_var]] <- map(y_loc, vec_slice, x = y_out) out <- dplyr_col_modify(out, new_cols) diff --git a/tests/testthat/test-join.r b/tests/testthat/test-join.r index 3519daf3cf..fff1805b44 100644 --- a/tests/testthat/test-join.r +++ b/tests/testthat/test-join.r @@ -261,6 +261,15 @@ test_that("nest_join returns list of tibbles (#3570)",{ expect_s3_class(out$df2[[1]], "tbl_df") }) +test_that("nest_join respects types of y",{ + df1 <- tibble(x = c(1, 2), y = c(2, 3)) + df2 <- rowwise(tibble(x = c(1, 1), z = c(2, 3))) + out <- nest_join(df1, df2, by = "x", multiple = "all") + + expect_s3_class(out$df2[[1]], "rowwise_df") +}) + + test_that("nest_join computes common columns", { df1 <- tibble(x = c(1, 2), y = c(2, 3)) df2 <- tibble(x = c(1, 3), z = c(2, 3)) @@ -319,7 +328,7 @@ test_that("joins preserve groups", { # See comment in nest_join i <- count_regroups(out <- nest_join(gf1, gf2, by = "a", multiple = "all")) - expect_equal(i, 1L) + expect_equal(i, 4L) expect_equal(group_vars(out), "a") })