[Rd] Recycling in arithmetic operations involving zero-length vectors
Duncan Murdoch
murdoch@dunc@n @end|ng |rom gm@||@com
Mon Jan 16 12:11:10 CET 2023
On 16/01/2023 5:23 a.m., Roland Fuß wrote:
> Dear R-core,
>
> The language definition is very clear:
>
> "As from R 1.4.0, any arithmetic operation involving a zero-length
> vector has a zero-length result."
>
> Thus, `1 + numeric()` returns `numeric(0)`. However, I don't find this
> very intuitive because usually the shorter vector is recycled to the
> length of the longer vector. Would it be possible to throw at least a
> warning for such cases? I don't expect them to be intended by most users.
>
> Best regards,
The previous paragraph says "If the length of the longer vector is not a
multiple of the shorter one, a warning is given." Since 1 is not a
multiple of 0, that implies a warning should be given here.
However, R 1.4.0 was released more than 20 years ago, so I would guess
there are lots of packages intentionally using this. For example, it's
a way to propagate bad inputs through a long calculation that allows a
single test at the end.
And even unintentional uses are unlikely to lead to problematic results:
numeric(0) is usually a pretty clear signal that something is wrong.
So I'd suggest a documentation change: "As from R 1.4.0, any arithmetic
operation involving a zero-length vector has a zero-length result
*without a warning*."
Duncan Murdoch
More information about the R-devel
mailing list