hist.default(xa)中的错误:"x"必须为数字 [英] Error in hist.default(xa) : 'x' must be numeric
问题描述
我是一名完全R的初学者,并且正在尝试做一些非常基础的事情-制作我从Excel导入的两个向量的直方图.
I'm a complete R beginner, and am trying to do something pretty basic - make histograms of two vectors I imported from Excel.
向量是xa和xb.我尝试了hist(xa),并收到以下错误:
The vectors are xa and xb. I tried hist(xa), and get the following error:
Error in hist.default(xa) : 'x' must be numeric
因此,我进行了一些搜索,并尝试使用as.numeric(xa)对此进行补救,并得到:
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'
所以我尝试了as.list函数,但是它将我的向量变成了矩阵.不太确定发生了什么.向量中的数字都是介于-2到+10之间的所有4位数字.任何帮助将不胜感激!
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!
推荐答案
您可以尝试以下方法...不能保证,因为您没有给出有效的示例:
Here's something you can try... no guarantees, since you have not given a working example:
newXa <- sapply(xa, as.numeric)
hist(newXa)
应该做的是看看'x'的结构
What should be done is to look at the structure of 'x'
str(x)
然后,如果'xa'是指x [['a']]的方式,您将执行以下操作:
Then if 'xa' is how you are referring to x[['a']] you would do this:
hist( x[['a']] )
如果str(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']])) )
这篇关于hist.default(xa)中的错误:"x"必须为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!