[R] prod(NaN, NA) vs. prod(NA, NaN)

Rolf Turner r@turner @end|ng |rom @uck|@nd@@c@nz
Tue Jul 3 23:09:35 CEST 2018

On 04/07/18 00:24, Martin Møller Skarbiniks Pedersen wrote:

> Hi,
>    I am currently using R v3.4.4 and I just discovered this:
>> prod(NA, NaN) ; prod(NaN, NA)
> [1] NA
> [1] NaN
> ?prod says:
>      If ‘na.rm’ is ‘FALSE’ an ‘NA’ value in any of the arguments will
>       cause a value of ‘NA’ to be returned, otherwise ‘NA’ values are
>       ignored.
> So according to the manual-page for prod() NA should be returned in both
> cases?
> However for sum() is opposite is true:
>> sum(NA, NaN) ; sum(NaN, NA)
> [1] NA
> [1] NA
> ?sum says:
>      If ‘na.rm’ is ‘FALSE’ an ‘NA’ or ‘NaN’ value in any of the
>       arguments will cause a value of ‘NA’ or ‘NaN’ to be returned,
>       otherwise ‘NA’ and ‘NaN’ values are ignored.
> Maybe the manual for prod() should say the same as sum() that
> both NA and NaN can be returned?


 > sum(NA,NaN)
[1] NA
 > sum(NaN,NA)
[1] NA

so sum gives NA "both ways around".  Perhaps a slight inconsistency 
here?  I doubt that it's worth losing any sleep over, however.

Interestingly (???):

 > NaN*NA
[1] NaN
 > NA*NaN
[1] NA
 > NaN+NA
[1] NaN
 > NA+NaN
[1] NA

So we have an instance of non-commutative arithmetic operations.  And 
sum() is a wee bit inconsistent with "+".

Again I doubt that the implications are all that serious.


Rolf Turner

Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

More information about the R-help mailing list