Skip to content
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
Closed

Issue with grouped operations and attributes #2773

Enchufa2 opened this issue May 14, 2017 · 4 comments

Comments

@Enchufa2
Copy link

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-
Copy link
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
Copy link
Author

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-
Copy link
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
Copy link
Member

hadley commented May 15, 2017

This is on our long term to do list.

@hadley hadley closed this as completed 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.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants