7.3 filter
filter() is used to filter rows in a data frame / tibble.
Keep rows if Democratic is found in column party:
| name | start | end | party |
|---|---|---|---|
| Eisenhower | 1953-01-20 | 1961-01-20 | Republican |
| Kennedy | 1961-01-20 | 1963-11-22 | Democratic |
| Johnson | 1963-11-22 | 1969-01-20 | Democratic |
| Nixon | 1969-01-20 | 1974-08-09 | Republican |
| Ford | 1974-08-09 | 1977-01-20 | Republican |
| Carter | 1977-01-20 | 1981-01-20 | Democratic |
| Reagan | 1981-01-20 | 1989-01-20 | Republican |
| Bush | 1989-01-20 | 1993-01-20 | Republican |
| Clinton | 1993-01-20 | 2001-01-20 | Democratic |
| Bush | 2001-01-20 | 2009-01-20 | Republican |
| Obama | 2009-01-20 | 2017-01-20 | Democratic |
filter(presidential,
party=="Democratic")## # A tibble: 5 x 4
## name start end party
## <chr> <date> <date> <chr>
## 1 Kennedy 1961-01-20 1963-11-22 Democratic
## 2 Johnson 1963-11-22 1969-01-20 Democratic
## 3 Carter 1977-01-20 1981-01-20 Democratic
## 4 Clinton 1993-01-20 2001-01-20 Democratic
## 5 Obama 2009-01-20 2017-01-20 Democratic
You can filter using several variables/columns:
filter(presidential,
party=="Republican", name=="Bush")
# This implicity uses the "&", i.e. the fact that both conditions have to be TRUE
filter(presidential,
party=="Republican" & name=="Bush")
# Any logical operators can be used
filter(presidential,
name %in% c("Bush", "Kennedy"))The same can be used for numerical values: let’s select all rows from table5 where century > 19:
filter(table5,
century > 19)## # A tibble: 3 x 4
## country century year rate
## <chr> <chr> <chr> <chr>
## 1 Afghanistan 20 00 2666/20595360
## 2 Brazil 20 00 80488/174504898
## 3 China 20 00 213766/1280428583
HANDS-ON
Going back to our previously create starwarsBMI data frame:
- How many characters have a BMI > 30?
- How many characters have a BMI > 30 AND are Droids (“Droid” in column
species)? - From the previous selection (BMI > 30 and Droid), select columns BMI, character_name, height and mass, and save in the new object
DroidBMI30.
Answer
# How many characters have a BMI > 30?
filter(starwarsBMI, BMI > 30)
# How many characters have a BMI > 30 AND are Droids ("Droid" in column "species")?
filter(starwarsBMI, BMI > 30 & species=="Droid")
# From the previous selection (BMI > 30 and Droid), select columns BMI, character_name, height and mass, and save in the new object DroidBMI30.
DroidBMI30 <- starwarsBMI %>%
filter(BMI > 30 & species=="Droid") %>%
select(BMI, character_name, height, mass)