4 C
London
Friday, December 20, 2024
HomeStatistics TutorialRHow to Add New Level to Factor in R (With Example)

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

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

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 frame(region=factor(c('A', 'B', NA, 'D', NA, 'F')),
                 sales=c(12, 18, 21, 14, 34, 40))

#view data frame
df

  region sales
1      A    12
2      B    18
3       21
4      D    14
5       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
levels(df$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) no region')

#convert each NA to 'no region'
df$region[is.na(df$region)] no region'

#view factor levels for region
levels(df$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
table(df$region)

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories