Replace Character Value with NA in R (2 Examples)

 

In this tutorial, I’ll show how to replace characters in vectors and data frame columns by NA in the R programming language.

Table of contents:

Let’s get started!

 

Example 1: Replace Specific Character Value with NA in Vector

In Example 1, I’ll illustrate how to set particular characters in a vector to NA (i.e. missing value).

Consider the following example vector in R:

vec <- c("A", "B", "A", "C", "B")        # Create example vector
vec                                      # Print example vector
# [1] "A" "B" "A" "C" "B"

As you can see based on the previous output of the RStudio console, our example vector contains a sequence of uppercase letters.

Let’s assume that we want to exchange the character letter “A” by NA. Then, we can use the following R code:

vec_NA <- vec                            # Duplicate vector
vec_NA[vec_NA == "A"] <- NA              # Replace particular value with NA
vec_NA                                   # Print updated vector
# [1] NA  "B" NA  "C" "B"

Have a look at the previous output: We have created a new character vector called vec_NA that contains Not Available values at the index positions where our input vector had the character value “A”.

 

Example 2: Replace Specific Character Value with NA in Data Frame

In this example, I’ll show how to replace characters in data frame variables by NA.

First, we have to create some example data:

data <- data.frame(x1 = letters[1:5],    # Create example data frame
                   x2 = letters[6:2],
                   x3 = letters[3:7])
data                                     # Print example  data frame

 

table 1 data frame replace character value na r

 

As shown in Table 1, the previous R syntax has created a data frame with five rows and three columns. All the columns contain characters.

Now, we can replace a particular character value (i.e. “c”) with NA using a logical condition:

data_NA <- data                          # Duplicate data frame
data_NA[data_NA == "c"] <- NA            # Replace particular value with NA
data_NA                                  # Print updated data frame

 

table 2 data frame replace character value na r

 

In Table 2 it is shown that we have created a new data frame containing NA values using the previous R programming code.

 

Video & Further Resources

In case you need more info on the content of this article, you might want to watch the following video of my YouTube channel. I illustrate the topics of this article in the video:

 

The YouTube video will be added soon.

 

In addition, you may want to have a look at the other articles on my website. I have released several related tutorials on topics such as dplyr, merging, and missing data.

 

At this point you should have learned how to replace certain character strings with NA in the R programming language. Let me know in the comments section, in case you have additional questions. Furthermore, please subscribe to my email newsletter in order to receive updates on the newest articles.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


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.

Menu
Top