[英]R: How to recode values of a variable to NA for cases where another variable has a value of NA
I'm cleaning a dataset and I need to recode a variable, DebtRatio, to have the value NA when MonthlyIncome has a value of NA. 我正在清理数据集,并且当MonthlyIncome的值为NA时,需要重新编码变量DebtRatio的值为NA。 By default this is not true and thus the values which have NA are giving crazy values for DebtRatio (naturally).
默认情况下,这是不正确的,因此具有NA的值自然会给DebtRatio疯狂的值。 However these cases have a variety of other interesting information so I dont wish to remove them completely.
但是,这些案例还有许多其他有趣的信息,因此我不希望完全删除它们。 Simply recode the values of these cases to NA.
只需将这些案例的值重新编码为NA。
However when I run my script, it doesnt seem to behave the way I want. 但是,当我运行脚本时,它似乎并没有达到我想要的方式。 I dont receive an error in the terminal but the values dont change at all.
我没有在终端中收到错误,但是值根本没有改变。
credit_train_18$DebtRatio[credit_train_18$MonthlyIncome == NA] <- NA
This works when recoding my categorical variables so I'm not sure why it fails in this particular case. 重新编码我的分类变量时,此方法有效,因此我不确定在这种情况下为什么它会失败。
I would really appreciate a solution to this problem 我真的很感激这个问题的解决方案
You should use is.na() rather than checking for equality. 您应该使用is.na()而不是检查是否相等。
credit_train_18$DebtRatio[is.na(credit_train_18$MonthlyIncome)] <- NA
Your approach is problematic because NA == NA returns NA rather than TRUE. 您的方法有问题,因为NA == NA返回NA而不是TRUE。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.