Skip to Tutorial Content

Filtering Strings

We will start with the covid dataset, and combine multiple logical statements to filteR our Rows.

Exercise 1

Write the R code required to filter the covid dataset to rows with both a fake_first_name of “penny” and a fake_first_name of “targaryen”.

covid %>% 
  select(subject_id, fake_first_name, fake_last_name) %>%
  filter(--- & ---)
covid %>% 
  select(subject_id, fake_first_name, fake_last_name) %>%
  filter(fake_first_name == "penny" & fake_last_name == "targaryen")

Exercise 2

Write the R code required to filter the covid dataset to rows with a payor_group of “government”.

covid %>% 
  select(subject_id, patient_class, payor_group) %>% 
  filter()
covid %>% 
  select(subject_id, patient_class, payor_group) %>% 
  filter(payor_group == "government")

Exercise 3

Write the R code required to filter the covid dataset to subjects who were seen in some part of radiology (includes the string “rad” in the clinic_name variable). Page through the results to check your work.

covid %>% 
  select(study_id, clinic_name) %>% 
  filter(str_detect())
covid %>% 
  select(subject_id, clinic_name) %>% 
  filter(str_detect(clinic_name, "rad"))

Exercise 4

Write the R code required to filter the covid dataset to subjects who were NOT seen in any clinical setting that ends in “ology”. Page through the results to check your work.

covid %>% 
  select(subject_id, clinic_name) %>% 
  filter(---)
covid %>% 
  select(subject_id, clinic_name) %>% 
  filter(!str_detect(clinic_name, "ology"))

Exercise 5

Write the R code required to filter the covid dataset to subjects who were in a patient_class seen by surgery. Page through the results to check your work.

covid %>% 
  select(subject_id, patient_class) %>% 
  filter(---)
covid %>% 
  select(subject_id, patient_class) %>% 
  filter(str_detect(patient_class, "surgery"))

Tutorial