How to Delete a Row in R: A Complete Guide for Beginners
Learning how to delete a row in R is one of the first essential skills every data analyst must master. Whether you are cleaning messy survey data, removing outliers, or filtering out incomplete records, the ability to remove unwanted rows keeps your dataset accurate and your analysis trustworthy. In R, there is no single "delete row" button — instead, you have several powerful approaches, from base R indexing to the dplyr package. This guide walks you through every method step by step, with practical examples you can run immediately.
Why Would You Need to Delete Rows in R?
Before jumping into the code, it helps to understand why rows get removed in real projects:
- Missing values: Rows containing
NAcan break statistical models. - Duplicates: Repeated entries inflate your results.
- Outliers or errors: Impossible values (e.g., age = 200) must go.
- Filtering criteria: You may only want customers from a specific country.
- Temporary rows: Some rows were created only for testing.
Each situation has a clean solution, and most of them take just one line of code.
Method 1: Delete Rows by Index Number
The simplest way to remove rows is by using negative indexing. In R, a minus sign tells the interpreter to exclude those positions It's one of those things that adds up..
# Sample data
df <- data.frame(
name = c("Anna", "Ben", "Cara", "Dan"),
score = c(85, 92, 78, 88)
)
# Delete the second row
df_new <- df[-2, ]
To remove multiple rows at once, combine their positions inside c():
df_new <- df[-c(1, 3), ] # removes row 1 and row 3
Important: Always include the comma before the closing bracket. The part before the comma selects rows, and the part after selects columns. Writing df[-2] would remove a column, not a row — a common beginner mistake Worth knowing..
Method 2: Delete Rows Based on a Condition
More often, you will delete rows that meet a logical condition rather than fixed positions. Use square brackets with a logical vector:
# Remove everyone who scored below 80
df_clean <- df[df$score >= 80, ]
You can combine conditions with & (and) or | (or):
df_clean <- df[df$score >= 80 & df$name != "Ben", ]
For readability, the base R function subset() does the same job without repeating the data frame name:
df_clean <- subset(df, score >= 80)
Method 3: Delete Rows with the dplyr Package
The tidyverse ecosystem offers the most readable syntax. The filter() function keeps rows that match your condition, so you simply describe what you want to keep.
library(dplyr)
df_clean <- df %>% filter(score >= 80)
If you truly want to drop rows by position, use slice() with negative numbers:
df_clean <- df %>% slice(-c(1, 3))
And to remove rows matching a condition explicitly, add the negation operator !:
df_clean <- df %>% filter(!(score < 80))
Method 4: Remove Rows Containing NA Values
Missing data is the most common reason people delete rows. R gives you several options:
# Base R: remove rows with any NA
df_clean <- na.omit(df)
# Alternative: keep only complete cases
df_clean <- df[complete.cases(df), ]
With dplyr, the drop_na() function from the tidyr package is clearer:
library(tidyr)
df_clean <- df %>% drop_na() # all columns
df_clean <- df %>% drop_na(score) # only the score column
Pro tip: drop_na(score) removes a row only if score is missing, while na.omit() removes a row if any column has NA. Choose carefully — aggressive deletion can shrink your dataset dramatically Practical, not theoretical..
Method 5: Delete Duplicate Rows
Duplicates quietly distort averages and counts. Base R handles them with duplicated():
df_unique <- df[!duplicated(df), ]
In dplyr, the distinct() function is more expressive:
df_unique <- df %>% distinct()
df_unique <- df %>% distinct(name, .keep_all = TRUE)
The second example keeps the first occurrence of each name while preserving all other columns And that's really what it comes down to..
Method 6: Delete Rows by Row Name
Sometimes rows carry meaningful names instead of numbers. You can filter them like this:
rownames(df) <- c("r1", "r2", "r3", "r4")
df_clean <- df[rownames(df) != "r2", ]
Working with data.table
If your dataset is huge, the data.table package deletes rows by reference, which is faster and memory-efficient:
library(data.table)
dt <- as.data.table(df)
dt <- dt[-c(1, 2)] # by index
dt <- dt[score >= 80] # by condition
Resetting Row Names After Deletion
After removing rows, R keeps
After removing rows, R keeps the original row names, which can leave you with non-sequential indices like 1, 3, 7. Reset them so they reflect the new order:
rownames(df_clean) <- NULL
Or assign explicit numbers:
rownames(df_clean) <- seq_len(nrow(df_clean))
This step matters mainly for base R data frames; tibbles and data.table objects do not rely on traditional row names.
Choosing the Right Method
For quick, interactive work, base R indexing and subset() are hard to beat because they require no packages. Which means when your analysis grows into a pipeline, dplyr::filter() and tidyr::drop_na() produce code that reads like English and plays nicely with the tidyverse. If you are working with millions of rows, data.table deletes by reference and avoids costly copies Easy to understand, harder to ignore. Nothing fancy..
This changes depending on context. Keep that in mind.
Regardless of the tool, always inspect your data before and after deletion—check dimensions with nrow(), look for unexpected NAs, and keep a copy of the original frame so a filtering mistake does not force you to reload everything That's the part that actually makes a difference. Surprisingly effective..
Deleting rows is not just about removing unwanted data; it is about making sure the rows that remain actually represent the question you are trying to answer. Pick the method that matches your data size and coding style, and your downstream models, plots, and summaries will be more trustworthy for it And it works..
The official docs gloss over this. That's a mistake.