r-quantities / units

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

Coversion of area times length to volume #296

Closed vorpalvorpal closed 2 years ago

vorpalvorpal commented 2 years ago

To calculate the number of L of rain falling on an area we calculate area*depth. This doesn't appear to work with units because m2 != m^2. Is there a correct way of doing this with out dropping units and then reapplying them?

Example:

area <- set_units(10, ha)
rain <- set_units(1000, mm)
(area*rain)
# 10000 [ha*mm]
area*rain %>% set_units(L)
# Error: cannot convert mm into L
edzer commented 2 years ago

You missed the operator precedance of * and %>%, try

(area*rain) %>% set_units(l)
vorpalvorpal commented 2 years ago

Sorry I don't understand. Why do I want to convert it to unitless (1) before converting to volume? Also, it doesn't work.

(area*rain) %>% set_units(1)
# Error: cannot convert ha*mm into 1
set_units(area*rain, 1)
# Error: cannot convert m2*mm into 1
edzer commented 2 years ago

I used l for liter, not 1.

Enchufa2 commented 2 years ago

@vorpalvorpal In other words, without parentheses, you think you are doing this

(area * rain) |> set_units("liter")
#> 1e+08 [L]

but instead you are doing this

area * (rain |> set_units("liter"))
#> Error: cannot convert mm into L

See https://stat.ethz.ch/R-manual/R-devel/library/base/html/Syntax.html