Convert Row Names into Column of Data Frame in R (Example)
This tutorial illustrates how to use the row names of a data frame as variable in R.
The content of the page looks as follows:
- Creation of Example Data
- Example 1: Convert Row Names to Column with Base R
- Example 2: Convert Row Names to Column with dplyr Package
- Example 3: Convert Row Names to Column with data.table Package
- Video, Further Resources & Summary
Here’s how to do it…
Creation of Example Data
We’ll use the following data frame in R as basement for this R programming tutorial:
data <- data.frame(x1 = LETTERS[1:5], # Create example data x2 = letters[5:1]) data # Print example data # x1 x2 # 1 A e # 2 B d # 3 C c # 4 D b # 5 E a
Our example data contains five rows and two columns. The row names of our data are ranging from 1 to 5.
In the following example, I’ll explain how to convert these row names into a column of our data frame. Let’s jump right into it!
Example 1: Convert Row Names to Column with Base R
Example 1 shows how to add the row names of a data frame as variable with the basic installation of the R programming language (i.e. without any add-on packages). Have a look at the following R code:
data1 <- data # Duplicate example data data1$row_names <- row.names(data1) # Apply row.names function data1 # Print updated data # x1 x2 row_names # 1 A e 1 # 2 B d 2 # 3 C c 3 # 4 D b 4 # 5 E a 5
As you can see based on the output of the RStudio console, we added a new column called row_names to our data frame by using the row.names function.
Example 2: Convert Row Names to Column with dplyr Package
In the R programming language, you usually have many different alternatives to do the data manipulation you want. Many people prefer to use the dplyr package for their data manipulation tasks. For that reason, I’m going to show you in this example how to convert row names to a column with the dplyr package.
Let’s install and load the package:
install.packages("dplyr") # Install & load dplyr library("dplyr")
Now, we can use the rownames_to_column function to add the row names of our data as variable:
data2 <- data # Duplicate example data data2 <- tibble::rownames_to_column(data2, "row_names") # Apply rownames_to_column data2 # Print updated data # row_names x1 x2 # 1 1 A e # 2 2 B d # 3 3 C c # 4 4 D b # 5 5 E a
Note that the rownames_to_column command adds the row_names column at the first index position of our data frame (in contrast to our R syntax of Example 1).
Example 3: Convert Row Names to Column with data.table Package
Another popular R package for data manipulation is the data.table package. Let’s install and load data.table to RStudio:
install.packages("data.table") # Install & load data.table library("data.table")
Now, we can apply the setDT function as shown below:
data3 <- data # Duplicate example data data3 <- setDT(data3, keep.rownames = TRUE) # Apply setDT function data3 # Print updated data # rn x1 x2 # 1: 1 A e # 2: 2 B d # 3: 3 C c # 4: 4 D b # 5: 5 E a
Video, Further Resources & Summary
If you need more explanations on the R programming codes of this tutorial, you might have a look at the following video of my YouTube channel. In the video, I’m explaining the topics of this tutorial:
In addition, you could read the other articles of this website:
- Introduction to dplyr in R
- Introduction to data.table in R
- Add New Column to Data Frame in R
- Convert Data Frame Column to Vector in R
- The R Programming Language
In summary: This article explained how to transform row names to a new explicit variable in the R programming language. If you have any further questions, tell me about it in the comments section below.