Skip to content
Prev 19015 / 63421 Next

prod(numeric(0)) surprise

I'm a little confused. I understand that numeric(0) means an empty
numeric vector, not the number 0 expressed as numeric. As it is now,
prod(numeric(0)) generates something -- a vector of length 1
containing the number 1 -- from nothing. I would have expected

prod(numeric(0)) ==> numeric(0)

this is consistent with

numeric(0) ==> numeric(0)
numeric(0) * 1 ==> numeric(0)
cumprod(numeric(0)) ==> numeric(0)

and, because concatenation occus before function evaluation,

prod(c(numeric(0),1)) ==> prod( c(1) ) ==> 1

I would expect sum() to behave the same way, e.g., sum(numeric(0)) ==>
numeric(0). From below,
I would have expected numeric(0) as the result (numeric(0) is the
result from log(numeric(0)), etc).

Martin (Morgan)


Martin Maechler <maechler at stat.math.ethz.ch> writes: