The fct_unify()
function from Hadley Wickham's forcats
package unifies the levels in a list of factors.
# using G. Grothendieck's test data frame
DF <- as.data.frame(matrix(letters,, 2), stringsAsFactors = FALSE)
str(DF)
'data.frame': 13 obs. of 2 variables:
$ V1: chr "a" "b" "c" "d" ...
$ V2: chr "n" "o" "p" "q" ...
DF[] <- lapply(DF, factor)
str(DF)
'data.frame': 13 obs. of 2 variables:
$ V1: Factor w/ 13 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10 ...
$ V2: Factor w/ 13 levels "n","o","p","q",..: 1 2 3 4 5 6 7 8 9 10 ...
DF[] <- forcats::fct_unify(DF)
str(DF)
'data.frame': 13 obs. of 2 variables:
$ V1: Factor w/ 26 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10 ...
$ V2: Factor w/ 26 levels "a","b","c","d",..: 14 15 16 17 18 19 20 21 22 23 ...
or as a one-liner to produce the numbers of the unified factor levels:
DF[] <- lapply(forcats::fct_unify(lapply(DF, factor)), as.numeric)
DF
V1 V2
1 1 14
2 2 15
3 3 16
4 4 17
5 5 18
6 6 19
7 7 20
8 8 21
9 9 22
10 10 23
11 11 24
12 12 25
13 13 26