Closed fabeit closed 1 year ago
See ?valid_udunits
. Also, see ?install_unit
for info about how to define new units. I believe @billdenney has experience on this.
Thanks for the quick reply. So I guess I will have to define a new unit, if I want to convert for example C/m2 to C/km2? Or is there a way to convert any non defined unit from m2 to km2?
I'm not sure what C is supposed to be here. In the SI, C is Coulomb, and it's supported by udunits2 (the underlying library), but I suppose that you don't want Coulombs here. So (1) you need to use another identifier, and (2) yes, you need to install your custom units to be able to make conversions.
Yes, I work with similar questions often in medical data standardization. It is a bit tricky to add new units when two things map identically.
This is similar to #334.
You have to work around a limitation on redefining the same unit multiple times. @Enchufa2, could we enable "symbol_aliases" to be added if setting something to "1 new unit" where it could go there? (I'm referring to the "symbol_aliases" column that shows with valid_udunits()
.)
library(units)
#> udunits database from C:/Users/wdenn/AppData/Local/R/win-library/4.2/units/share/udunits/udunits2.xml
# Must use a basic unit that converts without being 1 of the other (e.g. cannot
# directly use "mol_C" to "mol_CO2")
install_unit(symbol = "g_C")
# Must define all the units that have the same conversion at the same time
install_unit(symbol = c("mol_C", "mol_CO2"), def = "12.011 g_C")
u1 <- set_units(1, "mol_C/m^2")
u2 <- set_units(u1, "mol_CO2/km^2")
#> Error: In 'mol_CO2/km^2', 'mol_CO2' is not recognized by udunits.
#>
#> See a table of valid unit symbols and names with valid_udunits().
#> Custom user-defined units can be added with install_unit().
#>
#> See a table of valid unit prefixes with valid_udunits_prefixes().
#> Prefixes will automatically work with any user-defined unit.
Created on 2022-12-22 with reprex v2.0.2
Symbol aliases work. When you install a new unit, if you provide several symbols and/or several names, the first symbol and name becomes the primary, and the rest of them become aliases. But you need to respect udunits2 syntax. You cannot use a number at the end of a unit name, because mol_CO2
is recognised as mol_CO^2
, so the symbol you are trying to use there is mol_CO
.
Got it, thanks!
library(units)
#> udunits database from C:/Users/wdenn/AppData/Local/R/win-library/4.2/units/share/udunits/udunits2.xml
# Must use a basic unit that converts without being 1 of the other (e.g. cannot
# directly use "mol_C" to "mol_CO2")
install_unit(symbol = "g_C")
# Must define all the units that have the same conversion at the same time
install_unit(symbol = c("mol_C", "mol_COtwo"), def = "12.011 g_C")
u1 <- set_units(1, "mol_C/m^2")
u2 <- set_units(u1, "mol_COtwo/km^2")
Created on 2022-12-22 with reprex v2.0.2
Is there somewhere a document that provides the complete list? I was wondering for example if it's possible to convert C to CO2, or these kind of stochiometric equivalencies.
Thanks