Delete Rows from an R Data Frame

In this tutorial, you will learn how to delete one or more rows from a data frame in R. The examples cover removal by row number, column value, multiple conditions, row name, zero values, and missing values.

In R, removing rows usually means creating a subset that excludes the unwanted observations. Assign the subset back to the original variable when you want to replace the existing data frame.

R Syntax to Remove Rows by Index

To remove rows by position, place their row numbers inside a negative index vector:

</>
Copy
 mydataframe[-c(row_index_1, row_index_2),]

where

  • mydataframe is the data frame.
  • row_index_1, row_index_2, ... are the positions of the rows to exclude.
  • The empty position after the comma means that all columns must be retained.

Important: Include the comma after the negative row index. Without it, R may interpret the index as a column selection and you may end up deleting columns of the data frame instead of rows.

Example 1: Delete One Row from an R Data Frame

First, create a data frame named DF1.

</>
Copy
> DF1 = data.frame(V1= c(1, 5, 14, 23, 54), V2= c(9, 15, 85, 3, 42), V3= c(9, 7, 42, 87, 16))
> DF1
  V1 V2 V3
1  1  9  9
2  5 15  7
3 14 85 42
4 23  3 87
5 54 42 16
>

To delete the second row, use -2, or equivalently -c(2), in the row position. The result is assigned to DF2.

</>
Copy
> DF2 = DF1[-c(2),]
> DF2
  V1 V2 V3
1  1  9  9
3 14 85 42
4 23  3 87
5 54 42 16
>

The observation at row position 2 is excluded. Notice that the displayed row names remain 1, 3, 4, and 5; they are labels and are not automatically renumbered.

Example 2: Delete Multiple Rows from an R Data Frame

Create the same sample data frame for the multiple-row example.

</>
Copy
> DF1 = data.frame(V1= c(1, 5, 14, 23, 54), V2= c(9, 15, 85, 3, 42), V3= c(9, 7, 42, 87, 16))
> DF1
  V1 V2 V3
1  1  9  9
2  5 15  7
3 14 85 42
4 23  3 87
5 54 42 16
>

To remove the second and fourth rows, pass their positions as c(2, 4) and negate the vector.

</>
Copy
> DF2 = DF1[-c(2, 4),]
> DF2
  V1 V2 V3
1  1  9  9
3 14 85 42
5 54 42 16
>

The resulting data frame contains only the first, third, and fifth observations.

Remove Rows from an R Data Frame Based on a Column Value

Row positions are useful when the exact indices are known. In data-cleaning tasks, it is usually safer to keep or remove rows according to values in a column.

</>
Copy
employees <- data.frame(
  name = c("Asha", "Ben", "Chen", "Divya"),
  department = c("Sales", "Support", "Sales", "Finance"),
  score = c(82, 64, 91, 73)
)

employees_without_sales <- employees[employees$department != "Sales", ]

The logical expression is TRUE for rows whose department is not Sales. Only those rows are retained.

   name department score
2   Ben    Support    64
4 Divya    Finance    73

Delete R Data Frame Rows Using Multiple Conditions

Combine comparisons with & for AND or | for OR. The following example removes rows where the department is Sales and the score is below 90:

</>
Copy
remove_row <- employees$department == "Sales" & employees$score < 90
result <- employees[!remove_row, ]

The expression first identifies rows that meet both removal conditions. The ! operator reverses the result so that all other rows are retained.

   name department score
2   Ben    Support    64
3  Chen      Sales    91
4 Divya    Finance    73

Remove Rows with Certain Values Using dplyr filter()

When using the dplyr package, filter() keeps rows for which the supplied condition is true. To remove a value, write the condition for the rows that should remain.

</>
Copy
library(dplyr)

employees_without_sales <- employees %>%
  filter(department != "Sales")

Multiple removal conditions can be written inside filter(). This example excludes rows where the department is Sales and the score is below 90:

</>
Copy
result <- employees %>%
  filter(!(department == "Sales" & score < 90))

Remove Rows with Zero in a Specific R Data Frame Column

To delete rows containing zero in one specific column, retain rows whose value in that column is not zero:

</>
Copy
sales <- data.frame(
  product = c("A", "B", "C", "D"),
  units = c(12, 0, 8, 0)
)

sales_nonzero <- sales[sales$units != 0, ]
  product units
1       A    12
3       C     8

With dplyr, the equivalent operation is filter(units != 0).

Remove Rows by Row Name in R

A data frame can also be filtered by its row names. The following statement removes rows named record_2 and record_4:

</>
Copy
rownames(employees) <- c("record_1", "record_2", "record_3", "record_4")

result <- employees[!rownames(employees) %in% c("record_2", "record_4"), ]

The %in% operator checks whether each row name appears in the removal list. The leading ! keeps names that are not in that list.

Delete Rows Containing NA Values in R

Use is.na() when rows should be removed because a particular column contains a missing value:

</>
Copy
measurements <- data.frame(
  sample = c("S1", "S2", "S3", "S4"),
  value = c(14.2, NA, 18.5, NA)
)

complete_values <- measurements[!is.na(measurements$value), ]

To remove every row containing an NA in any column, use na.omit() or complete.cases():

</>
Copy
result_with_na_omit <- na.omit(measurements)
result_with_complete_cases <- measurements[complete.cases(measurements), ]

With dplyr, remove missing values from a selected column using filter(!is.na(value)). The tidyr::drop_na() function is another option when working with tidyverse packages.

Reset Row Names After Deleting R Data Frame Rows

Base R preserves the original row names after subsetting. To replace them with consecutive default row names, assign NULL to rownames():

</>
Copy
DF2 <- DF1[-c(2, 4), ]
rownames(DF2) <- NULL
  V1 V2 V3
1  1  9  9
2 14 85 42
3 54 42 16

Avoid Invalid Row Indices When Removing Rows

Before deleting rows from user-supplied index values, restrict the index vector to valid positions. This avoids unexpected missing rows in the result when an index exceeds the number of observations.

</>
Copy
rows_to_remove <- c(2, 4, 20)
valid_rows <- rows_to_remove[rows_to_remove >= 1 & rows_to_remove <= nrow(DF1)]

DF2 <- DF1[-valid_rows, , drop = FALSE]

The drop = FALSE argument explicitly preserves the data-frame structure. It is especially useful when writing reusable subsetting code.

Common Mistakes When Deleting Rows in R

  • Leaving out the comma: DF1[-2] selects columns, while DF1[-2, ] removes the second row.
  • Forgetting to assign the result: Subsetting does not modify the original data frame unless the result is assigned back.
  • Using == NA: Test missing values with is.na(x), not x == NA.
  • Reversing the condition: Base R and filter() retain rows for which a condition is true. Write the condition for the observations you want to keep, or negate the removal condition.
  • Confusing row names with row positions: A displayed row name such as 5 does not necessarily mean that the observation is currently in the fifth position.

Frequently Asked Questions About Removing Rows in R

How do I delete a row from a data frame by row number?

Use a negative row index followed by a comma. For example, df <- df[-3, ] removes the row at position 3 and keeps all columns.

How do I remove several nonconsecutive rows in R?

Place the row positions in a vector and negate it, as in df <- df[-c(2, 5, 8), ].

How do I delete rows containing a specific value with dplyr?

Use filter() with a condition that keeps other values. For example, df %>% filter(status != "inactive") removes rows where status equals inactive.

How do I remove rows with NA in only one column?

In base R, use df <- df[!is.na(df$column_name), ]. With dplyr, use filter(!is.na(column_name)).

Does removing rows change the original R data frame?

No. A subset operation returns another object. Use df <- df[condition, ] when the filtered result should replace df.

Editorial QA Checklist for R Row-Removal Examples

  • Confirm that every base R row subset includes a comma after the row expression.
  • Verify that each logical condition clearly identifies either the rows retained or the rows removed.
  • Use is.na(), complete.cases(), or na.omit() correctly for missing-value examples.
  • Check that printed row names are not described as current row positions after subsetting.
  • Ensure that all dplyr examples load the package or identify the package-qualified function.

Summary of R Data Frame Row Removal Methods

Use negative indices when the row positions are known. Use logical conditions when rows must be removed according to column values, and use is.na() or complete.cases() for missing data. The same tasks can be expressed with dplyr::filter() when working with tidyverse code.

In this R Tutorial, we have learned how to delete a row or multiple rows from a data frame in R using row indices, values, conditions, row names, zero checks, and missing-value checks.