我有一个这种格式的数据文件:
Weight Industry Type
251,787 Kellogg h
253,9601 Kellogg a
256,0758 Kellogg h
....
我阅读了数据并尝试使用以下命令绘制直方图:
ce <- read.table("file.txt", header = TRUE)
we = ce[,1]
in = ce[,2]
ty = ce[,3]
hist(we)
但我收到此错误:
Error en hist.default(we) : 'x' must be numeric.
我需要做什么才能为我的三个变量绘制直方图?
最佳答案
由于千位分隔符,数据将被读取为“非数字”。所以你需要转换它:
we <- gsub(",", "", we) # remove comma
we <- as.numeric(we) # turn into numbers
现在你可以做
hist(we)
和其他数字运算。
关于r - 无法绘制直方图, 'x' 必须是数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2349205/