Assume that we have the following data frame and we want to set the row names as an extra column in the data frame.
library(dplyr) df<-data.frame(A=c(1,2,3), B=c(10,20,30)) row.names(df)<-c("A","B","C") df
We can work with the dplyr
library and the rownames_to_column
function as follows:
df<-df%>%rownames_to_column("NewColumn") df
As we can see, we set a new column from the row names, and we called it “NewColumn”. Now, the row names take the default values such as 1, 2, 3, ..