Skip to content

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

Description

@jangorecki

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

Activity

  1. added this to the 1.16.0 milestone on Dec 9, 2023
  2. modified the milestones: 1.16.0, 1.17.0 on Jul 11, 2024
  3. iagogv3 commented on Nov 19, 2024

    @iagogv3
    Contributor

    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...

  4. MichaelChirico commented on Jan 17, 2025

    @MichaelChirico
    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.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions