
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Subset Rows of an R Data Frame by Column Values
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
Advertisements