Skip to content

Issue with grouped operations and attributes #2773

Description

@Enchufa2

Consider the following custom S3 class with an attribute:

x <- 1:10
class(x) <- "foo"
attr(x, "foo") <- 1:10

in which the length of the attribute must coincide with the vector. I define the following S3 methods:

as.data.frame.foo <- as.data.frame.numeric

mean.foo <- function(x, ...) {
  foo <- mean(attr(x, "foo"))
  structure(NextMethod(), "foo" = foo, class = "foo")
}

where mean summarises the vector as well as the attribute. Then,

data.frame(x=x) %>%
  summarise_all(mean) %>%
  .$x
# [1] 5.5
# attr(,"foo")
# [1] 5.5
# attr(,"class")
# [1] "foo"

Ok, but

data.frame(x=x, y=rep(c("A", "B"), each=5)) %>%
  group_by(y) %>%
  summarise_all(mean) %>%
  .$x
# [1] 3 8
# attr(,"foo")
# [1] 5.5              # <- I'd expect: 3 8
# attr(,"class")
# [1] "foo"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions