filter()

filter()

The filter() function will find rows/cases where the conditions indicated are true. It is often used with operators such as the following:
  1. > (greater than)
  2. < (less than)
  3. >= (greater than or equal to)
  4. <= (less than or equal to)
  5. == (equal to)
  6. != (not equal to)
These are used to specify which values you are looking to filter into your data frame.

NOTE: The filter() function selects rows. To select specific columns, see the select() function.

Example 1:

The Fingers data frame has a variable called SSLast. Some of the values are missing and marked as NA.

# Check the first few rows of the data (contains NAs)
head(select(Fingers, SSLast))

Example output:
output of head function showing first 6 rows of Fingers data frame. It has NA values for SSLast.

You may want to filter in only the rows that do not have missing cases. You can use the filter() function with the != operator to help achieve this.

# Filter in cases for SSLast that are not "NA"
# Optional: save it as a new data frame called Fingers_no_NA
Fingers_no_NA <- filter(FingersSSLast != "NA")

# Check the first few rows of the data (does not contain NAs)
head(select(Fingers_no_NA, SSLast))

Example output:
output of head function showing first 6 rows of Fingers data frame. It has no NA values for SSLast.

Example 2:

The Fingers data frame contains a range of values for Thumb.

# Check the first few rows of the data (a range of Thumb lengths)
head(select(FingersThumb))

Example output:

output of head function showing first 6 rows of Fingers data frame. It shows a range of values for Thumb length.

Perhaps you are interested in only analyzing the thumb lengths that are greater than 65. You can use the filter() function with the > operator to help achieve this.

# Filter in cases for Thumb that are greater than 65
# Optional: save it as a new data frame called Fingers_65
Fingers_65 <- filter(FingersThumb > 65)

# Check the first few rows of the data (only Thumb lengths over 65)
head(select(Fingers_65, Thumb))

Example output:
output of head function showing first 6 rows of Fingers data frame. It only shows values greater than 65.

    • Related Articles

    • The Pipe Operator %>%

      The Pipe Operator %>%  What it is In R, %>% is called the "pipe" operator.  It  takes the output of one statement and uses it as the input for the following statement. Another way of saying this is that it "pipes" or "chains" together a string of ...
    • select()

      The select() function will select specific columns (variables) in a data frame. This may be useful when a data frame has many variables and you only want to take a look at a few of them together, or save a subset of variables as a new data frame. ...
    • sort()

      The sort() function will sort a vector (or a single column in a data frame) by a specific variable, in ascending order. You can use the decreasing = TRUE argument with the sort() function to sort the data frame in descending order (be careful not to ...