Conditionally Remove Row From Data Frame In R (3 Examples)
Maybe your like
This page explains how to conditionally delete rows from a data frame in R programming.
The article will consist of this:
- Creation of Example Data
- Example 1: Remove Row Based on Single Condition
- Example 2: Remove Row Based on Multiple Conditions
- Example 3: Remove Row with subset function
- Video & Further Resources
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 xOur 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 xThe 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 xAs 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 xThe 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:
In addition, you may have a look at the related articles of my homepage. A selection of related articles is shown below:
- Remove Row with NA from Data Frame in R
- Extract Row from Data Frame in R
- Add New Row to Data Frame in R
- The R Programming Language
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.
Subscribe to the Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe.I hate spam & you may opt out anytime: Privacy Policy.
SubscribeLoading...
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.
Tag » How To Remove Rows In R
-
How Do I Delete Rows In A Data Frame? - Stack Overflow
-
How To Remove Rows From An R Data Frame - With Examples
-
Delete Or Drop Rows In R With Conditions - DataScience Made Simple
-
How To Delete Rows In R? Explained With Examples
-
How To Remove Multiple Rows In R (With Examples) - - Statology
-
Remove Rows From The Data Frame In R - R-bloggers
-
R Data Frame - Delete Row Or Multiple Rows - Tutorial Kart
-
How To Remove/Delete A Row In R - Rows With NA, Conditions ...
-
How To Remove Single Row Or Multiple Rows In R - R-Lang
-
How To Remove Rows In R DataFrame? - GeeksforGeeks
-
Remove Rows With NA In R Data Frame (6 Examples) | Some Or All ...
-
How To Conditionally Remove Rows In R DataFrame?
-
How To Remove Rows And Columns Of A Matrix In R
-
How To Remove Rows In R Data Frame That Contains A Specific Number?