Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do NA==NULL , NULL==NULL print logical(0)?

Tags:

null

r

na

Why does NA==NULL result in logical (0) instead of FALSE?

And why does NULL==NULL result in logical(0) instead of TRUE?

like image 799
Tasneema Avatar asked Oct 17 '25 02:10

Tasneema


1 Answers

NULL is a "zero-length" object, so any elementwise comparison or operation with NULL will have length zero: logical(0) represents a logical vector of length zero. You might find identical() useful: identical(NULL,NULL) is TRUE, identical(NULL,NA) is FALSE. Also see ?is.null, ?is.na for testing for the special values of NA and NULL.

See also: Compare a value to null. Why is this true?

@Dason points out that == does elementwise comparison; when you do elementwise operations on vectors of two different lengths, R typically "recycles" the shorter vector to be equal in length to the longer one (with a warning if the lengths are not evenly divisible), but the R language definition says

As from R 1.4.0, any arithmetic operation involving a zero-length vector has a zero-length result.

like image 176
Ben Bolker Avatar answered Oct 18 '25 17:10

Ben Bolker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!