Dplyr: filter() discards rownames

Created on 2 Apr 2014  路  8Comments  路  Source: tidyverse/dplyr

Hello,

filter() does discard rownames, e.g.:

rownames(filter(mtcars, cyl == 4))
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11"
rownames(mtcars[mtcars$cyl == 4, ])
[1] "Datsun 710" "Merc 240D" "Merc 230" "Fiat 128"
[5] "Honda Civic" "Toyota Corolla" "Toyota Corona" "Fiat X1-9"
[9] "Porsche 914-2" "Lotus Europa" "Volvo 142E"

Is it on purpose, a limitation or planned ?
I could not find any explanation in the documentation.

Thanks
Karl

Most helpful comment

keep row names is really important

All 8 comments

Yes, all dplyr methods ignore rownames.

Thanks, but that does not completely answer the question: is-it on purpose, or something you may consider adding ?

On purpose. They're hard to get right in general (esp because of uniqueness criterion) and don't allow you to do anything that you can't otherwise do.

Sometimes there is useful information in the rownames however. Arguably it should already be part of the data, but how about adding a draw_rownames function as a short cut for people to do this?
Something like:

draw_rownames <- function(.data) .data %>% do(mutate(.,rownames=rownames(.)))

mtcars %>% draw_rownames() %>% filter(carb==6)
   mpg cyl disp  hp drat   wt qsec vs am gear carb     rownames
1 19.7   6  145 175 3.62 2.77 15.5  0  1    5    6 Ferrari Dino

Is it possible to include a flag to instruct dplyr functions to include rownames?

No, sorry.

keep row names is really important

Use subset() instead ...

Was this page helpful?
0 / 5 - 0 ratings