units::set_units() 无法识别变量中的单位

units::set_units() won't recognise units from an variable

如何从变量中将单位读取到 units 包中?我收到错误 ...not recognised by udunits。下面是一个 MWE:

library(units)
measurements <- runif(10)
unitsofmeasurement <- "mm"
set_units(measurements, unitsofmeasurement)

returns错误

Error: In ‘unitsofmeasurement’, ‘unitsofmeasurement’ is not recognized by udunits.

但以下有效:

set_units(measurements, "mm")

as_units 的帮助页面上有一些关于此的信息。您可以通过将模式设置为 "standard".

来获得您想要的行为
units_options(set_units_mode = "standard")
set_units(measurements, unitsofmeasurement)

Units: mm
 [1] 0.33276818 0.16444913 0.07442400 0.15536703 0.13068749 0.08494946 0.86531095
 [8] 0.40301657 0.78669608 0.14237601