Reputation: 63
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?
Upvotes: 3
Views: 3396
Reputation: 915
If Thomas' solution doesn't work you can always use the countrycode
package to change your countrycodes to something that causes fewer 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.
Upvotes: 0
Reputation: 44525
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.
Upvotes: 6