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
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

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top