Turn NA into Factor Level Using addNA() Function in R (Example Code)

In this R tutorial you’ll learn how to apply the addNA function.

Constructing Example Data

x <- factor(c("xxx", "yo", NA, NA,    # Create factor vector in R
              "yo", NA, "xxx", NA))
x                                     # Display example factor in RStudio console
# [1] xxx  yo   <NA> <NA> yo   <NA> xxx  <NA>
# Levels: xxx yo

Example: Converting NA Values into Factor Level Using addNA() Function

x_new <- addNA(x)                     # Using addNA function
x_new                                 # Display new factor vector
# [1] xxx  yo   <NA> <NA> yo   <NA> xxx  <NA>
# Levels: xxx yo <NA>

Related Articles

Here, you may find some additional resources on topics such as factors, numeric values, character strings, and data conversion.

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