How to Add New Level to Factor in R (With Example)

You can use the following basic syntax to add a new level to a factor variable in R:

levels(df$my_factor) <- c(levels(df$my_factor), 'new_level')

The following example shows how to use this syntax in practice.

Example: Add New Level to Factor in R

Suppose we have the following data frame in R that shows the number of sales made in different regions for some retail store:

#create data frame
df <- data.frame(region=factor(c('A', 'B', NA, 'D', NA, 'F')),
                 sales=c(12, 18, 21, 14, 34, 40))

#view data frame

  region sales
1      A    12
2      B    18
3   <NA>    21
4      D    14
5   <NA>    34
6      F    40

Notice that the region variable is a factor.

To view the levels for this factor, we can use the levels() function:

#view factor levels for region

[1] "A" "B" "D" "F"

We can use the following syntax to add a new factor level called “no region”:

#add factor level called 'no region'
levels(df$region) <- c(levels(df$region), 'no region')

#convert each NA to 'no region'
df$region[$region)] <- 'no region'

#view factor levels for region

[1] "A" "B" "D" "F" "no region"

The new level called “no region” has been added as a factor level.

If we’d like, we can use the table() function to count the occurrence of each factor level:

#view occurrences of each factor level

A         B         D         F no region 
1         1         1         1         2 

From the output we can see that the new factor level called “no region” occurs twice in the region column of the data frame.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Convert Factor to Numeric in R
How to Convert Factor to Character in R
How to Reorder Factor Levels in R

Leave a Reply

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