229

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?

4

9 Answers 9

233

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()

3
  • 15
    Not exactly the same, because it's not doing it by reference :) Apr 8, 2015 at 10:09
  • 5
    UPDATE: dplyr update requires one to use tibble::rownames_to_column() since dplyr::rownames is deprecated.
    – EDennnis
    May 13, 2019 at 15:27
  • This is great if one wants to stay within the tidyverse Dec 28, 2019 at 15:11
161

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.

8
  • 5
    Use colnames(df)[1] <- "newname" to rename the first column if needed.
    – Swetabh
    Nov 16, 2016 at 5:24
  • 5
    @Swetabh Well, no. setnames(df, 1, "newname") is the data.table way. Nov 16, 2016 at 7:45
  • 1
    @DavidArenburg Well, (at least) now you can do it in the same call setDT(df, keep.rownames = "newname")[]
    – s_baldur
    Feb 5, 2019 at 11:11
  • 1
    @DavidArenburg found in documentation for as.data.table(): If TRUE, adds the input object's names as a separate column named "rn". keep.rownames = "id" names the column "id" instead
    – s_baldur
    Feb 5, 2019 at 11:35
  • 2
    @snoram good find, I'll make a PR regarding that to make docs consistent. Feb 5, 2019 at 11:38
118

A one line option is :

df$names <- rownames(df)
4
  • 22
    I hope you are aware of the fact that it adds rownames as a column at the last, indeed not as a first column.
    – Agaz Wani
    Mar 18, 2016 at 12:28
  • 2
    After to remove index use rownames(df) <- NULL
    – vasili111
    May 12, 2021 at 17:33
  • @vasili111 - could you explain what does it mean by 'to remove index' as I see no difference to the data table before (df$names <- rownames(df)) and after (rownames(df) <- NULL). Do you mean it stores the index column internally? Jun 29, 2022 at 21:18
  • @AravindanKalai Hi. rownames(df) <- NULL will remove original row names. I was referring to row names.
    – vasili111
    Jul 1, 2022 at 16:10
40

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)
1
  • 20
    Or: df <- cbind(names = rownames(df), df) Jun 13, 2017 at 20:47
31

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))
9

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

2
  • 2
    @HectorHaffenden have edited this for the poster, because it's actually a nice suggestion.
    – tjebo
    Jul 26, 2019 at 10:55
  • 3
    "as_data_frame() was deprecated in tibble 2.0.0. Please use as_tibble() instead." Otherwise this is my favourite. May 5, 2021 at 16:05
4

Change data rownames as a real column

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

Or by using DBIs sqlRownamesToColumn

library(DBI)
sqlRownamesToColumn(df)
3
df = data.frame(columnNameILike = row.names(df), df, row.names=NULL)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.