198

I have a data frame like this:

df
              VALUE              ABS_CALL DETECTION P-VALUE    
    1007_s_at "957.729231881542" "P"      "0.00486279317241156"
    1053_at   "320.632701283368" "P"      "0.0313356324173416" 
    117_at    "429.842323161046" "P"      "0.0170004527476119" 
    121_at    "2395.7364289242"  "P"      "0.0114473584876183" 
    1255_g_at "116.493632746934" "A"      "0.39799368200131"   
    1294_at   "739.927122116896" "A"      "0.0668649772942343" 

I want to convert the row names into the first column. Currently I use something like this to make row names as the first column:

  d <- df
  names <- rownames(d)
  rownames(d) <- NULL
  data <- cbind(names,d)

Is there a single line to do this?

Agaz Wani
  • 5,080
  • 7
  • 41
  • 58
  • possible duplicate of [R: converting row names in multiple data frames to column in data frame](http://stackoverflow.com/questions/18403199/r-converting-row-names-in-multiple-data-frames-to-column-in-data-frame) – rmuc8 Apr 08 '15 at 09:50
  • 21
    You don't need extra packages, here's a one-liner: `d – ssp3nc3r Oct 06 '17 at 20:13
  • 1
    The comment by @ssp3nc3r should be an accepted answer – Hrant Mar 27 '19 at 12:16

9 Answers9

197

Or you can use tibble's rownames_to_column which does the same thing as David's answer:

library(tibble)
df <- tibble::rownames_to_column(df, "VALUE")

Note: The earlier function called add_rownames() has been deprecated and is being replaced by tibble::rownames_to_column()

Kevin Wright
  • 2,257
  • 22
  • 26
hrbrmstr
  • 74,560
  • 11
  • 127
  • 189
153

You can both remove row names and convert them to a column by reference (without reallocating memory using ->) using setDT and its keep.rownames = TRUE argument from the data.table package

library(data.table)
setDT(df, keep.rownames = TRUE)[]
#    rn     VALUE  ABS_CALL DETECTION     P.VALUE
# 1:  1 1007_s_at  957.7292         P 0.004862793
# 2:  2   1053_at  320.6327         P 0.031335632
# 3:  3    117_at  429.8423         P 0.017000453
# 4:  4    121_at 2395.7364         P 0.011447358
# 5:  5 1255_g_at  116.4936         A 0.397993682
# 6:  6   1294_at  739.9271         A 0.066864977

As mentioned by @snoram, you can give the new column any name you want, e.g. setDT(df, keep.rownames = "newname") would add "newname" as the rows column.

David Arenburg
  • 89,637
  • 17
  • 130
  • 188
102

A one line option is :

df$names <- rownames(df)
Jaap
  • 77,147
  • 31
  • 174
  • 185
Emily
  • 1,071
  • 1
  • 7
  • 3
36

Alternatively, you can create a new dataframe (or overwrite the current one, as the example below) so you do not need to use of any external package. However this way may not be efficient with huge dataframes.

df <- data.frame(names = row.names(df), df)
drasc
  • 479
  • 4
  • 7
23

Moved my comment into an answer per suggestion above:

You don't need extra packages, here's a one-liner:

d <- cbind(rownames(d), data.frame(d, row.names=NULL))
ssp3nc3r
  • 3,490
  • 2
  • 11
  • 23
8

dplyr::as_tibble(df, rownames = "your_row_name") will give you even simpler result.

Dima Lituiev
  • 11,752
  • 9
  • 35
  • 56
SteveS
  • 3,164
  • 2
  • 24
  • 43
  • 2
    @HectorHaffenden have edited this for the poster, because it's actually a nice suggestion. – tjebo Jul 26 '19 at 10:55
  • 3
    "`as_data_frame()` was deprecated in tibble 2.0.0. Please use `as_tibble()` instead." Otherwise this is my favourite. – Samuel Saari May 05 '21 at 16:05
3

Or by using DBIs sqlRownamesToColumn

library(DBI)
sqlRownamesToColumn(df)
Bappa Das
  • 5,817
  • 3
  • 18
  • 40
Agaz Wani
  • 5,080
  • 7
  • 41
  • 58
0
df = data.frame(columnNameILike = row.names(df), df)
Bappa Das
  • 5,817
  • 3
  • 18
  • 40
Yale Liu
  • 61
  • 1
  • 1
0

Change data rownames as a real column

data <- data %>%
  rownames_to_column(var="the name you want")
marianthi
  • 11
  • 3