Skip to Tutorial Content

Selecting Variables in a dataset

We will start with the prostate dataset, seen here with glimpse():

glimpse(prostate)
## Rows: 316
## Columns: 20
## $ rbc_age_group      <dbl> 3, 3, 3, 2, 2, 3, 3, 1, 1, 2, 2, 1, 1, 1, 3, 1, 1, …
## $ median_rbc_age     <dbl> 25, 25, 25, 15, 15, 25, 25, 10, 10, 15, 15, 10, 10,…
## $ age                <dbl> 72.1, 73.6, 67.5, 65.8, 63.2, 65.4, 65.5, 67.1, 63.…
## $ aa                 <dbl> 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, …
## $ fam_hx             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ p_vol              <dbl> 54.0, 43.2, 102.7, 46.0, 60.0, 45.9, 42.6, 40.7, 45…
## $ t_vol              <dbl> 3, 3, 1, 1, 2, 2, 2, 3, 2, 2, 1, 3, 2, 2, 2, 2, 1, …
## $ t_stage            <dbl> 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, NA,…
## $ b_gs               <dbl> 3, 2, 3, 1, 2, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1, …
## $ bn                 <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ organ_confined     <dbl> 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, …
## $ preop_psa          <dbl> 14.08, 10.50, 6.98, 4.40, 21.40, 5.10, 6.03, 8.70, …
## $ preop_therapy      <dbl> 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, …
## $ units              <dbl> 6, 2, 1, 2, 3, 1, 2, 4, 1, 2, 2, 2, 2, 4, 2, 4, 5, …
## $ s_gs               <dbl> 1, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1, 3, 1, 2, …
## $ any_adj_therapy    <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ adj_rad_therapy    <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ recurrence         <dbl> 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ censor             <dbl> 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ time_to_recurrence <dbl> 2.67, 47.63, 14.10, 59.47, 1.23, 74.70, 13.87, 8.37…

Exercise 1

Write the R code required to select the columns with participant attributes (age, aa, fam_hx) to produce a 3 column dataset.

prostate %>% 
  select(---)
prostate %>% 
  select(age, aa, fam_hx)

Exercise 2

Write the R code required to select the variables that contain “recurrence” in the variable name

Watch the number of == signs

prostate %>% 
  select(---)
prostate %>% 
  select(contains("text"))
prostate %>% 
  select(contains("recurrence"))

Exercise 3

Write the R code required to select all the columns from p_vol to the last column in the prostate dataset (skipping over the first 5).

prostate %>% 
  select(---)
prostate %>% 
  select(p_vol:last_col()) 

Exercise 4

Write the R code required to select the variable age, and all of the variables that start with preop.
prostate %>% 
  select(---)
prostate %>% 
  select(age, starts_with("preop"))

Exercise 5

Take a look at the licorice gargle data, seen here with glimpse()

glimpse(licorice)
## Rows: 235
## Columns: 19
## $ pre_op_gender            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ pre_op_asa               <dbl> 3, 2, 2, 2, 1, 2, 3, 2, 3, 3, 2, 2, 2, 2, 3, …
## $ pre_op_calc_bmi          <dbl> 32.98, 23.66, 26.83, 28.39, 30.45, 35.49, 25.…
## $ pre_op_age               <dbl> 67, 76, 58, 59, 73, 61, 66, 61, 83, 69, 31, 7…
## $ pre_op_mallampati        <dbl> 2, 2, 2, 2, 1, 3, 1, 2, 1, 2, 1, 2, 2, 1, 2, …
## $ pre_op_smoking           <dbl> 1, 2, 1, 1, 2, 1, 1, 1, 1, 3, 1, 2, 3, 3, 3, …
## $ pre_op_pain              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ treat                    <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ intra_op_surgery_size    <dbl> 2, 1, 2, 3, 2, 3, 3, 1, 1, 2, 2, 1, 1, 2, 2, …
## $ extubation_cough         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ pacu30min_cough          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ pacu30min_throat_pain    <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ pacu30min_swallow_pain   <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ pacu90min_cough          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ pacu90min_throat_pain    <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ post_op4hour_cough       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ post_op4hour_throat_pain <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ pod1am_cough             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ pod1am_throat_pain       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …

Write the R code required to select all the variables that end with pain or cough.

licorice %>% 
  select(---)
licorice %>% 
  select(ends_with(c("pain", "cough")))

Exercise 6

Take a look at the licorice gargle data, seen here

Write the R code required to select all the variables that match the pattern pacu-digit in 0-9-0.

licorice %>% 
  select(---)
licorice %>% 
  select(matches("pacu[---]0"))
licorice %>% 
  select(matches("pacu[0-9]0"))

Note that [3,9] would also work for this one.

Selecting Variables