Relocating Variables in a dataset
Sometimes it is convenient to re-order your columns, particularly when a mutate statement or a join puts new variables all the way to the far right of your dataset view. You may want to rearrange your variables to put similar variables together, or pull observation identifiers like patient_id, visit_date, and visit_number to the “front” - left side, being the first variables you see before you begin scrolling.
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 bring the patient identifiers (age, aa, fam_hx) to the front (as the first 3 variables).
prostate %>%
relocate(---)
prostate %>%
relocate(age, aa, fam_hx)
Exercise 2
Write the R code required to move the first two variables about rbc_age to just after the variable units.
prostate %>%
relocate(---, ---)
prostate %>%
relocate(--- , ---)
prostate %>%
relocate(rbc_age_group, median_rbc_age , .after = units)
Exercise 3
Write the R code required to move the variables patient_class and payor_group in the covid dataset to just before the variable pan_day.
medicaldata::blood_storage%>%
relocate(--- , ---)
covid %>%
relocate(--- , .before = ---)
covid %>%
relocate(patient_class, payor_group , .before = pan_day)
Exercise 4
Write the R code required to move all the non-numeric variables with names that end with “group” to the front of the covid dataset
covid %>%
relocate(--- & ---)
covid %>%
relocate(!where(---) & ends_with(---))
covid %>%
relocate(!where(is.numeric) & ends_with("group"))