Charistine
Charistine

Reputation: 61

r- hist.default, 'x' must be numeric

Just picking up R and I have the following question:

Say I have the following data.frame:

v1     v2     v3  
3      16     a  
44     457    d  
5      23     d  
34     122    c  
12     222    a

...and so on

I would like to create a histogram or barchart for this in R, but instead of having the x-axis be one of the numeric values, I would like a count by v3. (2 a, 1 c, 2 d...etc.)

If I do hist(dataFrame$v3), I get the error that 'x 'must be numeric.

  1. Why can't it count the instances of each different string like it can for the other columns?
  2. What would be the simplest code for this?

Upvotes: 5

Views: 12966

Answers (1)

MrFlick
MrFlick

Reputation: 206401

OK. First of all, you should know exactly what a histogram is. It is not a plot of counts. It is a visualization for continuous variables that estimates the underlying probability density function. So do not try to use hist on categorical data. (That's why hist tells you that the value you pass must be numeric.)

If you just want counts of discrete values, that's just a basic bar plot. You can calculate counts of values in R for discrete data using table and then plot that with the basic barplot() command.

barplot(table(dataFrame$v3))

If you want to require a minimum number of observations, try

tbl<-table(dataFrame$v3)
atleast <- function(i) {function(x) x>=i}
barplot(Filter(atleast(10), tbl))

Upvotes: 9

Related Questions