替换 <NA>与 NA [英] replace <NA> with NA

查看:44
本文介绍了替换 <NA>与 NA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含条目的数据框;似乎这些值不被视为 NA,因为 is.na 返回 FALSE.我想将这些值转换为 NA 但找不到方法.

I have a data frame containing entries; It appears that these values are not treated as NA since is.na returns FALSE. I would like to convert these values to NA but could not find the way.

推荐答案

这可能成为问题的两个类是字符和因素.这应该循环遍历一个 dtaframe 并将NA"值转换为真正的 <NA>,但仅适用于这两个类:

The two classes where this is likely to be an issue are character and factor. This should loop over a dtaframe and convert the "NA" values into true <NA>'s but just for those two classes:

make.true.NA <- function(x) if(is.character(x)||is.factor(x)){
                                  is.na(x) <- x=="NA"; x} else {
                                  x}
df[] <- lapply(df, make.true.NA)

(在没有数据示例的情况下未经测试.)形式的使用:df_name[] 将尝试保留原始数据帧的结构,否则将失去其类属性.我看到 ujjwal 认为你对 NA 的拼写有侧翼<>"字符,所以你可以尝试更通用的功能:

(Untested in the absence of a data example.) The use of the form: df_name[] will attempt to retain the structure of the original dataframe which would otherwise lose its class attribute. I see that ujjwal thinks your spelling of NA has flanking "<>" characters so you might try this functions as more general:

make.true.NA <- function(x) if(is.character(x)||is.factor(x)){
                                  is.na(x) <- x %in% c("NA", "<NA>"); x} else {
                                  x}

这篇关于替换 &lt;NA&gt;与 NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆