Skip to Tutorial Content

Renaming 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 rename the variable fam_hx to fhx. Remember (new_name = old_name) syntax.

prostate %>% 
  rename(--- = ---)
prostate %>% 
  rename(fhx = fam_hx)

Exercise 2

Write the R code required to rename all of the variables that start with “preop” by replacing “preop” with “PRE”, using replace_with(), and the str_replace() function

prostate %>% 
  rename_with(--- , ---)
prostate %>% 
  rename_with(--- , ---)
prostate %>% 
  rename_with(~str_replace(., "preop", "PRE_"),
              starts_with("preop"))

Exercise 3

Write the R code required to rsystematically rename all of the variables in the blood storage dataset to upper case with the toupper() function.

medicaldata::blood_storage%>% 
  rename_with(--- , ---)
medicaldata::blood_storage%>% 
  rename_with(~ ---(.), ---())
medicaldata::blood_storage%>% 
  rename_with(~ toupper(.), everything())

Exercise 4

Write the R code required to rsystematically rename all of the variables in the blood storage dataset to lower_snake_case with janitor::clean_names()

medicaldata::blood_storage%>% 
  janitor::---()
medicaldata::blood_storage%>% 
  janitor::clean_names()

Renaming Variables