Convert row names into first column
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()
A one line option is :
df$names <- rownames(df)
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.