We provide effective and economically affordable online training courses for R and Python, click here for more details and course registration !

Dplyr is a package in R. It belongs to Tidyverse framework, and is allowed to use pipeline structure to chain multiple operations together into one statement. There are many functions in Dplyr package, and one of them is filter(). filter() is used to select observations in terms of specific conditions. For example we will only select observations with ‘gear’ is four from mtcars dataset here.

#load tidyverse package, which includes dplyr package
library(tidyverse)
#using mtcars dataset
data("mtcars")
#show first observations of dataset
head(mtcars)
#output 
                  mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
#use filter() to select observations with gear is 4
df_gear4 <- filter(mtcars, gear == 4)
#show first observations of dataset
head(df_gear4)
#output
               mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4     21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710    22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Merc 240D     24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230      22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280      19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4

filter() can also work based on multiple conditions. In the following example, observations with gear 4 and horsepower less than 100 are selected.

#observations in which gear are four and horsepower lower than 100
df_hpl100_gear4 <- filter(mtcars, gear == 4,hp <= 100)
#show first observations of dataset
head(df_hpl100_gear4)
#output
                mpg cyl  disp hp drat    wt  qsec vs am gear carb
Datsun 710     22.8   4 108.0 93 3.85 2.320 18.61  1  1    4    1
Merc 240D      24.4   4 146.7 62 3.69 3.190 20.00  1  0    4    2
Merc 230       22.8   4 140.8 95 3.92 3.150 22.90  1  0    4    2
Fiat 128       32.4   4  78.7 66 4.08 2.200 19.47  1  1    4    1
Honda Civic    30.4   4  75.7 52 4.93 1.615 18.52  1  1    4    2
Toyota Corolla 33.9   4  71.1 65 4.22 1.835 19.90  1  1    4    1

In the following example, observations with gear 4 and cyl less than 6 are selected.

#use filter() to select observations with gear 4 and cyl less than 6
df_gear4_cyl6 <- filter(mtcars, gear == 4|cyl < 6)
#show first observations of dataset
head(df_gear4_cyl6)
#output
               mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4     21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710    22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Merc 240D     24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230      22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280      19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4

For getting more knowledge of R and a preview of our training course, you can watch R tutorial videos on our YouTube channel !


0 Comments

Leave a Reply

Avatar placeholder