How to Add New Level to Factor in R

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

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!

Example

First, let’s create a sample dataset:

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

animal
[1] "dog"  "cat"  "bird" "dog"  "cat"  "fish"
levels(animal_factor)
[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)
levels(animal_factor)
[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!

To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)