R无法使用hist(),因为“内容不是数字"由于十进制负数? [英] R cannot use hist() because "content not numeric" due to negative decimal numbers?

查看:31
本文介绍了R无法使用hist(),因为“内容不是数字"由于十进制负数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R的新手,我正在尝试使用hist()绘制100,000个数字列表的直方图,像这样

I am new to R and I am trying to draw a histogram using hist() of a list of 100,000 numbers like this

-0.764
-0.662
-0.764
-0.019
0.464
0.668
0.464

但是我不能这样做,因为R抱怨内容不是数字.这是我尝试过的:

but I cannot do it because R complains that the content is not numeric. This is what I've tried:

  • 我使用 t<-read.table(file ="file.txt",sep ="\ n",dec =.",标头= TRUE)读取文件,数据加载并看起来很好(我得到了相同的值)

  • I read the file using t <- read.table(file= "file.txt", sep = "\n", dec = ".", header = TRUE), the data loads and looks well (I get the same values)

我尝试使用 as.numeric(c(t [,1])),sapply(t,as.numeric)将其设为数字​​,但是得到的数字完全不同,像

I tried to make it numeric using as.numeric(c(t[,1])), sapply(t, as.numeric), but I get completely different numbers, like

53 428 791 428 582 979 979 428 456 533 550

53 428 791 428 582 428 979 428 456 533 550

我认为小数点."可能是一个问题.或负号-"或两者兼而有之.有什么想法吗?

I think their might be a problem with the decimal point "." or the negative signs "-" or both. Any ideas?

非常感谢!

推荐答案

R似乎已将数据的第一列转换为一个因素.如果此列中的所有数据都在文件中为数字,则不会发生这种情况.因此,必须有一个不能识别为数字的元素.

R seems to have transformed the first column of your data as a factor. This should not happen if all your data in this column where numeric in your file. So there must be an element which is not recognized as a number.

您可以尝试在R中进行以下操作(有点脏),以找出问题所在.从以下因素开始:

You can try the following (which is a bit dirty) in R to try to identify where the problem is. Starting with the following factor :

R> v <- factor(c("0.51", "-0.12", "0.345", "0.45b", "-0.8"))

您可以确定哪个值导致问题:

You can identify which value causes problem with :

R> v[is.na(as.numeric(as.character(v)))]
[1] 0.45b

您可以使用以下命令在向量中找到此值的位置:

And you can find the position of this value in your vector with :

R> which(is.na(as.numeric(as.character(v))))
[1] 4

这篇关于R无法使用hist(),因为“内容不是数字"由于十进制负数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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