无法绘制直方图,"x"必须为数字 [英] Can't draw Histogram, 'x' must be numeric

查看:164
本文介绍了无法绘制直方图,"x"必须为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下格式的数据文件:

I have a data file with this format:

Weight    Industry Type  
251,787   Kellogg  h  
253,9601  Kellogg  a  
256,0758  Kellogg  h  
....

我读取了数据并尝试使用以下命令绘制直方图:

I read the data and try to draw an histogram with this commands:

 ce <- read.table("file.txt", header = TRUE)

 we = ce[,1]
 in = ce[,2]
 ty = ce[,3]

hist(we)

但是我得到这个错误:

错误hist.default(we):"x"必须为数字.

Error en hist.default(we) : 'x' must be numeric.

要为三个变量绘制直方图,我需要做什么?

What do I need to do in order to draw histograms for my three variables ?

推荐答案

由于千位分隔符,数据将被读取为非数字".因此,您需要对其进行转换:

Because of the thousand separator, the data will have been read as 'non-numeric'. So you need to convert it:

 we <- gsub(",", "", we)   # remove comma
 we <- as.numeric(we)      # turn into numbers

现在您可以做

 hist(we)

和其他数字运算.

这篇关于无法绘制直方图,"x"必须为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆