强制引进的不适用 [英] NA introduced by coercion
问题描述
我有一个记事本txt文件 inflation.txt
,看起来像这样:
I have a file a notepad txt file inflation.txt
that looks something like this:
1950-1 0.0084490544865279
1950-2 −0.0050487986543660
1950-3 0.0038461526886055
1950-4 0.0214293914558992
1951-1 0.0232839389540449
1951-2 0.0299121323429455
1951-3 0.0379293285389640
1951-4 0.0212773984472849
在先前的 stackoverflow帖子中,我学习了如何将此文件导入R:
From a previous stackoverflow post, I learned how to import this file into R:
data <- read.table("inflation.txt", sep = "" , header = F ,
na.strings ="", stringsAsFactors= F, encoding = "UTF-8")
但是,此代码以字符
的形式读取文件.当我尝试将此文件转换为数字格式时,所有负值都将替换为NA:
However, this code reads the file as a character
. When I try to convert this file to numeric format, all negative values are replaced with NA:
b=as.numeric(data$V2)
Warning message:
In base::as.numeric(x) : NAs introduced by coercion
> head(b)
[1] 0.008449054 NA 0.003846153 0.021429391 0.023283939 0.029912132
有人可以告诉我我做错了什么吗?可以将 inflation.txt
文件另存为 data.frame
吗?
Can someone please show me what I am doing wrong? Is it possible to save the inflation.txt
file as a data.frame
?
推荐答案
问题是您数据中的-"
不是减号(是破折号),因此数据将被读取为字符.
The issue is that "−"
that you have in your data is not minus sign (it is a dash), hence the data is being read as character.
您有两个选择.
- 在任何文本编辑器中打开文件,找到所有
-"
并用负号替换,然后直接使用read.table
. li>
- Open the file in any text editor and find and replace all the
"−"
with negative sign and then usingread.table
would work directly.
data <- read.table("inflation.txt")
- 如果您无法更改原始文件中的数据,则在将数据读入R后将其替换为
sub
.
data$V2 <- as.numeric(sub('−', '-', data$V2, fixed = TRUE))
这篇关于强制引进的不适用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!