R Warning Message: NAs Introduced by Coercion (Example)

 

This article explains how to debug the warning message “NAs introduced by coercion” in the R programming language.

The content of the post is structured as follows:

Let’s dive into it…

 

Creation of Example Data

First, I’ll have to create some example data.

vec <- c("50", "200", "1,000", "10", "1200", "2,100")  # Create example vector
vec                                                    # Print example vector
# [1] "50"    "200"   "1,000" "10"    "1200"  "2,100"

Have a look at the previous RStudio console output. It shows that our example data is a vector of character strings containing six vector elements.

 

Example 1: Reproduce the Warning Message: NAs Introduced by Coercion

In this example, I’ll show how to replicate the warning message “NAs introduced by coercion” when using the as.numeric function in R. Let’s apply the as.numeric function to our example vector:

as.numeric(vec)                                        # Applying as.numeric function
# [1]   50  200   NA   10 1200   NA
# Warning message:
# NAs introduced by coercion

As you can see, the warning message “NAs introduced by coercion” is returned and some output values are NA (i.e. missing data or not available data).

The reason for this is that some of the character strings are not properly formatted numbers and hence cannot be converted to the numeric class.

The next example shows how to solve this problem in R.

 

Example 2: Modify Data to Avoid Warning Message Using gsub() Function

In Example 2, I’ll illustrate how to handle the as.numeric() warning message “NAs introduced by coercion”.

As explained before, some of our input values are not formatted properly, because they contain commas (i.e. ,) between the numbers. We can remove these commas by using the gsub function:

vec_new <- gsub(",", "", vec)                          # Applying gsub function
vec_new                                                # Print updated example vector
# [1] "50"   "200"  "1000" "10"   "1200" "2100"

Have a look at the previous output of the RStudio console. It shows that our updated vector does not contain commas anymore.

Now, let’s apply the as numeric function again:

as.numeric(vec_new)                                    # Applying as.numeric function
# [1]   50  200 1000   10 1200 2100

As you can see, we did not only avoid the warning message, we also created an output vector without any NA values.

 

Example 3: Suppress Warning Message Using suppressWarnings() Function

Sometimes you might not want to convert non-number values to numeric. In this case, you can simply ignore the warning message “NAs introduced by coercion” by wrapping the suppressWarnings function around the as.numeric function:

suppressWarnings(as.numeric(vec))                      # Applying suppressWarnings function
# [1]   50  200   NA   10 1200   NA

The output is the same as in Example 1, but this time without printing the warning message to the RStudio console.

 

Video, Further Resources & Summary

Do you want to know more about warnings and errors in R? Then I can recommend watching the following video of my YouTube channel. In the video, I’m explaining the R programming codes of this tutorial in a live programming session.

 

The YouTube video will be added soon.

 

In addition, you might have a look at the related articles of my homepage. You can find some tutorials about warning and error messages below.

 

Summary: In this post, I explained how to get rid of the warning “NAs introduced by coercion” when converting a character or factor variable to numeric in the R programming language.

In case you have further questions, don’t hesitate to let me know in the comments section. Furthermore, please subscribe to my email newsletter in order to get updates on new tutorials.

 

Subscribe to the Statistics Globe Newsletter

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


2 Comments. Leave new

  • Hi Joachim, thanks for this tutorial and your help in advance!
    I am having the error NAs introduced by coercion. In my case however I am trying to reformat the string characters (e.g. green, blue, red) from two specific columns so that the characters (e.g. green) is represented by a numeric number (e.g. 1) I’ve been trying a long while now so that I can use the data as part of a neural network but cannot get past this error. Can you help me with this?

    Reply
    • Hi Ali,

      Thank you for the very kind words and your interesting question. A simple solution might be the following:

      x <- c("green", "red", "green", "blue", "blue", "yellow")
      x_num <- as.numeric(as.factor(x_fac))
      x_num
      [1] 2 3 2 1 1 4

      Note that the numeric output would be based on the alphabetic order of the input vector.

      I hope that helps!

      Joachim

      Reply

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