R: Install a function for conversion between user-defined units.

install_conversion_function {units} | R Documentation

## Install a function for conversion between user-defined units.

### Description

Tells the `units` package how to convert one-way from one unit to another.

### Usage

    install_conversion_function(from, to, f)

### Arguments

`from` | String for the symbol of the unit being converted from.
`to` | String for the symbol of the unit being converted to.
`f` | A function responsible for conversion.

### Details

This is the most general way of specifying conversion between user-defined units. The function installes a one-way conversion from one unit to another through a general function, `f`, that must take one numeric argument and return one numeric argument. When the `units` package tries to convert between units, it will look up `from` and `to` to see if it can find a conversion function. If it can, it will call `f` and consider the value converted from unit `from` to unit `to`.

It is the user's responsibility to install a conversion from `to` back to `from` as well. One-way conversion does not work well with the `units` package, since conversion is done in several places for unit expression simplification and if a unit can only be converted in one direction, this simplification will not work correctly.

For conversion that can be done as a linear function, y = α x + β, you should instead use the `install_conversion_constant` function. This function will automatically install conversion functions for both directions of unit conversion.

### See Also

`install_conversion_constant`

### Examples

    install_symbolic_unit("apple")
    install_symbolic_unit("orange")
    apples <- 2 * as_units("apple")
    oranges <- 3 * as_units("orange")

    # one orange is worth two apples
    install_conversion_function("orange", "apple", function(x) 2 * x)
    install_conversion_function("apple", "orange", function(x) x / 2)
    apples + oranges
    oranges + apples

[Package units version 0.5-1 Index]