Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"NA" string converted into <NA>

Tags:

dataframe

r

csv

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?

like image 262
Armin Avatar asked Jan 08 '23 01:01

Armin


1 Answers

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="" ) 
like image 77
Mike Wise Avatar answered Jan 15 '23 21:01

Mike Wise