Conditionally Remove Row from Data Frame in R (3 Examples) | How to Delete Rows

 

This page explains how to conditionally delete rows from a data frame in R programming.

The article will consist of this:

Let’s do this.

 

Creation of Example Data

In the examples of this R programming tutorial, we’ll use the following data frame as basement:

data <- data.frame(x1 = 1:5,                  # Create example data
                   x2 = letters[1:5],
                   x3 = "x")
data                                          # Print example data
#   x1 x2 x3
# 1  1  a  x
# 2  2  b  x
# 3  3  c  x
# 4  4  d  x
# 5  5  e  x

Our example data contains five rows and three columns.

 

Example 1: Remove Row Based on Single Condition

If we want to delete one or multiple rows conditionally, we can use the following R code:

data[data$x1 != 2, ]                          # Remove row based on condition
#   x1 x2 x3
# 1  1  a  x
# 3  3  c  x
# 4  4  d  x
# 5  5  e  x

The previous R syntax removed each row from our data frame, which fulfilled the condition data$x1 != 2 (i.e. the second row).

In this example, we used only one logical condition. However, we can also remove rows according to multiple conditions and that’s what I’m going to show you next!

 

Example 2: Remove Row Based on Multiple Conditions

We can remove rows based on multiple conditions by using the &- or the |-operator. Have a look at the following R code:

data[data$x1 != 2 & data$x2 != "e", ]         # Multiple conditions
#   x1 x2 x3
# 1  1  a  x
# 3  3  c  x
# 4  4  d  x

As you can see based on the output of the RStudio console, the previous R syntax deleted two rows according to the two logical conditions data$x1 != 2 & data$x2 != “e”.

 

Example 3: Remove Row with subset function

Alternatively to Examples 1 and 2, we can use the subset function:

subset(data, data$x1 != 2 & data$x2 != "e")   # Apply subset function
#   x1 x2 x3
# 1  1  a  x
# 3  3  c  x
# 4  4  d  x

The resulting output is the same as in Example 2, since we used the same condition. However, this time we used the subset command instead of square brackets. Which of these options you prefer, is a matter of taste!

 

Video & Further Resources

Do you need more info on the content of this tutorial? Then you may want to have a look at the following video of my YouTube channel. I’m illustrating the R codes of this tutorial in the video:

 

The YouTube video will be added soon.

 

In addition, you may have a look at the related articles of my homepage. A selection of related articles is shown below:

 

To summarize: In this tutorial you learned how to exclude specific rows from a data table or matrix in the R programming language. Please let me know in the comments, in case you have further questions.

 



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top