Compare a value to null. Why is this true?

后端 未结 2 1208
野的像风
野的像风 2021-01-17 16:20

Why is

isTRUE(NULL != 2)
[1] FALSE

And how would I receive TRUE?

In my real case I have variables and I want to process something,

2条回答
  •  长情又很酷
    2021-01-17 16:43

    As @Roland pointed out, we can't perform any logical operations directly on NULL object. To compare them we might need to perform an additional check of is.null and then perform the logical comparison.

    We can use identical instead to compare values which handles integers as well as NULL.

    identical(4, 2) 
    #FALSE
    
    identical(NULL, 2) 
    #FALSE
    
    identical(2, 2) 
    #TRUE
    

提交回复
热议问题