How to Remove Rows in R (With Examples)

Rows can be removed from a data frame in R using the subset() and filter() functions. The subset() function can be used to select a subset of a data frame based on the values of one or more columns, while the filter() function can be used to apply logical tests to one or more columns and remove those rows that do not meet the criteria. Both of these functions are powerful tools for removing specific rows from a data frame. Examples are provided to show how to use these functions.


You can use the following syntax to remove specific row numbers in R:

#remove 4th row
new_df <- df[-c(4), ]

#remove 2nd through 4th row
new_df <- df[-c(2:4), ]

#remove 1st, 2nd, and 4th row
new_df <- df[-c(1, 2, 4), ]

You can use the following syntax to remove rows that don’t meet specific conditions:

#only keep rows where col1 value is less than 10 and col2 value is less than 6
new_df <- subset(df, col1<10 & col2<6)

And you can use the following syntax to remove rows with an NA value in any column:

#remove rows with NA value in any column
new_df <- na.omit(df)

The following examples show how to use each of these functions in practice.

Example 1: Remove Rows by Number

The following code shows how to remove rows by specific row numbers in R:

#create data frame
df <- data.frame(player=c('A', 'B', 'C', 'D', 'E'),
                 pts=c(17, 12, 8, 9, 25),
                 rebs=c(3, 3, 6, 5, 8),
                 blocks=c(1, 1, 2, 4, NA))

#view data frame
df

  player pts rebs blocks
1      A  17    3      1
2      B  12    3      1
3      C   8    6      2
4      D   9    5      4
5      E  25    8     NA

#remove 4th row
df[-c(4), ]

  player pts rebs blocks
1      A  17    3      1
2      B  12    3      1
3      C   8    6      2
5      E  25    8     NA

#remove 2nd through 4th row
df[-c(2:4), ]

  player pts rebs blocks
1      A  17    3      1
5      E  25    8     NA

#remove 1st, 2nd, and 4th row
df[-c(1, 2, 4), ]

  player pts rebs blocks
3      C   8    6      2
5      E  25    8     NA

Example 2: Remove Rows by Condition

The following code shows how to remove rows that don’t meet a specific condition:

#create data frame
df <- data.frame(player=c('A', 'B', 'C', 'D', 'E'),
                 pts=c(17, 12, 8, 9, 25),
                 rebs=c(3, 3, 6, 5, 8),
                 blocks=c(1, 1, 2, 4, NA))

#view data frame
df

  player pts rebs blocks
1      A  17    3      1
2      B  12    3      1
3      C   8    6      2
4      D   9    5      4
5      E  25    8     NA

#only keep rows where pts is less than 10 and rebs is less than 6
subset(df, pts<10 & rebs<6)

  player pts rebs blocks
4      D   9    5      4

Example 3: Remove Rows with NA Values

The following code shows how to remove rows with a NA value in any row:

#create data frame
df <- data.frame(player=c('A', 'B', 'C', 'D', 'E'),
                 pts=c(17, 12, 8, 9, 25),
                 rebs=c(3, 3, 6, 5, 8),
                 blocks=c(1, 1, 2, 4, NA))

#view data frame
df

  player pts rebs blocks
1      A  17    3      1
2      B  12    3      1
3      C   8    6      2
4      D   9    5      4
5      E  25    8     NA

#remove rows with NA value in any row:
na.omit(df)

  player pts rebs blocks
1      A  17    3      1
2      B  12    3      1
3      C   8    6      2
4      D   9    5      4

x