Change Index Numbers of Data Frame Rows in R (2 Examples)


This article shows how to modify or reset all index numbers of a data frame in R.

The article contains two examples for the modification of indices. More precisely, the tutorial consists of this content:

Let’s start right away.


Creation of Example Data

The following data will be used as basement for this tutorial:

data  <- data.frame(x1 = 9:5,               # Create example data
                    x2 = letters[5:9],
                    x3 = "c")
rownames(data) <- c(4, 1, 5, 3, 2)
data                                        # Print example data


table 1 data frame change index numbers data frame rows r


Table 1 shows the structure of our example data – It is composed of five rows and three variables.

As you can see, the index numbers of our data frame are mixed up. In the following examples, I’ll explain how to clean these row names in R.


Example 1: Assign Sequence of Consecutive Index Numbers to Data Frame Rows Using nrow() Function

This example illustrates how to assign new index numbers to our data frame rows using the nrow function.

Have a look at the following R code:

data_new1 <- data                           # Duplicate data
rownames(data_new1) <- 1:nrow(data_new1)    # Assign sequence to row names
data_new1                                   # Print updated data


table 2 data frame change index numbers data frame rows r


As shown in Table 2, the previous code has created a new data frame where the index numbers are ordered from 1 to the number of lines.

Please note that we could apply the same R code to a matrix object instead of a data frame object as well.


Example 2: Reset Index Numbers of Data Frame Rows Using NULL

Example 2 explains how to clear the data frame index numbers by assigning NULL to our row names.

Check out the following syntax:

data_new2 <- data                           # Duplicate data
rownames(data_new2) <- NULL                 # Reset row names
data_new2                                   # Print updated data


table 3 data frame change index numbers data frame rows r


As shown in Table 3, we have created another data frame with index numbers ranging from 1 to N. However, this time we have used NULL instead of the nrow function.


Video, Further Resources & Summary

I have recently released a video on my YouTube channel, which shows the R codes of this article. You can find the video below.



In addition, you might want to have a look at the related tutorials of Some related posts about data frame manipulation can be found below:


Summary: On this page you have learned how to adjust and set data frame indices in the R programming language. Don’t hesitate to let me know in the comments, in case you have any additional comments or questions.


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.

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.