Reset Sequence or Row Numbers in an R Data Frame

When rows are filtered, removed, or reordered in a base R data frame, the displayed row names may keep their original values. For example, a filtered result may show row names 1 and 4 instead of a continuous sequence.

To reset the displayed row numbers, assign new row names after creating the subset. The simplest base R approach is rownames(dataframe) <- NULL. You can also assign an explicit sequence with seq_len(nrow(dataframe)).

Why Row Numbers Become Non-Sequential After Filtering

A base R data frame stores row names as an attribute. Subsetting preserves those row names, even when some rows are excluded. The values shown at the left of the data frame are therefore row names, not a separate data column.

Example 1 – Reset Row Numbers in R Data Frame

Consider a Dataframe DF1 shown below.

</>
Copy
> DF1 = data.frame(x = c(9, NA, 7, 4), y = c(4, NA, NA, 21))
> DF1
   x  y
1  9  4
2 NA NA
3  7 NA
4  4 21

In this original dataframe, the row numbers are ordered from 1 to 4.

Let us filter the rows of this dataframe that do not contain any NAs.

</>
Copy
> resultDF = DF1[complete.cases(DF1), ]
> resultDF
  x  y
1 9  4
4 4 21

The second and third rows are trashed out and only rows numbered one and four got into the filtered output dataframe. But the row numbers are not in a sequence.

We need the rows of resultDF to be numbered in sequence without missing any numbers. We will set the rownames with a sequence of numbers with a length equal to number of rows in the dataframe.

</>
Copy
> rownames(resultDF) = seq(length=nrow(resultDF))
> resultDF
  x  y
1 9  4
2 4 21

The row names are now displayed as a continuous sequence.

Reset R Data Frame Row Names with NULL

For a base R data frame, assigning NULL to rownames() restores automatic row names. This is usually clearer than manually calculating a sequence.

</>
Copy
resultDF <- DF1[complete.cases(DF1), ]
rownames(resultDF) <- NULL
resultDF
  x  y
1 9  4
2 4 21

Assign Sequential Row Names with seq_len()

When you specifically want character row names based on the current number of rows, use seq_len(nrow(resultDF)). Unlike expressions such as 1:nrow(resultDF), seq_len() also behaves correctly when the data frame has zero rows.

</>
Copy
rownames(resultDF) <- seq_len(nrow(resultDF))

Add an Explicit Row Number Column in Base R

Resetting row names only changes the labels displayed beside the data. If the sequence must remain part of the dataset for sorting, exporting, joining, or analysis, add a normal column instead.

</>
Copy
resultDF$row_number <- seq_len(nrow(resultDF))
resultDF
  x  y row_number
1 9  4          1
2 4 21          2

An explicit column is preferable when the number has meaning beyond display. Row names can be dropped or changed by some import, export, and data-transformation operations.

Create Sequential Row Numbers with dplyr

In a dplyr workflow, use row_number() inside mutate() to create a regular column after filtering.

</>
Copy
library(dplyr)

resultDF <- DF1 |>
  filter(if_all(everything(), ~ !is.na(.x))) |>
  mutate(row_number = row_number())

This creates a sequential column in the current row order. It does not modify base R row names.

Common Mistakes When Resetting R Row Numbers

  • Using 1:nrow(df) when the data frame may be empty. Prefer seq_len(nrow(df)).
  • Assuming row names are a real column. They are an attribute unless you explicitly create a column.
  • Resetting row names before filtering. Apply the reset after the final subset or reorder operation.
  • Using row names as permanent identifiers. Store stable IDs in a dedicated column instead.

FAQs About Resetting Row Numbers in R

How do I reset row numbers after removing rows in R?

After removing rows, run rownames(df) <- NULL. Base R will restore automatic row names beginning at 1.

Does resetting row names change the data values?

No. It changes only the row-name attribute. The values and columns in the data frame remain unchanged.

How do I turn row numbers into a data frame column?

Use df$row_number <- seq_len(nrow(df)). This stores the sequence as an ordinary column that can be exported and manipulated.

Why do tibbles not show row names like base R data frames?

Tibbles generally use columns rather than row names for identifiers. Add a row-number column with dplyr::row_number() when a visible sequence is required.

Editorial QA Checklist for R Row Number Reset Examples

  • Confirm that the filtered example preserves non-sequential row names before the reset.
  • Verify that rownames(df) <- NULL produces automatic row names starting at 1.
  • Use seq_len(nrow(df)) instead of 1:nrow(df) in newly added examples.
  • Distinguish clearly between row names and an explicit row-number column.
  • Check that all R and output blocks use the correct PrismJS classes.

Summary of R Data Frame Row Number Reset Methods

Use rownames(df) <- NULL to restore automatic row names in a base R data frame. Use seq_len(nrow(df)) when assigning a sequence explicitly, and create a normal column when the row number must be retained as data. In this R Tutorial, we have learned how to reset the sequence of row numbers after filtering or subsetting a data frame.