Test syntax highlighting
Prologue
This quiz will test your understanding of the filter()
function in the {dplyr} package. Good luck!
Question 1
df
is a dataset from a smallpox outbreak in Nigeria.
df <- outbreaks::smallpox_abakaliki_1967 |> as_tibble()
df
## # A tibble: 5 × 8
## case_ID date_of_onset age gender vaccinated vaccscar ftc compound
## <int> <date> <int> <fct> <fct> <fct> <fct> <fct>
## 1 1 1967-04-05 10 f n n y 1
## 2 2 1967-04-18 25 f n n y 1
## 3 3 1967-04-25 35 m n n y 1
## 4 4 1967-04-27 4 f n n y 1
## 5 5 1967-04-30 11 m n n y 1
Which of the following is an incorrect interpretation of a filter statement on the age
column in df
? {3}
df %>% filter(!age > 5)
: Drop row(s) whereage
is above 5df %>% filter(age < 5)
: Keep row(s) whereage
is below 5df %>% filter(age == 5)
: Drop row(s) whereage
is equal to 5
Question 2
df
is an excerpt of a dataset from an H7N9 outbreak in China.
df <- outbreaks::fluH7N9_china_2013[c(1,2,3,74, 75), c(6:8)]
df
## gender age province
## 1 m 87 Shanghai
## 2 m 27 Shanghai
## 3 f 35 Anhui
## 74 <NA> ? Shanghai
## 75 <NA> ? Shanghai
To drop the single female patient, which of the following filter statements is correct {1}
df %>% filter(gender == "m" & is.na(gender))
df %>% filter(gender != "f")
df %>% filter(gender == "m")
Question 3
df <-
tribble(~id, ~state,
1, "Kano",
2, "Lagos",
3, "Bauchi",
4, "Kano",
5, "FCT",
6, NA)
df
## # A tibble: 6 × 2
## id state
## <dbl> <chr>
## 1 1 Kano
## 2 2 Lagos
## 3 3 Bauchi
## 4 4 Kano
## 5 5 FCT
## 6 6 <NA>
From df
, you would like to keep rows where state
is either “FCT” or “Kano”. Which of the following uses correct syntax for this. {1}
df %>% filter(state %in% c("FCT", "Kano"))
df %>% filter(state == "FCT" & state == "Kano"))
df %>% filter(state == c("FCT", "Kano"))