Set unit of a value in accordance with StoX convention (adds the attribute 'stoxUnit'). Converts unit if already set.
setUnit(value, id, conversionTable = RstoxData::StoxUnits, assertNew = FALSE)
value to set unit for.
id of the desired unit for the column
formatted as StoxUnits
if True an error is raised if the unit is already set for 'value'
converted value with the attribute 'Unit' set / altered.
The argument 'conversionTable' defines valid units and their symbols.
This defaults to StoxUnits
.
Providing id as NA removes unit from value
dt <- data.table::data.table(weight=c(1000,1200))
dt$weight <- setUnit(dt$weight, "mass-g")
print(dt$weight)
#> [1] 1000 1200
#> attr(,"stoxUnit")
#> [1] "mass-g"
dt$weight <- setUnit(dt$weight, "mass-kg")
print(dt$weight)
#> [1] 1.0 1.2
#> attr(,"stoxUnit")
#> [1] "mass-kg"