Roland Fuß
2023-Jan-16 10:23 UTC
[Rd] Recycling in arithmetic operations involving zero-length vectors
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, -- Dr. Roland Fu? Th?nen-Institut f?r Agrarklimaschutz/ Th?nen Institute of Climate-Smart Agriculture Bundesallee 65 D-38116 Braunschweig, Germany Tel.: ++49 531 596 2627 Fax: ++49 531 596 2699 Email: roland.fuss at thuenen.de Arbeitsgruppe "Emissionsberichterstattung"/ Working group "Emission Inventories" Email: emissionsinventare at thuenen.de Das Johann Heinrich von Th?nen-Institut, Bundesforschungsinstitut f?r L?ndliche R?ume, Wald und Fischerei ? kurz: Th?nen-Institut ? besteht aus 15 Fachinstituten, die in den Bereichen ?konomie, ?kologie und Technologie forschen und die Politik beraten. The Johann Heinrich von Th?nen Institute, Federal Research Institute for Rural Areas, Forestry and Fisheries ? Th?nen Institute in brief ? consists of 15 specialized institutes that carry out research and provide policy advice in the fields of economy, ecology and technology.
Duncan Murdoch
2023-Jan-16 11:11 UTC
[Rd] Recycling in arithmetic operations involving zero-length vectors
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