简体   繁体   English

在R中的数据帧中将特定值转换为NA

[英]Converting particular values to NA in a dataframe in R

I have the following data frame: 我有以下数据框:

> head(newdat2)
   i42 i35 i32 i31 i30 i29 i28 i27 i26
1    5   5   5   5   5   5   5   5   5   
5    3   3   2   2   4   4   4   3   2      
6    5   5   5   2   5   5   5   5   5      
7    5   5   5   5   5   5   5   5   5      
8    4   5   4   3   5   4   4   3   4      
11   3   2   2   6   2   4   2   2   2   

I would like to convert any values that are not 1,2,3,4, or 5 to NAs. 我想将任何不是1,2,3,4或5的值转换为NA。 How could I go about doing this? 我该怎么做呢? I have tried the following: 我尝试了以下方法:

newdat2[(newdat2!=1)|(newdat2!=2)|(newdat2!=3)|(newdat2!=4)|(newdat2!=5)]<-NA

But this just makes all the values NA I also also tried some combos of sapply and ifelse, but nothing is working. 但是,这只是使所有值均不适用,我也尝试了一些sapply和ifelse组合,但没有任何效果。 Any thoughts? 有什么想法吗? Thank you! 谢谢!

Here's a possible implementation of the is.na<- replacement function 这是is.na<-替换功能的可能实现

df[] <- lapply(df, function(x) `is.na<-`(x, !x %in% 1:5))
#    i42 i35 i32 i31 i30 i29 i28 i27 i26
# 1    5   5   5   5   5   5   5   5   5
# 5    3   3   2   2   4   4   4   3   2
# 6    5   5   5   2   5   5   5   5   5
# 7    5   5   5   5   5   5   5   5   5
# 8    4   5   4   3   5   4   4   3   4
# 11   3   2   2  NA   2   4   2   2   2

Your approach can be made to work, you just need to get the boolean logic right. 可以使您的方法起作用,您只需要正确设置布尔逻辑即可。 David Arenburg's answer offers a cleaner approach anyway, but you may find it useful to understand "what went wrong" with your attempt. 无论如何,David Arenburg的答案提供了一种更清洁的方法,但是您可能会发现理解尝试中的“出问题”很有用。

Note that (x!=1)|(x!=2) will evaluate to true for both 1 and 2 (as well as any other number): when x is 1 the x!=2 clause is true, and when x is 2 the x!=1 clause is true. 请注意, (x!=1)|(x!=2)对于1和2(以及任何其他数字)都将为true:当x为1时, x!=2子句为true,而x为2 x!=1子句为true。 To get all numbers that are neither 1 nor 2 you want to use the & connective: (x!=1)&(x!=2) . 要获取既不是1也不是2的所有数字,则要使用&连词: (x!=1)&(x!=2) This is an application of one of two extremely useful principles known as DeMorgan's laws , which state that, for any boolean expressions P and Q , !(P or Q) == (!P) and (!Q) , and !(P and Q) == (!P) or (!Q) . 这是被称为DeMorgan定律的两个极其有用的原理之一的应用,该定律指出,对于任何布尔表达式PQ!(P or Q) == (!P) and (!Q)以及!(P and Q) == (!P) or (!Q)

It is not too hard to show that this extends to arbitrarily many expressions, so in your case the correct code would be 很难看出这可以扩展到任意多个表达式,因此在您的情况下,正确的代码是

newdat2[(newdat2!=1)&(newdat2!=2)&(newdat2!=3)&(newdat2!=4)&(newdat2!=5)]<-NA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM