filter(): Subsetting with filter

Now suppose that we want to filter the data table to only show the entries for which the murder rate is lower than 0.71. To do this we use the filter function, which takes the data table as the first argument and then the conditional statement as the second. Like mutate, we can use the unquoted variable names from murders inside the function and it will know we mean the columns and not objects in the workspace.

filter(murders, rate <= 0.71)
#>           state abb        region population total  rate
#> 1        Hawaii  HI          West    1360301     7 0.515
#> 2          Iowa  IA North Central    3046355    21 0.689
#> 3 New Hampshire  NH     Northeast    1316470     5 0.380
#> 4  North Dakota  ND North Central     672591     4 0.595
#> 5       Vermont  VT     Northeast     625741     2 0.320

Instruction

Run the sample code to see how filter() function works.

library(dplyr) library(dslabs) data("murders") # Add the rate column murders <- mutate(murders, rate = total / population * 100000) # Check murders to find that the new column is added. head(murders) # select the data under condition rate <= 0.71 filter(murders, rate <= 0.71)

Previous: 3-3 | mutate(): Adding a column

Next: 3-5 | select(): Selecting columns with select

Back to Main