Organizes the levels to use for the two inputs. This is useful for when one wants to merge two vectors that are factors. Ideally, they have the same levels, in which case those are returned. If they have overlapping levels, then the levels are merged and sorted (using sort()). Otherwise, the levels are dropped (returning NULL)

organize_levels(x, y, ...)

Arguments

x, y

Two factor vectors

...

passed to sort; see ?sort for options

Value

vector of levels or NULL for no levels

Examples

set.seed(0) a <- factor(sample(LETTERS, 5), levels = LETTERS) b <- factor(sample(LETTERS, 5), levels = LETTERS) a
#> [1] N Y D G A #> Levels: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
b
#> [1] B W K N R #> Levels: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
organize_levels(a, b)
#> [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" #> [20] "T" "U" "V" "W" "X" "Y" "Z"