The filter() function will find rows/cases where the conditions indicated are true. It is often used with operators such as the following:
- > (greater than)
- < (less than)
- >= (greater than or equal to)
- <= (less than or equal to)
- == (equal to)
- != (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:
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(Fingers, SSLast != "NA")
# Check the first few rows of the data (does not contain NAs)
head(select(Fingers_no_NA, SSLast))
Example output:
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(Fingers, Thumb))
Example output:
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(Fingers, Thumb > 65)
# Check the first few rows of the data (only Thumb lengths over 65)
head(select(Fingers_65, Thumb))
Example output: