units {units}R Documentation

Set measurement units on a numeric vector

Description

Set measurement units on a numeric vector

Convert units

retrieve measurement units from units object

Usage

## S3 replacement method for class 'numeric'
units(x) <- value

## S3 replacement method for class 'units'
units(x) <- value

set_units(x, value, ...)

## S3 method for class 'units'
set_units(x, value, ...)

## S3 method for class 'numeric'
set_units(x, value = unitless, ...)

## S3 method for class 'units'
units(x)

Arguments

x

numeric vector, or object of class units

value

object of class units or symbolic_units, or in the case of set_units expression with symbols that can be resolved in ud_units (see examples).

...

ignored

Details

set_units is a pipe-friendly version of units<- that evaluates value first in the environment of ud_units.

Value

object of class units

the units method retrieves the units attribute, which is of class symbolic_units

Examples

x = 1:3
class(x)
units(x) <- with(ud_units, m/s) # valid
class(x)
y = 2:5
a <- with(ud_units, 1:3 * m/s)
units(a) <- with(ud_units, km/h)
a
# note that these units have NOT been defined or declared before:
set_units(1:5, N/m^2)
set_units(1:5, unitless) # unit "1", unitless
if (require(magrittr)) {
 1:5 %>% set_units(N/m^2)
 1:10 %>% set_units(m) %>% set_units(km)
}

[Package units version 0.4-5 Index]