Find Non-Numeric Values in R (Example)
In this post, I’ll illustrate how to identify non-numeric values in a vector or a data frame column in the R programming language.
The tutorial will contain these contents:
Let’s get started!
Constructing Exemplifying Data
To begin with, we have to create some data that we can use in the example code later on:
x <- c(1, 2, 3, "x", 4, "5,5") # Create example vector x # Print example vector #  "1" "2" "3" "x" "4" "5,5"
The previous output of the RStudio console shows that our exemplifying data is a vector object that is currently formatted as a character. This is because not all values in our vector are numerical.
Let’s check which of these values are non-numeric!
Example: Identify Non-Numeric Values Using as.numeric(), is.na() & which() Functions
In this example, I’ll illustrate how to find the index location and the values that are non-numeric.
x_nonum <- which(is.na(as.numeric(x))) # Get indices of non-numeric # Warning message: # In which(is.na(as.numeric(x))) : NAs introduced by coercion
Note that the previous R code has returned the warning message “NAs introduced by coercion“.
This is because the as.numeric function automatically converts non-numeric data to NA (and returns a warning that it has done so).
We can use this fact to identify the index positions of those NA values by applying the is.na and which functions to the output of the as.numeric function.
We have stored those indices in the data object x_nonum:
x_nonum # Print indices of non-numeric #  4 6
As you can see, the fourth and sixth elements of our vector contain non-numeric values.
We can also print these data to the RStudio console:
x[x_nonum] # Print non-numeric values #  "x" "5,5"
The vector elements “x” and “5,5” are not numerical.
Note: In this tutorial, I have explained how to find non-numeric values in a vector object. However, it would also be possible to search for non-numeric values in a data frame variable using basically the same syntax as in the present tutorial.
Video, Further Resources & Summary
Would you like to know more about the testing for non-numeric values in a data object? Then I recommend having a look at the following video on my YouTube channel. I illustrate the content of this article in the video.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you may have a look at the related articles on https://www.statisticsglobe.com/. You can find some tutorials on topics such as data inspection, vectors, and lists below:
- Find Missing Values (6 Examples for Data Frame, Column & Vector)
- Find Unique Values in List in R
- Find Values Contained in First Vector but not Another
- R Programming Tutorials
To summarize: In this R tutorial you have learned how to check for non-numeric values in a data object. Please let me know in the comments below, if you have further questions.
Statistics Globe Newsletter