- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to deal with hist.default, 'x' must be numeric in R?
The error hist.default, 'x' must be numeric occurs when we pass a non-numerical column or vector to the hist function. If we have a non-numerical column in a data frame or a non-numerical vector and we want to create the histogram of that data can be created with the help of barplot and table function.
Check out the below given Example
to understand how it can be done.
Following snippet creates a sample data frame −
x<-sample(LETTERS[1:5],20,replace=TRUE) df<-data.frame(x) df
The following dataframe is created
x 1 C 2 B 3 B 4 B 5 E 6 E 7 C 8 A 9 C 10 B 11 D 12 E 13 A 14 C 15 B 16 A 17 C 18 E 19 B 20 B
To create histogram of x with hist function on the above created data frame, add the following code to the above snippet −
x<-sample(LETTERS[1:5],20,replace=TRUE) df<-data.frame(x) hist(df$x)
To create histogram of x with barplot function on the above created data frame, add the following code to the above snippet −
x<-sample(LETTERS[1:5],20,replace=TRUE) df<-data.frame(x) barplot(table(df$x))
If you execute all the above given snippets as a single program, it generates the following Output −
- How to deal with error 'height' must be a vector or a matrix while creating barplot?
- How to deal with error “Error in eval(predvars, data, env) : numeric 'envir' arg not of length one” in R?
- How to deal with 'Boolean' values in PHP & MySQL?
- How to deal with error “Error in shapiro.test(…) : sample size must be between 3 and 5000” in R?
- Update 'a' record with 'b' and 'b' with 'a' in a MySQL column (swap) with only 'a' and 'b' values?
- Replace '*' with '^' with Java Regular Expressions
- How to adjust 'tick frequency' in Matplotlib for string X-axis?
- Program to find number of string we can make where 'a' can be 'a' or 'b', and 'b' remains 'b'in Python
- How to deal with error “var(x) : Calling var(x) on a factor x is defunct.” in R?
- How to Use 'U' and 'L' formatters in Arduino?
- How to use wildcards like $ ('#name*'), $ ('#name%') in jQuery selectors?
- How to use the 'break' and 'next' statements in Ruby?
- How to change the 'text-decoration' property with jQuery?
- MySQL where column = 'x, y, z'?