R - 'NA' text treated as N/A

*爱你&永不变心* 提交于 2019-12-07 20:44:58

问题


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))

I initially thought it might be a factor issue, so I made sure the iso code column is character. But this didn't help.

How can this be solved?


回答1:


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"

You could confirm this by giving us a dput() of (part of) your data.

When you read in your data, try changing na.strings = "NA" (e.g., in read.csv) to something else and see if it works.

For example, with 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

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.




回答2:


If Thomas' solution doesn't work you can always use the countrycode package to change your countrycodes to something that causes less problems. In your case from ISO2-character to ISO3-character for instance.

country.info$iso.code<-countrycode(country.info$iso.code,"iso2c","iso3c",
                                     warn=TRUE)

(If iso2c causes problems use country.names, hoping the Republic of Congo and the Democratic Republic of Congo don't mess things up.)



来源:https://stackoverflow.com/questions/17990233/r-na-text-treated-as-n-a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!