Convert Row Names into Column of DataFrame in R - GeeksforGeeks

17 Nov, 2021
In this article, we will discuss how to Convert Row Names into Columns of Dataframe in R Programming Language. 
row.name() function is used to set and get the name of the DataFrame. Apply the row.name() function to the copy of the DataFrame and a name to the column which contains the name of the column with the help of the $ sign.
Syntax:
row.names(dataframe)
Example:
R
# DataFrame is created with the# help of data.frame functiondata <- data.frame(x1 = (10:15),                     x2 = (15:10)) # print the DataFramedata # make copy of DataFramecopydata <- data       # apply row.name function to get# the name of the rowcopydata$rn <- row.names(data)   # print the resultcopydata
Output:
In the R language there’s a package named dplyr which performs several DataFrame tasks. So, we are going to add a row name into a column of a DataFrame with the help of this package. At first, we are going to install and load the dplyr package. After loading the package we follow the same steps we followed in the first method but this time with the help of the function of dplyr library. The function used in this method is rownames_to_columns(). 
Syntax:
tibble::rownames_to_column(data_frame, column_name)
Example:
R
# DataFrame is created with the# help of data.frame functiondata <- data.frame(x1 = (10:15),                     x2 = (15:10)) # load the packagelibrary("dplyr") # make the copy of the data framecopydata <- data   # Apply rownames_to_column on the copy of# DataFrame and put name of function rncopydata <- tibble::rownames_to_column(copydata, "rn") # print the copied DataFramecopydata
Output:
In the R language there’s a package named data.table which performs several DataFrame tasks. So, we are going to add a row name into a column of a DataFrame with the help of this package. At first, we are going to install and load the data.table package. After loading the package we follow the same steps we followed in the first method but this time with the help of the function of data.table library. The function used in this method is setDT(). 
Syntax:
setDT(x, keep.rownames=TRUE/FALSE, key=NULL, check.names=TRUE/FALSE)
While using this syntax we keep the row name true to get the name of the row.
Example:
R
# DataFrame is created with the help# of data.frame functiondata <- data.frame(x1 = (10:15),                     x2 = (15:10)) # print the DataFramedata # load the librarylibrary("data.table") # make copy of dataframecopydata <- data          # Apply setDT function with rowname true# to get the rownamecopydata <- setDT(data, keep.rownames = TRUE)[]          # print the datacopydata 
Output:
prachisoda1234
sweetyty
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?
Replace Specific Characters in String in R
Convert Matrix to Dataframe in R