&#34; NA&#34;字符串转换为<na>

时间:2015-06-29 16:55:05

标签: r csv dataframe

我有一个csv文件,其中包含国家/地区名称及其iso代码。这是它的样子:

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

我使用以下代码在data.frame中阅读:

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

这是数据的样子:

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

即使我明确设置"NA",字符串<NA>也会转换为colClasses = c("character")

如何让"NA""NA"中显示为data.frame

1 个答案:

答案 0 :(得分:9)

尝试明确设置na.strings参数 - 默认为“NA”:

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