How to Add New Level to Factor in R


Steven P. Sanderson II, MPH


March 6, 2024


As an R programmer, working with categorical data is a common task, and factors (a data type in R) are used to represent categorical variables. However, sometimes you may encounter a situation where you need to add a new level to an existing factor. This could happen when you have new data that includes a category not present in your original dataset.

In this blog post, we’ll explore how to add a new level to a factor in R using base R functions. Let’s dive in!


First, let’s create a sample dataset:

# Create a sample dataset
animal <- c("dog", "cat", "bird", "dog", "cat", "fish")
animal_factor <- factor(animal)

[1] "dog"  "cat"  "bird" "dog"  "cat"  "fish"
[1] "bird" "cat"  "dog"  "fish"

Here, we’ve created a character vector called animal and converted it into a factor called animal_factor.

Now, let’s say we want to add a new level “reptile” to our animal_factor. We can do this using the levels() function:

# Add a new level to the factor
new_levels <- c(levels(animal_factor), "reptile")
animal_factor <- factor(animal_factor, levels = new_levels)
[1] "bird"    "cat"     "dog"     "fish"    "reptile"

Here’s what the code does:

  1. new_levels <- c(levels(animal_factor), "reptile"): This line creates a new vector called new_levels that contains all the existing levels from animal_factor plus the new level “reptile”.
  2. animal_factor <- factor(animal_factor, levels = new_levels): This line recreates the animal_factor object as a factor, but with the levels specified in new_levels.
  3. levels(animal_factor): This line prints the updated levels of the animal_factor, which now includes “reptile”.

You see that the output is:

[1] "bird" "cat"  "dog"  "fish" "reptile"

As you can see, the new level “reptile” has been added to the factor animal_factor.

It’s important to note that adding a new level to a factor doesn’t change the existing data; it simply allows for the possibility of including the new level in future data.

Now that you’ve learned how to add a new level to a factor in R, it’s your turn to practice! Try creating your own dataset and experiment with adding new levels to factors. You can also explore other related functions, such as levels<-() and addNA(), which can be useful when working with factors.

Remember, practice makes perfect, so keep coding and exploring the world of R!