Example 2: Error in dataframe ‘x’ must be numeric
Similarly here we will create dataframe with string data elements and try to plot hist().
How to produce an error:
R
# Create data for chart val <- data.frame ( "num" = c ( "77" , "55" , "80" , "60" ), "course" = c ( 'DSA' , 'C++' , 'R' , 'Python' )) print (val) hist (val[,1]) |
Output:
num course 1 77 DSA 2 55 C++ 3 80 R 4 60 Python Error in hist.default(val[, 1]): 'x' must be numeric Traceback:
In the above example, we have seen when we try to plot hist then it produce an error due to string data which means the histogram must be in numeric data.
How to solve this error:
To solve this error we will convert the dataframe element into numeric data using as.numeric() methods.
R
# Create data for chart val <- data.frame ( "num" = c (77,55,80,60), "course" = c ( 'DSA' , 'C++' , 'R' , 'Python' )) print (val) hist (val[,1]) |
Output:
How to Fix: ‘x’ must be numeric in R
In this article, we are going to see how to fix: ‘x’ must be numeric. For this we will cover two example for the error message “x must be numeric”.