tidyverse / tibble

A modern re-imagining of the data frame

Home Page:https://tibble.tidyverse.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Should `[` keep row names?

DavisVaughan opened this issue · comments

It seems like both column and row subsetting with [ drop row names.

library(tibble)

cars <- as_tibble(mtcars, rownames = NA)

rownames(cars)
#>  [1] "Mazda RX4"           "Mazda RX4 Wag"       "Datsun 710"         
#>  [4] "Hornet 4 Drive"      "Hornet Sportabout"   "Valiant"            
#>  [7] "Duster 360"          "Merc 240D"           "Merc 230"           
#> [10] "Merc 280"            "Merc 280C"           "Merc 450SE"         
#> [13] "Merc 450SL"          "Merc 450SLC"         "Cadillac Fleetwood" 
#> [16] "Lincoln Continental" "Chrysler Imperial"   "Fiat 128"           
#> [19] "Honda Civic"         "Toyota Corolla"      "Toyota Corona"      
#> [22] "Dodge Challenger"    "AMC Javelin"         "Camaro Z28"         
#> [25] "Pontiac Firebird"    "Fiat X1-9"           "Porsche 914-2"      
#> [28] "Lotus Europa"        "Ford Pantera L"      "Ferrari Dino"       
#> [31] "Maserati Bora"       "Volvo 142E"

# Column subsetting drops row names
rownames(cars[1])
#>  [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13" "14" "15"
#> [16] "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30"
#> [31] "31" "32"

# Row subsetting drops row names
rownames(cars[1,])
#> [1] "1"

Do we want to keep them if they exist? This came up in tidyverse/dplyr#6290 since dplyr uses [.

I'd like to push people away from row names on tibbles, so I'm perfectly happy if the answer is no.

I'd also be happy if we started actively discouraging as_tibble(rownames = NA) since row names aren't actually supported in tibble (as shown by this issue)

Maybe look into soft deprecating rownames = NA in the next major release of tibble.

I think this is the only place where we allow users to keep row names in tibble, and then we immediately destroy them in the next tibble operation. I think that is a little inconsistent, so I'd rather us just push people to promote their row names to real columns instead.

@krlmlr and I both agree that we don't want [ to keep row names

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.