Learn R Programming

units (version 0.4-5)

install_conversion_function: 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 = \alpha x + \beta\), 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

Run this code
# NOT RUN {
apples <- 2 * make_unit("apple")
oranges <- 3 * make_unit("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

# }

Run the code above in your browser using DataLab