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:

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:

 

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


7 Comments. Leave new

  • Hi Joachim,

    Thanks for the post. Iam wondering how can I give a specific name for the variable row_names instead of 1, 2, 3, 4, 5 please. For example, in my dataset I need to povide a name called ‘Forest’ for all rows until the end.

    data1 <- data
    data1$row_names <- row.names(data1)
    data1
    # x1 x2 row_names
    # 1 A e 1
    # 2 B d 2
    # 3 C c 3
    # 4 D b 4
    # 5 E a 5

    For example, I meant this way…

    # x1 x2 row_names
    # 1 A e Forest
    # 2 B d Forest
    # 3 C c Forest
    # 4 D b Forest
    # 5 E a Forest

    Reply
  • Hi Cansu,

    Yes, absoloutely. I am wodering how can I put “Forest” in those rows.

    Additionally, I wondering it is possible to replace the value “1” (this value is currently present in the coloumn row_names) with the “Forest” in the coloumn row_names.

    I will be happy if you can sort this either ways!

    Thanks

    Reply
    • Hello Ratheesh John,

      If I get you well, you want two distinct things. One is to update a certain row, where the value is 1 and the other is to add a new variable with all rows equal to Forest. For the first operation, you should do the following.

      data <- data.frame(ID = 1:5, x = rnorm(5))
      data
      #   ID          x
      # 1  1 -0.7553115
      # 2  2 -0.7323388
      # 3  3  0.5159656
      # 4  4  0.1939738
      # 5  5  0.8964893
       
      class(data$ID)
      # [1] "integer"
       
      data$ID <- ifelse(data$ID == 1, "Forest", data$ID)
      data
      #      ID          x
      # 1 Forest -0.7553115
      # 2      2 -0.7323388
      # 3      3  0.5159656
      # 4      4  0.1939738
      # 5      5  0.8964893
       
      class(data$ID)
      # [1] "character"

      Please be aware that the class of the ID column would be converted to a character after replacing 1 with Forest. For the latter one, you can create a new variable as follows.

      data2 <- data.frame(x = rnorm(5))
      data2
      #           x
      # 1 0.2251617
      # 2 0.7358427
      # 3 0.4949946
      # 4 0.6476031
      # 5 1.7458042
       
      data2$new_var <- rep("Forest", nrow(data2))
      data2
      #           x new_var
      # 1 0.2251617  Forest
      # 2 0.7358427  Forest
      # 3 0.4949946  Forest
      # 4 0.6476031  Forest
      # 5 1.7458042  Forest

      I am not sure how you will utilize these data settings; therefore, I suspect that I misunderstood your question. If so, please give some more details.

      Bets,
      Cansu

      Reply
  • Cansu,

    For example, I have a coloumn called “ID” and I wanted to replace the value “1” of it with character “Forest”.
    geometry ID
    1 POLYGON ((15.5219 -92.7997,… 1
    2 POLYGON ((19.08333 -96.9667… 1
    3 POLYGON ((25.5699 -101.28, … 1

    Reply
  • Hi Cansu,

    Perfect, exactly you got it!.
    Thanks very much for this.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top