Table of Contents
The required data type for ‘x’ in R to fix the error “x must be numeric” is a numeric data type. This means that the value of ‘x’ must be a number or a numerical expression in order for the code to run without any errors. Using any other data type such as character or logical will result in the aforementioned error. Properly specifying the data type of ‘x’ as numeric will ensure the program runs smoothly and produces the desired outcome.
Fix in R: ‘x’ must be numeric
One error you may encounter in R is:
Error in hist.default(data) : 'x' must be numeric
This error occurs when you attempt to create a histogram for a variable that is not numeric.
This tutorial shares exactly how to fix this error.
How to Reproduce the Error
Suppose we attempt to create a histogram for the following vector of data:
#define vector data <- c('1.2', '1.4', '1.7', '1.9', '2.2', '2.5', '3', '3.4', '3.7', '4.1') #attempt to create histogram to visualize distribution of values in vector hist(data) Error in hist.default(data) : 'x' must be numeric
We receive an error because data is currently not a numeric vector. We can confirm this by checking the class:
#check class
class(data)
[1] "character"
Currently data is a character vector.
How to Fix the Error
The easiest way to fix this error is to simply use as.numeric() to convert our vector to numeric:
#convert vector from character to numeric data_numeric <- as.numeric(data) #create histogram hist(data_numeric)

Notice that we don’t receive an error and we’re able to successfully create the histogram because our vector is now numeric.
We can verify this by checking the class:
#check class
class(data_numeric)
[1] "numeric"
The following tutorials explain how to fix other common errors in R:
Cite this article
stats writer (2024). What is the required data type for ‘x’ in R in order to fix the error “x must be numeric”?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/what-is-the-required-data-type-for-x-in-r-in-order-to-fix-the-error-x-must-be-numeric/
stats writer. "What is the required data type for ‘x’ in R in order to fix the error “x must be numeric”?." PSYCHOLOGICAL SCALES, 3 May. 2024, https://scales.arabpsychology.com/stats/what-is-the-required-data-type-for-x-in-r-in-order-to-fix-the-error-x-must-be-numeric/.
stats writer. "What is the required data type for ‘x’ in R in order to fix the error “x must be numeric”?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/what-is-the-required-data-type-for-x-in-r-in-order-to-fix-the-error-x-must-be-numeric/.
stats writer (2024) 'What is the required data type for ‘x’ in R in order to fix the error “x must be numeric”?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/what-is-the-required-data-type-for-x-in-r-in-order-to-fix-the-error-x-must-be-numeric/.
[1] stats writer, "What is the required data type for ‘x’ in R in order to fix the error “x must be numeric”?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. What is the required data type for ‘x’ in R in order to fix the error “x must be numeric”?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
