r-quantities / units

Measurement units for R
https://r-quantities.github.io/units
175 stars 28 forks source link

Feature Request: Allow binary comparisons and arithmetic operators with NA that does not have units #308

Closed billdenney closed 1 year ago

billdenney commented 2 years ago

All binary comparisons and arithmetic operators (e.g. <, >, +, *, etc.) when performed with an NA value result in NA. It would be helpful if the Ops with units allowed NA values to have these comparisons by default.

units::set_units(1, "m") < NA
#> Error in Ops.units(units::set_units(1, "m"), NA): both operands of the expression should be "units" objects

Created on 2022-03-09 by the reprex package (v2.0.1)

edzer commented 2 years ago

We have

> units::set_units(1, "m") < units::set_units(NA, "m")
[1] NA

I don't see why NA should be treated differently from a regular numeric.

billdenney commented 2 years ago

My rationale for asking is that in some of my code in the PKNCA package, I use NA as an indicator of missingness. I'm working through the package to make it units-enabled while trying to keep the code as generalized as reasonable. For that, I'm trying to only use units-specific code where required.

What I'm doing right now is I'm adding more code to test for NA first and then do the comparison if the value is not NA. It would be simpler to be able to do the boolean operator comparison directly since it will always result in NA.

That said, I can add the separate NA test first throughout the code, if this is not a feature of interest.