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 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
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
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.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you might want to have a look at the related tutorials of https://www.statisticsglobe.com/. Some related posts about data frame manipulation can be found below:
- Change Row Names of Data Frame or Matrix
- Don’t Display Data Frame Row Names
- Substitute Data Frame Row Names by Values in Vector
- Subset Data Frame and Matrix by Row Names
- Select Data Frame Rows where Column Values are in Range
- Find Index of Maximum & Minimum Value of Vector & Data Frame Row
- Count Number of Cases within Each Group of Data Frame
- R Programming Examples
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.
Statistics Globe Newsletter