-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ungroup()
drops data frame attributes
#6774
Comments
FWIW this happens in dplyr 1.0.10 and looks to have happened since at least 2020, so I don't consider this an immediate regression. And I'm not 100% it is a bug. # pak::pak("tidyverse/[email protected]")
library(dplyr, warn.conflicts = FALSE)
d = mtcars
attr(d, "foo") = "bar"
attr(d, "foo")
#> [1] "bar"
# kept after group-by
d |> group_by(cyl) |> attr("foo")
#> [1] "bar"
# dropped here
d |> group_by(cyl) |> ungroup() |> attr("foo")
#> NULL
# really the `ungroup()` case is because of
d |> group_by(cyl) |> as_tibble() |> attr("foo")
#> NULL
# which looks like
dplyr:::as_tibble.grouped_df
#> function (x, ...)
#> {
#> new_tibble(dplyr_vec_data(x), nrow = nrow(x))
#> }
#> <bytecode: 0x7fa953f20e68>
#> <environment: namespace:dplyr> Created on 2023-03-16 with reprex v2.0.2.9000 |
ungroup()
drops data frame attributes
Interesting, I would have thought Lines 82 to 83 in 68ae922
even though |
Looking at tidyverse/tibble#769, while I understand the reasoning I don't think it's intuitive for |
Another test case for consideration: partial ungrouping does not drop the attribute: library(dplyr)
d = mtcars
attr(d, "foo") = "bar"
d |> group_by(cyl, am) |> ungroup(cyl) |>attr("foo")
#> [1] "bar"
d |> group_by(cyl, am) |> ungroup(am) |>attr("foo")
#> [1] "bar"
d |> group_by(cyl, am) |> ungroup(am, cyl) |> attr("foo")
#> NULL
d |> group_by(cyl, am) |> ungroup(am) |> ungroup(cyl) |> attr("foo")
#> NULL nor does ungrouping an ungrouped data frame:
Which feels funny to me, since the treatment of attributes differs based on if I'm dropping some groups or all groups. The core of the matter is that as a user I would generally expect the following to be TRUE: identical(
d |> group_by(...) |> ungroup(),
d
) Relacing the Lines 157 to 159 in 3b696db
becomes ungroup.grouped_df <- function(x, ...) {
if (missing(...)) {
new_tibble(x, groups = NULL) (and the same change would be needed for Test: d = as_tibble(mtcars)
attr(d, "foo") = "bar"
d |> group_by(cyl, am) |>tibble::new_tibble(groups = NULL) |> attr("foo")
#> [1] "bar"
identical(
d |> group_by(cyl, am) |>tibble::new_tibble(groups = NULL),
d
)
#> [1] TRUE |
This is referring to dataframe/tibble attributes, not tibble column attributes. The CRAN install of dplyr 1.1.0 sometimes drops attributes:
I can understand why it might make sense for
summarize()
to drop attributes (but then why doescount()
preserve them?), but I don't see whyungroup(group_by(...))
should drop attributes unilaterally. Given that almost alldplyr
verbs preserve the attribute, I'm wondering if this is a bug/regression.There are a variety of issues regarding attributes but they are all closed.
The text was updated successfully, but these errors were encountered: