R: Convert numeric types from one unit to another

ud.convert {udunits2} | R Documentation

## Convert numeric types from one unit to another

### Description

This function takes the numeric argument `x`, quantified in units `u1` and converts it to be of units `u2`.

### Usage

    ud.convert(x, u1, u2)

### Arguments

`x` | Some argument which is convertible to a numeric type by `as.double`.
`u1` | A character string which is parseable into a udunits compatible unit.
`u2` | Another character string which is also parseable into a udunits compatible unit and for which there exists a defined transformation from the units represented by u1.

### Details

This function uses the udunits function `cv_convert_doubles` to convert the argument from one set of units to another.

### Value

Returns a numeric type having converted from one unit to another. The attributes of the original argument `x` (e.g. class, dimensions, etc.) are preserved and then re-applied to the return value of the transformation as such: `attributes(rv) <- attributes(x)` If either of unit `u1` or `u2` is unparseable, or there does not exist a conversion from one to the other the function raises an error.

### Author(s)

James Hiebert hiebert@uvic.ca

### References

Unidata's udunits reference: http://www.unidata.ucar.edu/software/udunits/ API guide for cv_convert_doubles: http://www.unidata.ucar.edu/software/udunits/udunits-2.1.24/udunits2lib.html#index-cv_005fconvert_005fdoubles-39

### See Also

`ud.are.convertible`

### Examples

    x <- seq(10)
    ud.convert(x, "miles", "km")                   # c(1.609344, 3.218688, 4.828032, ...)
    x <- c(-40, 0, 100)
    ud.convert(x, "celsius", "degree_fahrenheit")  # c(-40, 32, 212)
    err <- try(ud.convert(100,"miles", "grams"))   # Error
    err <- try(ud.convert(NA, "not", "parseable")) # Error

[Package udunits2 version 0.13 Index]