Skip to Tutorial Content

Filtering with Multiple Logical Statements

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

Exercise 1

Write the R code required to filter the prostate dataset to rows with both a prostate volume (p_vol) greater than 90 AND a tumor volume (t_vol) >2.

prostate %>% 
  select(age, p_vol, t_vol) %>% 
  filter()
prostate %>% 
  select(age, p_vol, t_vol) %>% 
  filter(p_vol > 90 & t_vol >2)

Exercise 2

Write the R code required to filter the prostate dataset to rows with either a family history (fam_hx) of prostate cancer or a preop_psa >20.

prostate %>% 
  select(age, preop_psa, fam_hx) %>% 
  filter()
prostate %>% 
  select(age, preop_psa, fam_hx) %>% 
  filter(fam_hx ==1 | preop_psa > 20)

Exercise 3

Write the R code required to filter the prostate dataset to high risk patients who are either African-American (aa), or have a surgical Gleason score (s_gs) of 4, but not both

prostate %>% 
  select(age, aa, s_gs) %>% 
  filter()
prostate %>% 
  select(age, aa, s_gs) %>% 
  filter(xor(aa ==1, s_gs == 4))

Exercise 4

Write the R code required to filter the prostate dataset to rows with (age >70 OR preop_psa >= 15) AND not African-american.

prostate %>% 
  select(age, preop_psa, aa) %>% 
  filter()
prostate %>% 
  select(age, preop_psa, aa) %>% 
  filter( (age > 70 | preop_psa >= 15) & aa != 1)

Tutorial