-1

I want to get the row names of sorted dataframe while using arrange(), for e.g.

library(dplyr)
head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

After I use arrange function

head(arrange(mtcars,disp))
   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
1 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
2 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
3 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
4 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
5 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
6 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1

Is there any way by which row names can be seen when using arrange()?

deepesh
  • 1,470
  • 2
  • 10
  • 31
  • Not sure what you're asking. What's wrong with `colnames(mtcars)`? – Maurits Evers Jul 06 '18 at 12:05
  • @AntoniosK yes it exactly what I want – deepesh Jul 06 '18 at 12:06
  • 2
    @AntoniosK Ah sorry; should've read more carefully; thanks for clarifying! You can turn row names into a column with `mtcars %>% rownames_to_column("row") %>% arrange(...)` – Maurits Evers Jul 06 '18 at 12:07
  • I want that when the dataframe gets sorted, then accordingly the row names should also changed – deepesh Jul 06 '18 at 12:08
  • You can arrange rows according to row names with `mtcars %>% rownames_to_column("row") %>% arrange(row)`. Is that what you're after? – Maurits Evers Jul 06 '18 at 12:10
  • ...you can use `column_to_rownames()` after what @MauritsEvers posted, if you need to. It's from `tibble` package. – AntoniosK Jul 06 '18 at 12:11
  • 2
    If you don't mind, you could also use `base R`'s `order()` as in `mtcars[order(mtcars$disp), ]` – markus Jul 06 '18 at 12:13
  • @markus if you could mention this in the answer section, so that I could upvote it. Thanks – deepesh Jul 06 '18 at 12:14
  • 1
    @deepesh Glad it helped. It is not exactly what you asked for but in the post [How to sort a dataframe by multiple column(s)?](https://stackoverflow.com/questions/1296646/how-to-sort-a-dataframe-by-multiple-columns/6871968#6871968) you'll find plenty ways to sort a data.frame in R. – markus Jul 06 '18 at 12:18

2 Answers2

1

From the help pages of arrange:

When applied to a data frame, row names are silently dropped. To preserve, convert to an explicit variable with tibble::rownames_to_column()

Henrik
  • 56,228
  • 12
  • 124
  • 139
MSW Data
  • 421
  • 3
  • 8
1

Probably this:

mtcars$names <- rownames(mtcars)

and then arrange the table

Maylo
  • 592
  • 5
  • 16
  • Got my answer...But still just curious ,can't these row names change according to the sort, I mean is that necessary to include a 'names' column ? – deepesh Jul 06 '18 at 12:11
  • @deepesh this looks like a known issue when using `arrange`. Check: https://github.com/tidyverse/dplyr/issues/3058 – AntoniosK Jul 06 '18 at 12:17
  • @deepesh unfortunately dplyr discards the row names by default when using 'arrange'. Don't know why – Maylo Jul 06 '18 at 12:18