Armin
Armin

Reputation: 317

"NA" string converted into <NA>

I have a csv file with country names and their iso codes. Here's what it looks like:

"Name","Code"
"Afghanistan","AF"
"Albania","AL"
"Algeria","DZ"
"Namibia","NA"

I read it in a data.frame using the following code:

cc = read.csv("countries.csv", header=TRUE, stringsAsFactors=FALSE, 
                                           colClasses = c("character")) 

Here's what the data looks like:

Name         Code
Afghanistan  AF
Albania      AL
Algeria      DZ
Namibia      <NA>

The string "NA" is getting converted to <NA> even though I explicitly set colClasses = c("character").

How do I get "NA" to show as "NA" in the data.frame?

Upvotes: 5

Views: 764

Answers (1)

Mike Wise
Mike Wise

Reputation: 22817

Try setting the na.strings parameter explicitly - the default is"NA":

    cc = read.csv("countries.csv", header=TRUE, stringsAsFactors=FALSE,
                              colClasses = c("character"),  na.strings="" ) 

Upvotes: 10

Related Questions