r-quantities / units

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

bug using deparse on units with denominator power greater than 1 #376

Closed jeromepcollet closed 1 month ago

jeromepcollet commented 1 month ago

I have some variables, and I want to set their unities to other unitless variables. Among them, some are volumic variables, and the process fails, because the denominator is deparsed as "m3-1"

examples

works

library(units)
lineic_density <- set_units(1, kg/m)
lineic_density
lineic_density_unity <- deparse_unit(lineic_density)
lineic_density_unity
other_lineic_density <- set_units(1, density_unity)

fails

library(units)
density <- set_units(1, kg/m3)
density
density_unity <- deparse_unit(density)
density_unity
other_density <- set_units(1, density_unity)
Enchufa2 commented 1 month ago

Are you having trouble with the issue buttons? :)

Not sure what's your point here, because both examples fail. Do not use deparse_unit for this. This works:

x <- set_units(1, kg/m)
y <- set_units(1, units(x), mode="standard")

x <- set_units(1, kg/m3)
y <- set_units(1, units(x), mode="standard")
jeromepcollet commented 1 month ago

Sorry for my misuse of buttons. My constraint is that I have to store units in a text file before resetting them to other variables. And I forgot the use of "standard". Now I think following examples are OK : first works, second fails ############ library(units)

works

lineic_density <- set_units(1, kg/m) lineic_density lineic_density_unity <- deparse_unit(lineic_density) lineic_density_unity other_lineic_density <- set_units(2, lineic_density_unity, mode = "standard") other_lineic_density

fails

density <- set_units(1, kg/m3) density density_unity <- deparse_unit(density) density_unity other_density <- set_units(2, density_unity, mode = "standard") other_density

Enchufa2 commented 1 month ago

There's certainly an open bug in our parser when there's a m3 instead of m^3, but that's #221. About the main point of your use case, see my comment above and use units() instead of deparse_unit() for this.

Enchufa2 commented 1 month ago

Ah, sorry, I didn't catch this:

My constraint is that I have to store units in a text file before resetting them to other variables.

In such a case, specify your units as km/m^3 or "km m-3" to circumvent the issue with #221.

jeromepcollet commented 1 month ago

OK, I do not see exactly how to use https://github.com/r-quantities/units/issues/221, I will look further. I make my need more precise : I wanted to get the units automatically from a previous data-frame, to set these units on a new data.frame. Thank you for your help, I see my need is very specific.

Enchufa2 commented 1 month ago

OK, I do not see exactly how to use #221, I will look further.

No, you don't need to "use" #221. I mean that our parser has a problem parsing m3 vs m^3, and this is reported in #221, so no need to keep this issue open too.

I make my need more precise : I wanted to get the units automatically from a previous data-frame, to set these units on a new data.frame. Thank you for your help, I see my need is very specific.

If you use the ^ character to specify powers, i.e. km/m^3 instead of km/m3, your workflow above should work fine.