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" |
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 |
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 |
suppressWarnings(as.numeric(my_data)) # Suppressing warning messages # [1] 1 2 3 4 5 NA