Skip to content

tibble preserves names attribute of added vector in column (and as.data.frame preserves as well) #837

Description

I wonder whether a tibble should preserve a vector's names attribute when added as a column or not. A data frame drops this attribute when a column is added.

?tibble might contain a hint that this is intended behaviour, but it is not clear from that ("tibble() is much lazier than base::data.frame() in terms of transforming the user's input. Character vectors are not coerced to factor. List-columns are expressly anticipated and do not require special tricks. Column names are not modified.").

See the example below about the difference in behaviour.

I would "vote" mimicing base R data frame's behaviour here.

At least when converting back to data frame via as.data.frame.tbl_df the names attribute should be pruned from columns.

library(tibble)
a <- 1:5
b <- 6:10
names(b) <- letters[1:5]

df <- data.frame(a, a * 2)
tb <- tibble(df)

df$b <- b
tb$b <- b

lapply(df, names) # data frame's columns do not have names
#> $a
#> NULL
#> 
#> $a...2
#> NULL
#> 
#> $b
#> NULL
lapply(tb, names) # tibble's column b preserved names
#> $a
#> NULL
#> 
#> $a...2
#> NULL
#> 
#> $b
#> [1] "a" "b" "c" "d" "e"

df_tb <- as.data.frame(tb)
lapply(df_tb, names) # data frame's column b has names - illegal data frame?
#> $a
#> NULL
#> 
#> $a...2
#> NULL
#> 
#> $b
#> [1] "a" "b" "c" "d" "e"

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions