Unlike measurements::conv_unit() this function doesn't fail if it can't convert a unit -- instead it outputs NA.

convert_units(x, from, to, quietly = FALSE)

Arguments

x

a numeric vector giving the measurement value in its original units.

from

the unit in which the measurement was made.

to

the unit to which the measurement is to be converted.

quietly

TRUE suppresses warning.

Value

As in measurements::conv_unit() if possible, or NA.

See also

Other helpers: valid_units

Examples

convert_units(c(1, NA), from = "cm", to = "m")
#> [1] 0.01 NA
# Same measurements::conv_unit(c(1, NA), from = "cm", to = "m")
#> [1] 0.01 NA
# Passes convert_units(c(1, 10), from = "cm", to = c("m", "bad"))
#> Warning: Can't convert all units (inserting 1 missing values): #> the 'to' argument is not an acceptable unit.
#> [1] 0.01 NA
convert_units(c(1, 10), from = "cm", to = c("m", "bad"), quietly = TRUE)
#> [1] 0.01 NA
# Errs try(measurements::conv_unit(c(1, 10), from = "cm", to = c("m", "bad")))
#> Warning: longer object length is not a multiple of shorter object length
#> Warning: longer object length is not a multiple of shorter object length
#> Warning: longer object length is not a multiple of shorter object length
#> Warning: longer object length is not a multiple of shorter object length
#> Warning: the condition has length > 1 and only the first element will be used
#> Warning: the condition has length > 1 and only the first element will be used
#> Warning: longer object length is not a multiple of shorter object length
#> Warning: longer object length is not a multiple of shorter object length
#> [1] 0.01 0.10