user4400727
user4400727

Reputation:

converting rownames back to column format

I want to convert rownames in my dataframe into first column

Example intput:

                                        y
species1                         3.783584
species2                         3.696341
species3                         3.968285

Desired output:

       x                                y
species1                         3.783584
species2                         3.696341
species3                         3.968285

Upvotes: 1

Views: 40

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

Just assign the row names to a new column:

df$x <- rownames(df)
df <- df[,c("x", "y")]

The second step is only necessary if, for some reason, you care about column order.

If you also want to revert the row names to the numerical sequence they would have by default, you may do so via:

rownames(df) <- seq(nrow(df))

Upvotes: 1

Hunaidkhan
Hunaidkhan

Reputation: 1418

you can do it using

df$x <- rownames(df)

Upvotes: 1

Related Questions