[英]Can't draw histogram using dplyr data manipulation: 'x' must be numeric
我是使用R进行数据操作的新手,特别是dplyr,我试图编写一行代码以生成直方图,这给我一个错误,即“ x”必须是数字,但从我的判断中可以看出“ x”是数字。
我尝试运行的代码版本如下,并带有相应的错误:
mydata %>% filter(Type==1) %>% select(Amount) %>% hist()
Error in hist.default(.) : 'x' must be numeric
如果我将其更改为如下所示的箱线图,则可以正常工作:
mydata %>% filter(Type==1) %>% select(Amount) %>% boxplot()
如果我只是跑步:
mydata %>% filter(Type==1) %>% select(Amount)
我得到以下结果,将值显示为dbl:
# A tibble: 898 x 1
Amount
<dbl>
1 -1304.
2 -741.
3 -38.0
4 -1.13
5 0.
6 0.
7 0.
8 0.
9 0.
10 0.
# ... with 888 more rows**strong text**
另外,如果我运行以下代码,我会得到我想要的直方图,但是我不确定为什么我的原始代码行不起作用。
tmp <- mydata %>% filter(Type==1) %>% select(Amount)
hist(tmp$Amount)
我确信这是我遗失的简单东西,但我认为使用select(Amount)语句会将该值传递给hist()函数,但似乎没有发生。 对我来说,奇怪的是,我可以将其更改为箱线图,并且可以正常工作。 关于我的原始代码有什么问题的任何想法?
从?hist
和?boxplot
, hist
接受数字矢量作为数据,而boxplot
可以接受数字矢量或列表。
要从管道返回数字矢量,可以使用pull
将列提取为矢量,然后将其传递给hist
进行绘图:
mydata %>% filter(Type==1) %>% pull(Amount) %>% hist()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.