r-quantities / units

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

unit conversion with rbind (on data.frames) #232

Closed dleutnant closed 4 years ago

dleutnant commented 4 years ago

Hi, I just came across the following issue, in which the automatic unit conversion does not apply. The reprex aims to rbind two data.frames which share the same column 'dt' of class 'units'. The unit however differs and the first one is used during rbind. Most probably this is rather a "rbind"-related issue but I thought it is worth sharing it here...

library(units)
#> udunits system database from /usr/local/share/udunits

# dummy data
h <- data.frame(dt = set_units(24, "h"))
min <- data.frame(dt = set_units(1440, "min"))

# binding both data.frames works, but silently gives wrong unit conversion results
# issue: first unit dominates the other 

# ... here: hour
rbind(h, min)
#>         dt
#> 1   24 [h]
#> 2 1440 [h]
# ... here: mins
rbind(min, h)
#>           dt
#> 1 1440 [min]
#> 2   24 [min]

Created on 2020-03-24 by the reprex package (v0.3.0)

Enchufa2 commented 4 years ago

The issue we have here is that

x <- set_units(24, h)
x[2] <- set_units(1440, min)
x
#> Units: [h]
#> [1] 24 1440

And, more generally,

x[2] <- set_units(3, m)
x
#> Units: [h]
#> [1] 24 3