Variable labels can be created using ff_label. Some functions strip variable labels (variable attributes), e.g. forcats::fct_recode. Use this function to create a vector of variable labels from a data frame. Then use ff_relabel to relabel variables in data frame.

ff_relabel(.data, .labels)

finalfit_relabel(.data, .labels)

Arguments

.data

Data frame to be relabelled

.labels

Vector of variable labels (usually created using extract_variable_label).

Examples

# Label variable
colon_s$sex.factor %>%
  ff_label("Sex") %>%
  str()
#>  Factor w/ 2 levels "Female","Male": 2 2 1 1 2 1 2 2 2 1 ...
#>  - attr(*, "label")= chr "Sex"

# Make factor level "Unknown" NA
colon_s %>%
  dplyr::mutate_if(is.factor, forcats::fct_recode, 
  NULL = "Unknown") %>% 
  str()
#> 'data.frame':	929 obs. of  32 variables:
#>  $ id             : num  1 2 3 4 5 6 7 8 9 10 ...
#>  $ rx             : Factor w/ 3 levels "Obs","Lev","Lev+5FU": 3 3 1 3 1 3 2 1 2 3 ...
#>  $ sex            : num  1 1 0 0 1 0 1 1 1 0 ...
#>  $ age            : num  43 63 71 66 69 57 77 54 46 68 ...
#>   ..- attr(*, "label")= chr "Age (years)"
#>  $ obstruct       : num  NA 0 0 1 0 0 0 0 0 0 ...
#>  $ perfor         : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ adhere         : num  0 0 1 0 0 0 0 0 1 0 ...
#>  $ nodes          : num  5 1 7 6 22 9 5 1 2 1 ...
#>  $ status         : num  1 0 1 1 1 1 1 0 0 0 ...
#>  $ differ         : num  2 2 2 2 2 2 2 2 2 2 ...
#>  $ extent         : num  3 3 2 3 3 3 3 3 3 3 ...
#>  $ surg           : num  0 0 0 1 1 0 1 0 0 1 ...
#>  $ node4          : num  1 0 1 1 1 1 1 0 0 0 ...
#>  $ time           : num  1521 3087 963 293 659 ...
#>  $ sex.factor     : Factor w/ 2 levels "Female","Male": 2 2 1 1 2 1 2 2 2 1 ...
#>  $ rx.factor      : Factor w/ 3 levels "Obs","Lev","Lev+5FU": 3 3 1 3 1 3 2 1 2 3 ...
#>  $ obstruct.factor: Factor w/ 2 levels "No","Yes": NA 1 1 2 1 1 1 1 1 1 ...
#>  $ perfor.factor  : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...
#>  $ adhere.factor  : Factor w/ 2 levels "No","Yes": 1 1 2 1 1 1 1 1 2 1 ...
#>  $ differ.factor  : Factor w/ 3 levels "Well","Moderate",..: 2 2 2 2 2 2 2 2 2 2 ...
#>  $ extent.factor  : Factor w/ 4 levels "Submucosa","Muscle",..: 3 3 2 3 3 3 3 3 3 3 ...
#>  $ surg.factor    : Factor w/ 2 levels "Short","Long": 1 1 1 2 2 1 2 1 1 2 ...
#>  $ node4.factor   : Factor w/ 2 levels "No","Yes": 2 1 2 2 2 2 2 1 1 1 ...
#>  $ status.factor  : Factor w/ 2 levels "Alive","Died": 2 1 2 2 2 2 2 1 1 1 ...
#>  $ age.factor     : Factor w/ 3 levels "<40 years","40-59 years",..: 2 3 3 3 3 2 3 2 2 3 ...
#>  $ loccomp        : num  NA 0 1 1 0 0 0 0 1 0 ...
#>  $ loccomp.factor : Factor w/ 2 levels "No","Yes": NA 1 2 2 1 1 1 1 2 1 ...
#>  $ time.years     : num  4.167 8.458 2.638 0.803 1.805 ...
#>   ..- attr(*, "label")= chr "Time (years)"
#>  $ mort_5yr       : Factor w/ 2 levels "Alive","Died": 2 1 2 2 2 2 2 1 1 1 ...
#>  $ age.10         : num  4.3 6.3 7.1 6.6 6.9 5.7 7.7 5.4 4.6 6.8 ...
#>  $ mort_5yr.num   : num  2 1 2 2 2 2 2 1 1 1 ...
#>  $ hospital       : Factor w/ 5 levels "hospital_1","hospital_2",..: 5 3 5 4 5 4 2 2 2 2 ...

# Reset data
data(colon_s)

# Extract variable labels
vlabels = colon_s %>% extract_variable_label()

# Run function where labels are lost
colon_s %>%
  dplyr::mutate_if(is.factor, forcats::fct_recode, 
  NULL = "Unknown") %>% 
  str()
#> 'data.frame':	929 obs. of  32 variables:
#>  $ id             : num  1 2 3 4 5 6 7 8 9 10 ...
#>  $ rx             : Factor w/ 3 levels "Obs","Lev","Lev+5FU": 3 3 1 3 1 3 2 1 2 3 ...
#>  $ sex            : num  1 1 0 0 1 0 1 1 1 0 ...
#>  $ age            : num  43 63 71 66 69 57 77 54 46 68 ...
#>   ..- attr(*, "label")= chr "Age (years)"
#>  $ obstruct       : num  NA 0 0 1 0 0 0 0 0 0 ...
#>  $ perfor         : num  0 0 0 0 0 0 0 0 0 0 ...
#>  $ adhere         : num  0 0 1 0 0 0 0 0 1 0 ...
#>  $ nodes          : num  5 1 7 6 22 9 5 1 2 1 ...
#>  $ status         : num  1 0 1 1 1 1 1 0 0 0 ...
#>  $ differ         : num  2 2 2 2 2 2 2 2 2 2 ...
#>  $ extent         : num  3 3 2 3 3 3 3 3 3 3 ...
#>  $ surg           : num  0 0 0 1 1 0 1 0 0 1 ...
#>  $ node4          : num  1 0 1 1 1 1 1 0 0 0 ...
#>  $ time           : num  1521 3087 963 293 659 ...
#>  $ sex.factor     : Factor w/ 2 levels "Female","Male": 2 2 1 1 2 1 2 2 2 1 ...
#>  $ rx.factor      : Factor w/ 3 levels "Obs","Lev","Lev+5FU": 3 3 1 3 1 3 2 1 2 3 ...
#>  $ obstruct.factor: Factor w/ 2 levels "No","Yes": NA 1 1 2 1 1 1 1 1 1 ...
#>  $ perfor.factor  : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...
#>  $ adhere.factor  : Factor w/ 2 levels "No","Yes": 1 1 2 1 1 1 1 1 2 1 ...
#>  $ differ.factor  : Factor w/ 3 levels "Well","Moderate",..: 2 2 2 2 2 2 2 2 2 2 ...
#>  $ extent.factor  : Factor w/ 4 levels "Submucosa","Muscle",..: 3 3 2 3 3 3 3 3 3 3 ...
#>  $ surg.factor    : Factor w/ 2 levels "Short","Long": 1 1 1 2 2 1 2 1 1 2 ...
#>  $ node4.factor   : Factor w/ 2 levels "No","Yes": 2 1 2 2 2 2 2 1 1 1 ...
#>  $ status.factor  : Factor w/ 2 levels "Alive","Died": 2 1 2 2 2 2 2 1 1 1 ...
#>  $ age.factor     : Factor w/ 3 levels "<40 years","40-59 years",..: 2 3 3 3 3 2 3 2 2 3 ...
#>  $ loccomp        : num  NA 0 1 1 0 0 0 0 1 0 ...
#>  $ loccomp.factor : Factor w/ 2 levels "No","Yes": NA 1 2 2 1 1 1 1 2 1 ...
#>  $ time.years     : num  4.167 8.458 2.638 0.803 1.805 ...
#>   ..- attr(*, "label")= chr "Time (years)"
#>  $ mort_5yr       : Factor w/ 2 levels "Alive","Died": 2 1 2 2 2 2 2 1 1 1 ...
#>  $ age.10         : num  4.3 6.3 7.1 6.6 6.9 5.7 7.7 5.4 4.6 6.8 ...
#>  $ mort_5yr.num   : num  2 1 2 2 2 2 2 1 1 1 ...
#>  $ hospital       : Factor w/ 5 levels "hospital_1","hospital_2",..: 5 3 5 4 5 4 2 2 2 2 ...

# Relabel
colon_s %<>% ff_relabel(vlabels)
colon_s %>% str()
#> 'data.frame':	929 obs. of  32 variables:
#>  $ id             : num  1 2 3 4 5 6 7 8 9 10 ...
#>   ..- attr(*, "label")= chr "id"
#>  $ rx             : Factor w/ 3 levels "Obs","Lev","Lev+5FU": 3 3 1 3 1 3 2 1 2 3 ...
#>   ..- attr(*, "label")= chr "rx"
#>  $ sex            : num  1 1 0 0 1 0 1 1 1 0 ...
#>   ..- attr(*, "label")= chr "sex"
#>  $ age            : num  43 63 71 66 69 57 77 54 46 68 ...
#>   ..- attr(*, "label")= chr "Age (years)"
#>  $ obstruct       : num  NA 0 0 1 0 0 0 0 0 0 ...
#>   ..- attr(*, "label")= chr "obstruct"
#>  $ perfor         : num  0 0 0 0 0 0 0 0 0 0 ...
#>   ..- attr(*, "label")= chr "perfor"
#>  $ adhere         : num  0 0 1 0 0 0 0 0 1 0 ...
#>   ..- attr(*, "label")= chr "adhere"
#>  $ nodes          : num  5 1 7 6 22 9 5 1 2 1 ...
#>   ..- attr(*, "label")= chr "nodes"
#>  $ status         : num  1 0 1 1 1 1 1 0 0 0 ...
#>   ..- attr(*, "label")= chr "status"
#>  $ differ         : num  2 2 2 2 2 2 2 2 2 2 ...
#>   ..- attr(*, "label")= chr "differ"
#>  $ extent         : num  3 3 2 3 3 3 3 3 3 3 ...
#>   ..- attr(*, "label")= chr "extent"
#>  $ surg           : num  0 0 0 1 1 0 1 0 0 1 ...
#>   ..- attr(*, "label")= chr "surg"
#>  $ node4          : num  1 0 1 1 1 1 1 0 0 0 ...
#>   ..- attr(*, "label")= chr "node4"
#>  $ time           : num  1521 3087 963 293 659 ...
#>   ..- attr(*, "label")= chr "time"
#>  $ sex.factor     : Factor w/ 2 levels "Female","Male": 2 2 1 1 2 1 2 2 2 1 ...
#>   ..- attr(*, "label")= chr "Sex"
#>  $ rx.factor      : Factor w/ 3 levels "Obs","Lev","Lev+5FU": 3 3 1 3 1 3 2 1 2 3 ...
#>   ..- attr(*, "label")= chr "Treatment"
#>  $ obstruct.factor: Factor w/ 2 levels "No","Yes": NA 1 1 2 1 1 1 1 1 1 ...
#>   ..- attr(*, "label")= chr "Obstruction"
#>  $ perfor.factor  : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...
#>   ..- attr(*, "label")= chr "Perforation"
#>  $ adhere.factor  : Factor w/ 2 levels "No","Yes": 1 1 2 1 1 1 1 1 2 1 ...
#>   ..- attr(*, "label")= chr "Adherence"
#>  $ differ.factor  : Factor w/ 3 levels "Well","Moderate",..: 2 2 2 2 2 2 2 2 2 2 ...
#>   ..- attr(*, "label")= chr "Differentiation"
#>  $ extent.factor  : Factor w/ 4 levels "Submucosa","Muscle",..: 3 3 2 3 3 3 3 3 3 3 ...
#>   ..- attr(*, "label")= chr "Extent of spread"
#>  $ surg.factor    : Factor w/ 2 levels "Short","Long": 1 1 1 2 2 1 2 1 1 2 ...
#>   ..- attr(*, "label")= chr "Time from surgery"
#>  $ node4.factor   : Factor w/ 2 levels "No","Yes": 2 1 2 2 2 2 2 1 1 1 ...
#>   ..- attr(*, "label")= chr ">4 positive nodes"
#>  $ status.factor  : Factor w/ 2 levels "Alive","Died": 2 1 2 2 2 2 2 1 1 1 ...
#>   ..- attr(*, "label")= chr "Status"
#>  $ age.factor     : Factor w/ 3 levels "<40 years","40-59 years",..: 2 3 3 3 3 2 3 2 2 3 ...
#>   ..- attr(*, "label")= chr "Age"
#>  $ loccomp        : num  NA 0 1 1 0 0 0 0 1 0 ...
#>   ..- attr(*, "label")= chr "loccomp"
#>  $ loccomp.factor : Factor w/ 2 levels "No","Yes": NA 1 2 2 1 1 1 1 2 1 ...
#>   ..- attr(*, "label")= chr "Local complications"
#>  $ time.years     : num  4.167 8.458 2.638 0.803 1.805 ...
#>   ..- attr(*, "label")= chr "Time (years)"
#>  $ mort_5yr       : Factor w/ 2 levels "Alive","Died": 2 1 2 2 2 2 2 1 1 1 ...
#>   ..- attr(*, "label")= chr "Mortality 5 year"
#>  $ age.10         : num  4.3 6.3 7.1 6.6 6.9 5.7 7.7 5.4 4.6 6.8 ...
#>   ..- attr(*, "label")= chr "age.10"
#>  $ mort_5yr.num   : num  2 1 2 2 2 2 2 1 1 1 ...
#>   ..- attr(*, "label")= chr "mort_5yr.num"
#>  $ hospital       : Factor w/ 5 levels "hospital_1","hospital_2",..: 5 3 5 4 5 4 2 2 2 2 ...
#>   ..- attr(*, "label")= chr "hospital"