替换由“。”编码的缺失值。在R数据帧中 [英] Replacing missing values coded by "." in an R dataframe
问题描述
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屋!