If two data frames have factors ou characters columns with the same column names, those in the first will have the same level order as those in the second. So, for factors, these data frames will have the same levels in the same order. Characters in the first will be converted to factor.

equallevels(x, y)

Arguments

x

the target data frame.

y

the reference data frame.

Value

the restructured data frame.

Examples

a <- data.frame(Species = as.character( sample(iris$Species, size = 20, repl = TRUE)), stringsAsFactors = FALSE) str(a)
#> 'data.frame': 20 obs. of 1 variable: #> $ Species: chr "setosa" "versicolor" "setosa" "virginica" ...
b <- equallevels(a, iris) str(b)
#> 'data.frame': 20 obs. of 1 variable: #> $ Species: Factor w/ 3 levels "setosa","versicolor",..: 1 2 1 3 2 3 3 1 2 3 ...