Function to that looks for VARN counterparts to any character or factor VAR variables in any dataframe and re-orders there factor levels, taking the lead from VARN's numeric guide.
varN_fctr_reorder(data)
a dataframe, including one enriched with SAS labels attributes
The data frame after having factor levels re-ordered by VARN
data(adae, package = "tidyCDISC")
varN_fctr_adae <- varN_fctr_reorder(adae)
unique(adae[,c("AGEGR1", "AGEGR1N")])
#> # A tibble: 3 × 2
#> AGEGR1 AGEGR1N
#> <chr> <dbl>
#> 1 <65 1
#> 2 65-80 2
#> 3 >80 3
levels(adae$AGEGR1)
#> NULL
levels(varN_fctr_adae$AGEGR1)
#> [1] "<65" "65-80" ">80"
unique(adae[,c("RACE", "RACEN")])
#> # A tibble: 3 × 2
#> RACE RACEN
#> <chr> <dbl>
#> 1 WHITE 1
#> 2 BLACK OR AFRICAN AMERICAN 2
#> 3 AMERICAN INDIAN OR ALASKA NATIVE 6
levels(adae$RACE)
#> NULL
levels(varN_fctr_adae$RACE)
#> [1] "WHITE" "BLACK OR AFRICAN AMERICAN"
#> [3] "AMERICAN INDIAN OR ALASKA NATIVE"