Merge Factors without Converting Levels to Number in R (Example Code)
In this tutorial you’ll learn how to keep factor levels when merging two factors in the R programming language.
Creating Example Data
vect1 <- factor(c(LETTERS[1:5])) # Construct two factor vectors vect1 # [1] A B C D E # Levels: A B C D E |
vect1 <- factor(c(LETTERS[1:5])) # Construct two factor vectors vect1 # [1] A B C D E # Levels: A B C D E
vect2 <- factor(c(LETTERS[3:10])) vect2 # [1] C D E F G H I J # Levels: C D E F G H I J |
vect2 <- factor(c(LETTERS[3:10])) vect2 # [1] C D E F G H I J # Levels: C D E F G H I J
Example: How to Combine Multiple Factors & Keep Factor Levels
unlist(list(vect1, vect2)) # Merge two factors # [1] A B C D E C D E F G H I J # Levels: A B C D E F G H I J |
unlist(list(vect1, vect2)) # Merge two factors # [1] A B C D E C D E F G H I J # Levels: A B C D E F G H I J