r-quantities / units

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

Add documentation about how to define derived units #240

Closed vorpalvorpal closed 3 years ago

vorpalvorpal commented 4 years ago

I wonder if it might be useful to have a function allowing the setting of derived units. Currently install_symbolic_unit() allows for new fundamental units and install_conversion_constant() allows for units on non-SI scales, but as far as I can see (and apologies if I am missing something), there is no way to create derived units.

My particular use case is Equivalence (Eq) which is a unit of moles valence (most often used as mEq/L). It would be nice if I could set a new fundamental unit called "valence" and then define "Eq" as "molesvalence" so that I could then do something like set "Ca2 <- set_units(2, valence)" and then determine the moles of calcium in a x mEq/L solution or in reverse, derive the mEq/L of a solution if I added y moles of calcium.

Thanks for this wonderful package!

Enchufa2 commented 4 years ago

Did you try? :)

install_symbolic_unit("valence")
install_conversion_constant("Eq", "mol*valence", 1)

x <- set_units(2, mol)
y <- set_units(3, valence)
set_units(x*y, Eq)
#> 6 [Eq]

It is true that we should improve the documentation to explicitly say that this is possible.

vorpalvorpal commented 4 years ago

Wow! That's amazing. I thought it must be possible and I was just missing something. Thank you!

Enchufa2 commented 3 years ago

New install_unit in the upcoming release.