R/utility.R
char2factor.Rd
Convert character variable to a factor based off it's numeric variable counterpart.
char2factor(df, c_var, n_var)
data frame.
character variable within the data frame.
numeric variable counter part within the data frame to control the levels.
A factor.
df <- tibble::tribble(
~TRT01P, ~TRT01PN,
"Placebo", 1,
"Low Dose", 2,
"High Dose", 3
)
# alphabetical order
dplyr::arrange(df, TRT01P)
#> # A tibble: 3 × 2
#> TRT01P TRT01PN
#> <chr> <dbl>
#> 1 High Dose 3
#> 2 Low Dose 2
#> 3 Placebo 1
# change to factor with char2factor
df$TRT01P <- char2factor(df, "TRT01P", "TRT01PN")
# factor order
dplyr::arrange(df, TRT01P)
#> # A tibble: 3 × 2
#> TRT01P TRT01PN
#> <fct> <dbl>
#> 1 Placebo 1
#> 2 Low Dose 2
#> 3 High Dose 3