Often the row numbers of your data frame in R may not be numbered in a sequential order and you may need to reset them.

Here are the most common ways to do so in R:

**Method 1: Reset Row Numbers Using NULL**

rownames(df) <- NULL

This method sets the row names equal to **NULL**, which effectively removes the current row names from the data frame.

The data frame then is forced to use the default row names, which are numbered from 1 to n where n is the number of rows in the data frame.

**Method 2: Reset Row Numbers Using nrow()**

rownames(df) <- 1:nrow(df)

This method sets the row names equal to the range of values from 1 to **nrow()** of the data frame where **nrow()** extracts the total number of rows in the data frame.

**Method 3: Reset Row Numbers Using dplyr**

library(dplyr) df<- df %>% as.data.frame(row.names = 1:nrow(.))

This method uses the **row.names** function from the **dplyr** package along with the **nrow()** function to reset the row numbers of the data frame to range from 1 to n.

The following examples show how to use each method in practice with the following data frame in R:

#create data frame df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'), points=c(99, 68, 86, 88, 95, 74, 78, 93), assists=c(22, 28, 31, 35, 34, 45, 28, 31), rebounds=c(30, 28, 24, 24, 30, 36, 30, 29)) #specify row numbers rownames(df) <- c(4, 3, 7, 6, 1, 2, 8, 5) #view data frame df team points assists rebounds 4 A 99 22 30 3 A 68 28 28 7 A 86 31 24 6 A 88 35 24 1 B 95 34 30 2 B 74 45 36 8 B 78 28 30 5 B 93 31 29

Let’s jump in!

**Example 1: Reset Row Numbers Using NULL**

One way to reset the row numbers of the data frame named **df** is by setting the **rownames** equal to **NULL** as follows:

#reset row numbers rownames(df) <- NULL #view updated data frame df team points assists rebounds 1 A 99 22 30 2 A 68 28 28 3 A 86 31 24 4 A 88 35 24 5 B 95 34 30 6 B 74 45 36 7 B 78 28 30 8 B 93 31 29

When we view the updated data frame, we can see that the row numbers are reset and now range from 1 to 8.

**Example 2: Reset Row Numbers Using nrow()**

Another way to reset the row numbers of the data frame named **df** is by setting the **rownames** equal to the range from **1** to **nrow()** as follows:

#reset row numbers rownames(df) <- 1:nrow(df) #view updated data frame df team points assists rebounds 1 A 99 22 30 2 A 68 28 28 3 A 86 31 24 4 A 88 35 24 5 B 95 34 30 6 B 74 45 36 7 B 78 28 30 8 B 93 31 29

When we view the updated data frame, we can see that the row numbers are reset and now range from 1 to 8.

**Example 3: Reset Row Numbers Using dplyr**

Another way to reset the row numbers of the data frame named **df** is by using the **row.names** function from the **dplyr** package as follows:

library(dplyr) #reset row numbers df<- df %>% as.data.frame(row.names = 1:nrow(.)) #view updated data frame df team points assists rebounds 1 A 99 22 30 2 A 68 28 28 3 A 86 31 24 4 A 88 35 24 5 B 95 34 30 6 B 74 45 36 7 B 78 28 30 8 B 93 31 29

When we view the updated data frame, we can see that the row numbers are reset and range from 1 to 8.

Feel free to use whichever method you prefer to reset the row numbers in your own data frame.

**Additional Resources**

The following tutorials explain how to perform other common tasks in R:

How to Retrieve Row Numbers in R

How to Replace NAs with Strings in R

How to Impute Missing Values in R