Error in hist.default(xa) : 'x' must be numeric

眉间皱痕 提交于 2019-12-10 18:20:58

问题


I'm a complete R beginner, and am trying to do something pretty basic - make histograms of two vectors I imported from Excel.

The vectors are xa and xb. I tried hist(xa), and get the following error:

Error in hist.default(xa) : 'x' must be numeric

So I did some searching, and tried to remedy this using as.numeric(xa), and got:

Error: (list) object cannot be coerced to type 'double'

So I tried the as.list function, but it turned my vector into a matrix. Not really sure what's going on. The numbers in the vectors are all 4 digits between about -2 and +10. Any help would be greatly appreciated!


回答1:


Here's something you can try... no guarantees, since you have not given a working example:

newXa <- sapply(xa, as.numeric)
hist(newXa)

What should be done is to look at the structure of 'x'

str(x)

Then if 'xa' is how you are referring to x[['a']] you would do this:

hist( x[['a']] )

And if str(x) showed that the "a" column were a factor, one might have more success with this:

 hist( as.numeric(as.character(x[['a']]))  )


来源:https://stackoverflow.com/questions/12633586/error-in-hist-defaultxa-x-must-be-numeric

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!