r- hist.default, 'x' must be numeric
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))
![Charistine](https://i.stack.imgur.com/0BqtP.png?s=256&g=1)
Comments
-
Charistine about 2 years
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
.- Why can't it count the instances of each different string like it can for the other columns?
- What would be the simplest code for this?
-
Charistine almost 10 yearsThank you. I'm a complete newbie to r. Now that I created this bar chart, I see that I have a lot of results in the hundreds and a few that just have a 1 or 2. Is there any way to exclude the data if it does not meet a minimum number of instances?
-
MrFlick almost 10 years@user3594525 I've added a possible solution in my answer.