[R] histogram error: 'x' must be numeric

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Fri Feb 10 14:59:25 CET 2006


jia ding wrote:

> Then, I use command:
> score<- read.csv('file.csv', header = FALSE,sep = ",")
> hist(score, main = "score")
> 
> it gives error msg:
> Error in hist.default("score", main = "score") :
>         'x' must be numeric
> 
> Can any of you know about it explain me why?

  Have a look at 'score' in R first. You might get this:

 > score
        V1
1 31.8450
2 24.5980
3 29.1223
4 24.7150
5 23.1847
6 24.2321
7 25.2995
8 23.4261
9 30.7873

  - read.csv reads things in into "data frames" - a bit like a matrix. 
You've read your data into a data frame with one column, so you can do:

  hist(score$V1)

  since V1 is the name of the column.

  If your data is just one column, then you could do:

  score = scan("file.csv")

  and then

  hist(score)

  since scan() has read it into a single vector, not a data frame.

Barry




More information about the R-help mailing list