[Rd] '==' operator: inconsistency in data.frame(...) == NULL

Hilmar Berger berger @end|ng |rom mp||b-ber||n@mpg@de
Wed Sep 4 15:25:46 CEST 2019

Dear all,

I just stumbled upon some behavior of the == operator which is at least 
somewhat inconsistent.

R version 3.6.1 (2019-07-05) -- "Action of the Toes"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

 > list(a=1:3, b=LETTERS[1:3]) == NULL
 > matrix(1:6, 2,3) == NULL
 > data.frame(a=1:3, b=LETTERS[1:3]) == NULL # same for == logical(0)
Error in matrix(if (is.null(value)) logical() else value, nrow = nr, 
dimnames = list(rn,  :
   length of 'dimnames' [2] not equal to array extent

 > data.frame(NULL) == 1
<0 x 0 matrix>
 > data.frame(NULL) == NULL
<0 x 0 matrix>
 > data.frame(NULL) == logical(0)
<0 x 0 matrix>

I wonder if data.frame(<some non-empty data>) == NULL should also return 
a value instead of an error. R help reads:

"At least one of |x| and |y| must be an atomic vector, but if the other 
is a list *R* attempts to coerce it to the type of the atomic vector: 
this will succeed if the list is made up of elements of length one that 
can be coerced to the correct type.

If the two arguments are atomic vectors of different types, one is 
coerced to the type of the other, the (decreasing) order of precedence 
being character, complex, numeric, integer, logical and raw."

It is not clear from the help what to expect for NULL or empty atomic 
vectors. It is also strange that for list() there is no error but for 
data.frame() with the same data an error is thrown. I can see that there 
might be reasons to return logical(0) instead of FALSE, but I do not 
fully understand why there should be differences between e.g. matrix() 
and data.frame().

Also, It is at least somewhat strange that data.frame(NULL) == NULL and 
similar expressions return an empty matrix, while comparing a normal 
filled matrix to NULL returns logical(0).

Even if this behavior is expected, the error message shown by 
data.frame(...) == NULL is not very informative.

Thanks and best regards,


	[[alternative HTML version deleted]]

More information about the R-devel mailing list