Filter Out the Cases from an Object in R Programming – filter() Function
filter()
function in R Language is used to choose cases and filtering out the values based on the filtering expression.
Syntax: filter(x, expr)
Parameters:
x: Object to be filtered
expr: expression as a base for filtering
Example 1:
library(dplyr)
d < - data.frame( name = c( "Abhi" , "Bhavesh" , "Chaman" , "Dimri" ),
age = c( 7 , 5 , 9 , 16 ),
ht = c( 46 , NA, NA, 69 ),
school = c( "yes" , "yes" , "no" , "no" ) )
d
filter (d, is .na(ht))
filter (d, ! is .na(ht))
|
Output:
name age ht school
1 Abhi 7 46 yes
2 Bhavesh 5 NA yes
3 Chaman 9 NA no
4 Dimri 16 69 no
name age ht school
1 Bhavesh 5 NA yes
2 Chaman 9 NA no
name age ht school
1 Abhi 7 46 yes
2 Dimri 16 69 no
Example 2:
library(dplyr)
d < - data.frame( name = c( "Abhi" , "Bhavesh" , "Chaman" , "Dimri" ),
age = c( 7 , 5 , 9 , 16 ),
ht = c( 46 , NA, NA, 69 ),
school = c( "yes" , "no" , "yes" , "no" ) )
d
filter (d, school = = "yes" )
filter (d, school = = "no" )
|
Output:
name age ht school
1 Abhi 7 46 yes
2 Bhavesh 5 NA no
3 Chaman 9 NA yes
4 Dimri 16 69 no
name age ht school
1 Abhi 7 46 yes
2 Chaman 9 NA yes
name age ht school
1 Bhavesh 5 NA no
2 Dimri 16 69 no
Last Updated :
19 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...