Yuteng Zhang
Yuteng Zhang

Reputation: 13

Row name to be set of number

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.

Upvotes: 1

Views: 6470

Answers (1)

Dave2e
Dave2e

Reputation: 24069

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.

Upvotes: 6

Related Questions