How to Avoid R Warning Message – NAs Introduced by Coercion (2 Examples)

This tutorial shows how to warning message “NAs introduced by coercion” in R.

Creation of Example Data

my_data <- c(1:5, "X")                   # Constructing character vector
my_data                                  # Show vector in RStudio console
# [1] "1" "2" "3" "4" "5" "X"

Example 1: Reproducing the Warning in R: NAs Introduced by Coercion

as.numeric(my_data)                      # Converting character to numeric
# [1]  1  2  3  4  5 NA
# Warning message:
# NAs introduced by coercion

Example 2: Suppressing the Warning in R with suppressWarnings Function

suppressWarnings(as.numeric(my_data))    # Suppressing warning messages
# [1]  1  2  3  4  5 NA

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