-6ren"> -我有一个 csv包含国家名称及其 ISO 代码的文件。这是它的样子: "Name","Code" "Afghanistan","AF" "Albania","AL" "Algeria","DZ" "N-6ren">
gpt4 book ai didi

r - "NA"字符串转换为

转载 作者:行者123 更新时间:2023-12-04 22:59:13 27 4
gpt4 key购买 nike

我有一个 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 ?

最佳答案

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

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

关于r - "NA"字符串转换为 <NA>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31121320/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com