r-quantities / units

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

[feature request] Convenience function to use the units of one value as the units for another. #245

Closed vorpalvorpal closed 4 years ago

vorpalvorpal commented 4 years ago

Often there are situations where some metadata about some value is provided (eg. reporting limit, confidence interval) at a different time or in a different input table to the main table holding the value and the unit. I feel like in such situations, a convenience function that copies the units of one value and applies it to another value would be useful. Something along the lines of the following:

clone_units <- function(copyTo, copyFrom){
  if ("units" %in% class(copyTo)){
    copyTo <- drop_units(copyTo)
  }
  set_units(copyTo, deparse_unit(copyFrom), mode = "standard")
}

This would also allow for setting units as per point 4 of unit #400 (ie., km -> mile, without conversion). This is useful when the units aren't clear, and you need to do some data exploration to work out what the unit actually is. In such a case changing units without conversion is useful.

Enchufa2 commented 4 years ago

I'm not sure I follow. Doing this right now is a one-liner, but requires the user to be fully aware of what they're doing:

x <- set_units(drop_units(x), units(y), mode="standard")

or, equivalently for any x,

x <- set_units(as.numeric(x), units(y), mode="standard")

"Cloning" units goes against the package's rationale, I think.