Closed
Description
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
Labels
No labels