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

Issue with grouped operations and attributes #2773

Closed
Enchufa2 opened this Issue May 14, 2017 · 4 comments

Comments

Projects
None yet
3 participants
@Enchufa2

Enchufa2 commented May 14, 2017

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"
@lionel-

This comment has been minimized.

Member

lionel- commented May 15, 2017

Aggregation of attributes is a really difficult issue because we don't have any information about how to do it. Some attributes are vectorised, some aren't. I'm surprised that the attributes of the original vector are preserved at all actually.

@Enchufa2

This comment has been minimized.

Enchufa2 commented May 15, 2017

This could work if the subsetting of grouped data were done by calling R's subsetting operator (at least when there are attributes involved). But even if I define the operator [.foo, the example doesn't work, so I suppose that this is done internally, at C++ level. Any chance of having support for this?

@lionel-

This comment has been minimized.

Member

lionel- commented May 15, 2017

There's the issue of subsetting and there's the issue of aggregating all the data back to one vector. We need C-level genericity of vectors and we have plans regarding this, but it's not a trivial issue.

@hadley

This comment has been minimized.

Member

hadley commented May 15, 2017

This is on our long term to do list.

@hadley hadley closed this May 15, 2017

@lock lock bot locked as resolved and limited conversation to collaborators Jun 8, 2018

@tidyverse tidyverse unlocked this conversation Jun 22, 2018

@tidyverse tidyverse locked and limited conversation to collaborators Jun 22, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.