filter()

  • The filter() function is use to find rows of data that match a condition.
  • To be retained, the row must return a value of TRUE for all conditions.
  • From the dplyr package

Syntax:

filter(.data, …, .by = NULL, .preserve = FALSE)

Arguments

.data: a data frame
…:

 

We will sue the ‘cars’ dataset to show the use of the filter() function.

dataset: cars

Dimensions: 50 rows and 2 columns. The columns realte the speed

 

# we will print cars that have a break distance greater than 17 feet

longest_break_distance <- cars % >% filter(dist > 17)

output

Now, we will print cars that have a break distance greater than 17 feet and speed greater than 20 miles per hour.

longest_break_distance <- cars % >% filter(dist > 17 && speed > 20)

print(long_break_dist)
speed dist
1 22 66
2 23 54
3 24 70
4 24 92
5 24 93
6 24 120
7 25 85

 

 

Filter and Plot Data

 

Let us filter and plot cars that are going over 25 miles per hour

 

over_25_mph <- cars %>%
ggplot(cars mapping = aes(x= speed, y=dist) +
geom_point()

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top