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

as.data.table should remove extra attributes from extended data.frames #5699

Closed
jangorecki opened this issue Oct 4, 2023 · 2 comments · Fixed by #5700
Closed

as.data.table should remove extra attributes from extended data.frames #5699

jangorecki opened this issue Oct 4, 2023 · 2 comments · Fixed by #5700
Milestone

Comments

@jangorecki
Copy link
Member

jangorecki commented Oct 4, 2023

Spotted when investigating #5698

x = iris
class(x) = c("tbl","data.frame")
attr(x, "t1") = "a"
y = as.data.table(x)
attr(y, "t1")
#[1] "a"       ## should be NULL
class(y)
#[1] "data.table" "data.frame"

We should assume creator of custom class provides correct data.frame method that clear any extra attributes

as.data.frame.tbl = function(x) {
  attr(x, "t1") = NULL
  class(x) = "data.frame"
  x
}
z = as.data.frame(x)
attr(z, "t1")
#NULL

So then consistency can be retained, as described in #5698

@iagogv3
Copy link
Contributor

iagogv3 commented Nov 19, 2024

Unmotivating the use of data.table...

Many times, one has to use haven to read data with SPSS, Stata or excel formats, which obviously produces tibbles. Fortunately, they can be transformed to data.table's and, for the moment, these transformations keep attributes, particularly variable labels. IMO, removing that information is not a big thing.

Although I use setDT instead of as.data.table...

@MichaelChirico
Copy link
Member

I think that removal of attributes by as.* methods is the norm in R... users wishing to retain attributes should do so explicitly.

It's somewhat inconvenient but I think it's more the expected behavior from experienced R users.

MichaelChirico added a commit that referenced this issue Jan 17, 2025
…table (#5700)

* custom data.frame classes redirected to as.data.frame before as.data.table, #5699

* Missing call spotted by Michael

* amend test to work in/out of cc()

* add NEWS

---------

Co-authored-by: Michael Chirico <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants