替换由“。”编码的缺失值。在R数据帧中 [英] Replacing missing values coded by "." in an R dataframe

查看:185
本文介绍了替换由“。”编码的缺失值。在R数据帧中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,缺少由。编码的值,我想将值重新编码为NA:



df< - data.frame(h= c(1,1,。))



我尝试以下内容:



df $ h [df $ h ==。]< - NA



但是, NA 显示为< NA> ,我无法执行命令像意味着(df $ h,rm.na = TRUE)



有谁知道问题是什么?当我将数字重新编码为NA时,没有问题



谢谢!

解决方案

使用 is.na 函数。不需要转换为因素,虽然您具有字符值的事实确实造成您想要的数字的强制。

  > df<  -  data.frame(h= c(1,1,。))
> is.na(df)< - df ==。
> df
h
1 1
2 1
3< NA>

我不知道为什么@TylerRinker删除了他对使用na.strings的回应,因为我认为这是正确的答案。



评论:一年以后,我意识到a)OP误解了当因素导致失踪的值被显示时,字符向量,以及b)主要问题不是重新编码为R缺失值的错误,OP代码已经正确地正确完成,而是@joran标识的拼写错误。


I have a dataframe with missing values coded by ".", and I want to recode the values as NA:

df <- data.frame("h"=c(1,1,"."))

I try the following:

df$h[df$h == "."] <- NA

But the NA appears as a <NA>, and I can't execute commands like mean(df$h,rm.na=TRUE)

Does anyone know what the problem is? When I recode numbers as NA there's no problem

Thanks!

解决方案

Use the is.na function. No need to convert to factor, although the fact that you had character values did cause coercion of what you wanted to be numeric.

> df <- data.frame("h"=c(1,1,"."))
> is.na(df) <- df=="."
> df
     h
1    1
2    1
3 <NA>

I'm not sure why @TylerRinker deleted his response regarding using 'na.strings', since I thought it to be the correct answer.

Comment: Looking at this a year later I realized that a) the OP misunderstood how missing values were displayed when they are in factors or character vectors, and b) that the main problem was not an error in recoding to an R-missing-value, which the OP's code already correctly had done correctly, but was rather the misspelling error that @joran identified.

这篇关于替换由“。”编码的缺失值。在R数据帧中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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