Extracts the factor levels of a vector or returns the unique values if the vector is not a factor.

get_levels(x)

Arguments

x

a vector

Value

x vector

References

A character vector containing the levels of the factor/vector

Examples

data(adae, package = "tidyCDISC")

# Create levels based on VARN
varN_fctr_adae <- varN_fctr_reorder(adae)

# `adae` does not have factor but `varN_fctr_adae` does
levels(adae$RACE)
#> NULL
levels(varN_fctr_adae$RACE)
#> [1] "WHITE"                            "BLACK OR AFRICAN AMERICAN"       
#> [3] "AMERICAN INDIAN OR ALASKA NATIVE"

# `get_levels()` either creates the factor or retrieves it
get_levels(adae$RACE)
#> [1] "AMERICAN INDIAN OR ALASKA NATIVE" "BLACK OR AFRICAN AMERICAN"       
#> [3] "WHITE"                           
get_levels(varN_fctr_adae$RACE)
#> [1] "WHITE"                            "BLACK OR AFRICAN AMERICAN"       
#> [3] "AMERICAN INDIAN OR ALASKA NATIVE"