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)

Arguments

value

value to set unit for.

id

id of the desired unit for the column

conversionTable

formatted as StoxUnits

assertNew

if True an error is raised if the unit is already set for 'value'

Value

converted value with the attribute 'Unit' set / altered.

Details

The argument 'conversionTable' defines valid units and their symbols. This defaults to StoxUnits.

Providing id as NA removes unit from value

Examples

 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"