Skip to content

Commit 26156fa

Browse files
1 parent 642186b commit 26156fa

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

R/group-by.r

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,8 @@ group_by_prepare <- function(.data, ..., .add = FALSE, .dots = deprecated(), add
149149
}
150150

151151
# If any calls, use mutate to add new columns, then group by those
152-
ungrouped <- ungroup(.data)
153-
atts <- attributes(.data)
154-
atts <- append(
155-
attributes(ungrouped),
156-
atts[setdiff(names(atts), c("names", "row.names", "groups", "class"))]
157-
)
158-
attributes(ungrouped) <- atts
152+
computed_columns <- add_computed_columns(.data, new_groups, "group_by")
159153

160-
computed_columns <- add_computed_columns(ungrouped, new_groups, "group_by")
161154
out <- computed_columns$data
162155
group_names <- computed_columns$added_names
163156

@@ -188,7 +181,7 @@ add_computed_columns <- function(.data, vars, .fn = "group_by") {
188181
# TODO: use less of a hack
189182
if (inherits(.data, "data.frame")) {
190183
cols <- withCallingHandlers(
191-
mutate_cols(.data, !!!vars),
184+
mutate_cols(ungroup(.data), !!!vars),
192185
error = function(e) {
193186
abort(c(
194187
glue("Problem adding computed columns in `{.fn}()`."),

tests/testthat/test-group-by.r

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,9 @@ test_that("group_by() keeps attributes unrelated to the grouping (#5760)", {
538538

539539
d3 <- group_by(d2, y, .add = TRUE)
540540
expect_equal(attr(d2, "foo"), "bar")
541+
542+
d4 <- group_by(d2, y2 = y * 2, .add = TRUE)
543+
expect_equal(attr(d2, "foo"), "bar")
541544
})
542545

543546
# Errors ------------------------------------------------------------------

0 commit comments

Comments
 (0)