- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to subset rows of an R data frame if any columns have values greater than a certain value?
To subset rows of an R data frame if any columns have values greater than a certain value, we can follow the below steps −
- First of all, create a data frame.
- Then, use filter_all function of dplyr package with any_vars function to subset the rows of the data frame for any columns having values greater than a certain value.
Create the data frame
Let's create a data frame as shown below −
x1<-sample(1:10,20,replace=TRUE) x2<-sample(1:20,20,replace=TRUE) df<-data.frame(x1,x2) df
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x1 x2 1 5 7 2 2 4 3 5 17 4 2 9 5 8 18 6 8 16 7 9 14 8 1 10 9 7 12 10 4 20 11 7 19 12 9 8 13 7 15 14 1 12 15 8 10 16 3 14 17 5 2 18 5 7 19 2 8 20 7 15
Subset the data frame
Loading dplyr package and using filter_all function any_vars function to subset the rows of the data frame df for all columns having values greater than 10 −
x1<-sample(1:10,20,replace=TRUE) x2<-sample(1:20,20,replace=TRUE) df<-data.frame(x1,x2) library(dplyr) df %>% filter_all(any_vars(.>10))
Output
x1 x2 1 5 17 2 8 18 3 8 16 4 9 14 5 7 12 6 4 20 7 7 19 8 7 15 9 1 12 10 3 14 11 7 15
- Related Articles
- How to subset rows of an R data frame if all columns have values greater than a certain value
- Replace all values in an R data frame if they are greater than a certain value.
- How to subset an R data frame if one of the supplied grouping values is found and numerical column value is greater than a certain value?
- How to subset an R data frame if numerical column is greater than a certain value for a particular category in grouping column?
- How to remove rows that contains NA values in certain columns of an R data frame?
- Check if any value in an R vector is greater than or less than a certain value.
- How to remove rows in an R data frame column that has duplicate values greater than or equal to a certain number of times?
- How to delete a row from an R data frame if any value in the row is greater than n?
- How to subset factor columns in an R data frame?
- How to subset an R data frame by ignoring a value in one of the columns?
- How to subset R data frame rows if at least one or more values matches?
- How to subset columns that has less than four categories in an R data frame?
- How to subset rows of an R data frame based on duplicate values in a particular column?
- How to subset rows of an R data frame using grepl function?
- How to subset an R data frame based on string values of a columns with OR condition?

Advertisements