1

I want to change the row names from abcde to 12345 For example I would like to convert:

     Var.1 Var.2 Var.3
 A     1     5     0
 B     2     4     1
 C     3     3     2
 D     4     2     3
 E     5     1     4

into

       names Var.1 Var.2 Var.3
  1     A     1     5     0
  2     B     2     4     1
  3     C     3     3     2
  4     D     4     2     3
  5     E     5     1     4

The example is from another question, but I ask reversely.

lmo
  • 37,904
  • 9
  • 56
  • 69
Yuteng Zhang
  • 13
  • 1
  • 4
  • Possible duplicate of [Convert the values in a column into row names in an existing data frame in R](http://stackoverflow.com/questions/5555408/convert-the-values-in-a-column-into-row-names-in-an-existing-data-frame-in-r) – erc Feb 26 '16 at 19:29
  • Yes we are similar but reverse the question – Yuteng Zhang Feb 26 '16 at 19:37

1 Answers1

5

Try using the "rownames" command:

rownames(mydataframe)<-c(1:5)

To add the current rownames as a new column to the dataframe:

cbind(names = rownames(mydataframe), mydataframe, row.names = NULL)

This edit includes the suggestion to make the function call generic.

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
Dave2e
  • 22,192
  • 18
  • 42
  • 50