Skip to content

as_tibble.data.frame() treats attr()-type attributes inconsistently: sripts "n" but not others. #1573

@krivit

Description

@krivit

as_tibble.data.frame() appears to strip some custom attributes. So far, I've only noticed this for n, but there may be others. I think it should either strip all the non-standard attributes or preserve them all, and document the behaviour either way.

library(tibble)
df <- data.frame(x=1:2, y=3:4)
attr(df, "n") <- 5
attr(df, "m") <- 6

attributes(df) # data.frame has m and n attributes.
#> $names
#> [1] "x" "y"
#> 
#> $class
#> [1] "data.frame"
#> 
#> $row.names
#> [1] 1 2
#> 
#> $n
#> [1] 5
#> 
#> $m
#> [1] 6

attributes(as_tibble(df)) # tibble has m but not n.
#> $class
#> [1] "tbl_df"     "tbl"        "data.frame"
#> 
#> $row.names
#> [1] 1 2
#> 
#> $names
#> [1] "x" "y"
#> 
#> $m
#> [1] 6

Created on 2024-04-02 with reprex v2.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions