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

1

6

How can I read units into the units package from a variable? I get the error ...not recognised by udunits. An MWE below:

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

returns the error

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

but the following works:

set_units(measurements, "mm")
Dippy answered 23/6, 2018 at 10:39 Comment(0)
P
7

There is some information about this on the help page for as_units. You can get the behavior you want by setting the mode to "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
Papen answered 23/6, 2018 at 11:0 Comment(1)
I missed this the first time I read the help page. Fixed by adding mode = "standard" to the set_units() function.Dippy

© 2022 - 2024 — McMap. All rights reserved.