R-'NA'文本被视为N/A [英] R - 'NA' text treated as N/A
问题描述
我在R中有一个数据框,其中包含国家/地区代码.纳米比亚的iso代码恰好是"NA". R将此文本"NA"视为N/A.
I have a data frame in R including country iso codes. The iso code for Namibia happens to be 'NA'. R treats this text 'NA' as N/A.
例如,下面的代码为我提供了纳米比亚这一行.
For example the code below gives me the row with Namibia.
test <- subset(country.info,is.na(country.info$iso.code))
我最初认为这可能是一个因素问题,所以我确保iso代码列是字符.但这没有帮助.
I initially thought it might be a factor issue, so I made sure the iso code column is character. But this didn't help.
如何解决?
推荐答案
这可能与您读取数据的方式有关.仅仅因为它的字符并不意味着您的"NA"
不是NA
,例如:
This probably relates to how you read in the data. Just because it's character doesn't mean your "NA"
isn't an NA
, e.g.:
z <- c("NA",NA,"US")
class(z)
#[1] "character"
您可以通过向我们提供(部分)数据的dput()
来确认这一点.
You could confirm this by giving us a dput()
of (part of) your data.
当您读入数据时,请尝试将na.strings = "NA"
(例如,在read.csv
中)更改为其他内容,以查看其是否有效.
When you read in your data, try changing na.strings = "NA"
(e.g., in read.csv
) to something else and see if it works.
例如,使用na.strings = ""
:
read.table(text="code country
NA Namibia
GR Germany
FR France", stringsAsFactors=FALSE, header=TRUE, na.strings="")
# code country
# 1 NA Namibia
# 2 GR Germany
# 3 FR France
请确保检查使用""
不会导致更改任何其他内容.否则,您可以使用在文件中绝对不会出现的字符串,例如"z_z_z"或类似的名称.您可以将text=..
替换为文件名.
Make sure to check that the use of ""
doesn't result in changing anything else. Else, you can use a string that will definitely not occur in your file like "z_z_z" or something like that.. You can replace the text=..
with your file name.
这篇关于R-'NA'文本被视为N/A的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!