| Title: | Measurement Units for R Vectors |
|---|---|
| Description: | Support for measurement units in R vectors, matrices and arrays: automatic propagation, conversion, derivation and simplification of units; raising errors in case of unit incompatibility. Compatible with the POSIXct, Date and difftime classes. Uses the UNIDATA udunits library and unit database for unit compatibility checking and conversion. Documentation about 'units' is provided in the paper by Pebesma, Mailund & Hiebert (2016, <doi:10.32614/RJ-2016-061>), included in this package as a vignette; see 'citation("units")' for details. |
| Authors: | Edzer Pebesma [aut, cre] (ORCID: <https://orcid.org/0000-0001-8049-7069>), Thomas Mailund [aut], Tomasz Kalinowski [aut], James Hiebert [ctb], Iñaki Ucar [aut] (ORCID: <https://orcid.org/0000-0001-6403-5550>), Thomas Lin Pedersen [ctb] |
| Maintainer: | Edzer Pebesma <[email protected]> |
| License: | GPL-2 |
| Version: | 1.0-1.3 |
| Built: | 2026-05-11 10:42:40 UTC |
| Source: | https://github.com/r-quantities/units |
convert units object into difftime object
as_difftime(x)as_difftime(x)
x |
object of class |
t1 = Sys.time() t2 = t1 + 3600 d = t2 - t1 du <- as_units(d) dt = as_difftime(du) class(dt) dtt1 = Sys.time() t2 = t1 + 3600 d = t2 - t1 du <- as_units(d) dt = as_difftime(du) class(dt) dt
units objectsBoxplot for units objects
## S3 method for class 'units' boxplot(x, ..., horizontal = FALSE)## S3 method for class 'units' boxplot(x, ..., horizontal = FALSE)
x |
object of class |
... |
parameters passed on to |
horizontal |
logical; whether boxplots should be horizontal;
default |
units_options(parse = FALSE) # otherwise we break on the funny symbol! u = set_units(rnorm(100), degree_C) boxplot(u)units_options(parse = FALSE) # otherwise we break on the funny symbol! u = set_units(rnorm(100), degree_C) boxplot(u)
S3 methods for units objects (see cbind).
## S3 method for class 'units' cbind(..., deparse.level = 1) ## S3 method for class 'units' rbind(..., deparse.level = 1)## S3 method for class 'units' cbind(..., deparse.level = 1) ## S3 method for class 'units' rbind(..., deparse.level = 1)
... |
(generalized) vectors or matrices. These can be given as named
arguments. Other R objects may be coerced as appropriate, or S4
methods may be used: see sections ‘Details’ and
‘Value’. (For the |
deparse.level |
integer controlling the construction of labels in
the case of non-matrix-like arguments (for the default method): |
x <- set_units(1, m/s) y <- set_units(1:3, m/s) z <- set_units(8:10, m/s) (m <- cbind(x, y)) # the '1' (= shorter vector) is recycled (m <- cbind(m, z)[, c(1, 3, 2)]) # insert a column (m <- rbind(m, z)) # insert a rowx <- set_units(1, m/s) y <- set_units(1:3, m/s) z <- set_units(8:10, m/s) (m <- cbind(x, y)) # the '1' (= shorter vector) is recycled (m <- cbind(m, z)[, c(1, 3, 2)]) # insert a column (m <- rbind(m, z)) # insert a row
Convert the units of a units object to their base units, as defined by
the udunits database (SI units).
convert_to_base(x, simplify = TRUE, keep_fraction = TRUE)convert_to_base(x, simplify = TRUE, keep_fraction = TRUE)
x |
object of class |
simplify |
logical; if |
keep_fraction |
logical; if |
An object of class units with units converted to base units.
x <- set_units(32, mJ/g) convert_to_base(x) convert_to_base(x, keep_fraction=FALSE) convert_to_base(x, simplify=FALSE) convert_to_base(x, simplify=FALSE, keep_fraction=FALSE)x <- set_units(32, mJ/g) convert_to_base(x) convert_to_base(x, keep_fraction=FALSE) convert_to_base(x, simplify=FALSE) convert_to_base(x, simplify=FALSE, keep_fraction=FALSE)
deparse unit to string in product power form (e.g. km m-2 s-1)
deparse_unit(x)deparse_unit(x)
x |
object of class units |
length one character vector
u = as_units("kg m-2 s-1") u deparse_unit(u)u = as_units("kg m-2 s-1") u deparse_unit(u)
Drop units attribute and class.
drop_units(x) ## S3 method for class 'units' drop_units(x) ## S3 method for class 'data.frame' drop_units(x) ## S3 method for class 'mixed_units' drop_units(x)drop_units(x) ## S3 method for class 'units' drop_units(x) ## S3 method for class 'data.frame' drop_units(x) ## S3 method for class 'mixed_units' drop_units(x)
x |
an object with units metadata. |
Equivalent to units(x) <- NULL, or the pipe-friendly version
set_units(x, NULL), but drop_units will fail if the object has
no units metadata. Use the alternatives if you want this operation to succeed
regardless of the object type.
A data.frame method is also provided, which checks every column and
drops units if any.
the numeric without any units attributes, while preserving other attributes like dimensions or other classes.
x <- 1 y <- set_units(x, m/s) # this succeeds drop_units(y) set_units(y, NULL) set_units(x, NULL) ## Not run: # this fails drop_units(x) ## End(Not run) df <- data.frame(x=x, y=y) df drop_units(df)x <- 1 y <- set_units(x, m/s) # this succeeds drop_units(y) set_units(y, NULL) set_units(x, NULL) ## Not run: # this fails drop_units(x) ## End(Not run) df <- data.frame(x=x, y=y) df drop_units(df)
units objectsHistogram for units objects
## S3 method for class 'units' hist(x, xlab = NULL, main = paste("Histogram of", xname), ...)## S3 method for class 'units' hist(x, xlab = NULL, main = paste("Histogram of", xname), ...)
x |
object of class |
xlab |
character; x axis label |
main |
character; title of histogram |
... |
parameters passed on to |
units_options(parse = FALSE) # otherwise we break on the funny symbol! u = set_units(rnorm(100), degree_C) hist(u)units_options(parse = FALSE) # otherwise we break on the funny symbol! u = set_units(rnorm(100), degree_C) hist(u)
Installing new symbols and/or names allows them to be used in as_units,
make_units and set_units. Optionally, a relationship can be
defined between such symbols/names and existing ones (see details and examples).
install_unit(symbol = character(0), def = character(0), name = character(0)) remove_unit(symbol = character(0), name = character(0))install_unit(symbol = character(0), def = character(0), name = character(0)) remove_unit(symbol = character(0), name = character(0))
symbol |
a vector of symbols to be installed/removed. |
def |
either
|
name |
a vector of names to be installed/removed. |
At least one symbol or name is expected, but multiple symbols and/or names
can be installed (and thus mapped to the same unit) or removed at the same
time. The def argument enables arbitrary relationships with existing
units using UDUNITS-2 syntax:
| String Type | Using Names | Using Symbols | Comment |
| Simple | meter | m | |
| Raised | meter^2 | m2 | higher precedence than multiplying or dividing |
| Product | newton meter | N.m | |
| Quotient | meter per second | m/s | |
| Scaled | 60 second | 60 s | |
| Prefixed | kilometer | km | |
| Offset | kelvin from 273.15 | K @ 273.15 | lower precedence than multiplying or dividing |
| Logarithmic | lg(re milliwatt) | lg(re mW) | "lg" is base 10, "ln" is base e, and "lb" is base 2 |
| Grouped | (5 meter)/(30 second) | (5 m)/(30 s) |
The above may be combined, e.g., "0.1 lg(re m/(5 s)^2) @ 50".
You may also look at the <def> elements in the units database to see
examples of string unit specifications.
# define a fortnight install_unit("fn", "2 week", "fortnight") year <- as_units("year") set_units(year, fn) # by symbol set_units(year, fortnight) # by name # clean up remove_unit("fn", "fortnight") # working with currencies install_unit("dollar") install_unit("euro", "1.22 dollar") install_unit("yen", "0.0079 euro") set_units(as_units("dollar"), yen) # clean up remove_unit(c("dollar", "euro", "yen")) # an example from microbiology cfu_symbols <- c("CFU", "cfu") cfu_names <- c("colony_forming_unit", "ColonyFormingUnit") install_unit("cell") install_unit(cfu_symbols, "3.4 cell", cfu_names) cell <- set_units(2.5e5, cell) vol <- set_units(500, ul) set_units(cell/vol, "cfu/ml") set_units(cell/vol, "CFU/ml") set_units(cell/vol, "colony_forming_unit/ml") set_units(cell/vol, "ColonyFormingUnit/ml") # clean up remove_unit(c("cell", cfu_symbols), cfu_names)# define a fortnight install_unit("fn", "2 week", "fortnight") year <- as_units("year") set_units(year, fn) # by symbol set_units(year, fortnight) # by name # clean up remove_unit("fn", "fortnight") # working with currencies install_unit("dollar") install_unit("euro", "1.22 dollar") install_unit("yen", "0.0079 euro") set_units(as_units("dollar"), yen) # clean up remove_unit(c("dollar", "euro", "yen")) # an example from microbiology cfu_symbols <- c("CFU", "cfu") cfu_names <- c("colony_forming_unit", "ColonyFormingUnit") install_unit("cell") install_unit(cfu_symbols, "3.4 cell", cfu_names) cell <- set_units(2.5e5, cell) vol <- set_units(500, ul) set_units(cell/vol, "cfu/ml") set_units(cell/vol, "CFU/ml") set_units(cell/vol, "colony_forming_unit/ml") set_units(cell/vol, "ColonyFormingUnit/ml") # clean up remove_unit(c("cell", cfu_symbols), cfu_names)
Helper function to apply a function to a units object and then restore
the original units.
keep_units(FUN, x, ..., unit = units(x))keep_units(FUN, x, ..., unit = units(x))
FUN |
the function to be applied. |
x |
first argument of |
... |
optional arguments to |
unit |
symbolic unit to restore after |
Provided for incompatible functions that do not preserve units. The user is responsible for ensuring the correctness of the output.
If x is not a units object
and unit is not provided by the user,
a warning is issued, and the output will also have no units
(see examples).
An object of class units.
x <- set_units(1:5, m) keep_units(drop_units, x) # An example use case is with random number generating functions: mu <- as_units(10, "years") keep_units(rnorm, n = 1, x = mu) # units can be directly specified if needed; for example, with # `rexp()`, the units of the rate parameter are the inverse of # the units of the output: rate <- as_units(3, "1/year") keep_units(rexp, n = 1, x = rate, unit = units(1/rate)) # if `x` does not actually have units, a warning is issued, # and the output has no units: rate2 <- 3 keep_units(rexp, n = 1, x = rate2)x <- set_units(1:5, m) keep_units(drop_units, x) # An example use case is with random number generating functions: mu <- as_units(10, "years") keep_units(rnorm, n = 1, x = mu) # units can be directly specified if needed; for example, with # `rexp()`, the units of the rate parameter are the inverse of # the units of the output: rate <- as_units(3, "1/year") keep_units(rexp, n = 1, x = rate, unit = units(1/rate)) # if `x` does not actually have units, a warning is issued, # and the output has no units: rate2 <- 3 keep_units(rexp, n = 1, x = rate2)
Load an XML database containing a unit system compatible with UDUNITS2.
load_units_xml(path = default_units_xml())load_units_xml(path = default_units_xml())
path |
a path to a valid unit system in XML format. |
A unit system comprises a root <unit-system> and a number of children
defining prefixes (<prefix>) or units (<unit>).
See the contents of
system.file("share/udunits", package="units")
for examples.
# load a new unit system load_units_xml(system.file("share/udunits/udunits2-base.xml", package="units")) ## Not run: set_units(1, rad) # doesn'twork ## End(Not run) # reload the default unit system load_units_xml() set_units(1, rad) # works again# load a new unit system load_units_xml(system.file("share/udunits/udunits2-base.xml", package="units")) ## Not run: set_units(1, rad) # doesn'twork ## End(Not run) # reload the default unit system load_units_xml() set_units(1, rad) # works again
Mathematical operations for units objects
## S3 method for class 'units' Math(x, ...)## S3 method for class 'units' Math(x, ...)
x |
object of class units |
... |
parameters passed on to the Math functions |
Logarithms receive a special treatment by the underlying udunits2
library. If a natural logarithm is applied to some unit, the result is
ln(re 1 unit), which means natural logarithm referenced to
1 unit. For base 2 and base 10 logarithms, the output lb(...)
and lg(...) respectively instead of ln(...).
This is particularly important for some units that are typically expressed in
a logarithmic scale (e.g., bels, or, more commonly, decibels),
such as Watts or Volts. For some of these units, the default udunits2
database contains aliases: e.g., BW (bel-Watts) is an alias of
lg(re 1 W); Bm (bel-milliWatts) is an alias of
lg(re 0.001 W); BV is an alias of lg(re 1 V) (bel-Volts).
See the output of valid_udunits() for further reference.
Additionally, the units package defines B, the bel, by
default (because it is not defined by udunits2) as an alias of
lg(re 1), unless a user-provided XML database already contains a
definition of B, or the define_bel option is set to FALSE
(see help(units_options)).
# roundings, cummulative functions x <- set_units(sqrt(1:10), m/s) signif(x, 2) cumsum(x) # trigonometry sin(x) # not meaningful x <- set_units(sqrt(1:10), rad) sin(x) cos(x) x <- set_units(seq(0, 1, 0.1), 1) asin(x) acos(x) # logarithms x <- set_units(sqrt(1:10), W) log(x) # base exp(1) log(x, base = 3) log2(x) log10(x) set_units(x, dBW) # decibel-watts set_units(x, dBm) # decibel-milliwatts# roundings, cummulative functions x <- set_units(sqrt(1:10), m/s) signif(x, 2) cumsum(x) # trigonometry sin(x) # not meaningful x <- set_units(sqrt(1:10), rad) sin(x) cos(x) x <- set_units(seq(0, 1, 0.1), 1) asin(x) acos(x) # logarithms x <- set_units(sqrt(1:10), W) log(x) # base exp(1) log(x, base = 3) log2(x) log10(x) set_units(x, dBW) # decibel-watts set_units(x, dBm) # decibel-milliwatts
matrixOps functions for units objects.
## S3 method for class 'units' matrixOps(x, y)## S3 method for class 'units' matrixOps(x, y)
x |
object of class |
y |
object of class |
object of class units
a = set_units(1:5, m) a %*% a a %*% t(a) a %*% 1:5 1:5 %*% aa = set_units(1:5, m) a %*% a a %*% t(a) a %*% 1:5 1:5 %*% a
Create or convert to a mixed units list-column
mixed_units(x, values, ...) ## S3 replacement method for class 'mixed_units' units(x) <- valuemixed_units(x, values, ...) ## S3 replacement method for class 'mixed_units' units(x) <- value
x |
numeric, or vector of class |
values |
character vector with units encodings, or list with symbolic units of class |
... |
ignored |
value |
see values |
if x is of class units, values should be missing or of class mixed_symbolic_units; if x is numeric, values should be a character vector the length of x.
a <- 1:4 u <- c("m/s", "km/h", "mg/L", "g") mixed_units(a, u) units(a) = as_units("m/s") mixed_units(a) # converts to mixed representationa <- 1:4 u <- c("m/s", "km/h", "mg/L", "g") mixed_units(a, u) units(a) = as_units("m/s") mixed_units(a) # converts to mixed representation
Ops functions for units objects, including comparison, product and divide, add, subtract.
## S3 method for class 'units' Ops(e1, e2)## S3 method for class 'units' Ops(e1, e2)
e1 |
object of class |
e2 |
object of class |
Users are advised against performing arithmetical operations with temperatures in different units. The units package ensure that results 1) are arithmetically correct, and 2) satisfy dimensional analysis, but could never ensure that results are physically meaningful. Temperature units are special because there is an absolute unit, Kelvin, and relative ones, Celsius and Fahrenheit degrees. Arithmetic operations between them are meaningless from the physical standpoint. Users are thus advised to convert all temperatures to Kelvin before operating.
object of class units
a <- set_units(1:3, m/s) b <- set_units(1:3, m/s) a + b a * b a / b a <- as_units("kg m-3") b <- set_units(1, kg/m/m/m) a + b a = set_units(1:5, m) a %/% a a %/% set_units(2) set_units(1:5, m^2) %/% set_units(2, m) a %% a a %% set_units(2)a <- set_units(1:3, m/s) b <- set_units(1:3, m/s) a + b a * b a / b a <- as_units("kg m-3") b <- set_units(1, kg/m/m/m) a + b a = set_units(1:5, m) a %/% a a %/% set_units(2) set_units(1:5, m^2) %/% set_units(2, m) a %% a a %% set_units(2)
units objectsCreate axis label with appropriate labels.
Plot method for units objects.
make_unit_label(lab, u, sep = units_options("sep"), group = units_options("group"), parse = units_options("parse")) ## S3 method for class 'units' plot(x, y, xlab = NULL, ylab = NULL, ...)make_unit_label(lab, u, sep = units_options("sep"), group = units_options("group"), parse = units_options("parse")) ## S3 method for class 'units' plot(x, y, xlab = NULL, ylab = NULL, ...)
lab |
character; name of the variable to plot |
u |
vector of class |
sep |
length two character vector, defaulting to |
group |
length two character vector with grouping symbols, e.g.
|
parse |
logical; indicates whether a parseable expression should be returned (typically needed for superscripts), or a simple character string without special formatting |
x |
object of class |
y |
object to plot along the y axis, or missing |
xlab |
character; x axis label |
ylab |
character; y axis label |
... |
other parameters, passed on to plot.default |
units_options can be used to set and change the
defaults for sep, group and doParse.
displacement = mtcars$disp * as_units("in")^3 units(displacement) = make_units(cm^3) weight = mtcars$wt * 1000 * make_units(lb) units(weight) = make_units(kg) plot(weight, displacement) units_options(group = c("(", ")") ) # parenthesis instead of square brackets plot(weight, displacement) units_options(sep = c("~~~", "~"), group = c("", "")) # extra space; no brackets plot(weight, displacement) units_options(sep = c("~", "~~"), group = c("[", "]")) gallon = as_units("gallon") consumption = mtcars$mpg * make_units(mi/gallon) units(consumption) = make_units(km/l) plot(displacement, consumption) # division in consumption units_options(negative_power = TRUE) # division becomes ^-1 plot(displacement, consumption) plot(1/displacement, 1/consumption)displacement = mtcars$disp * as_units("in")^3 units(displacement) = make_units(cm^3) weight = mtcars$wt * 1000 * make_units(lb) units(weight) = make_units(kg) plot(weight, displacement) units_options(group = c("(", ")") ) # parenthesis instead of square brackets plot(weight, displacement) units_options(sep = c("~~~", "~"), group = c("", "")) # extra space; no brackets plot(weight, displacement) units_options(sep = c("~", "~~"), group = c("[", "]")) gallon = as_units("gallon") consumption = mtcars$mpg * make_units(mi/gallon) units(consumption) = make_units(km/l) plot(displacement, consumption) # division in consumption units_options(negative_power = TRUE) # division becomes ^-1 plot(displacement, consumption) plot(1/displacement, 1/consumption)
These are the default scales for the units class. These will usually
be added automatically. To override manually, use scale_{type}_units.
scale_x_units(..., sec.axis = ggplot2::waiver(), unit = NULL) scale_y_units(..., sec.axis = ggplot2::waiver(), unit = NULL) scale_colour_units(..., unit = NULL) scale_color_units(..., unit = NULL) scale_fill_units(..., unit = NULL) scale_alpha_units(..., unit = NULL) scale_size_units(..., unit = NULL) scale_size_area_units(..., unit = NULL) scale_radius_units(..., unit = NULL) scale_linewidth_units(..., unit = NULL)scale_x_units(..., sec.axis = ggplot2::waiver(), unit = NULL) scale_y_units(..., sec.axis = ggplot2::waiver(), unit = NULL) scale_colour_units(..., unit = NULL) scale_color_units(..., unit = NULL) scale_fill_units(..., unit = NULL) scale_alpha_units(..., unit = NULL) scale_size_units(..., unit = NULL) scale_size_area_units(..., unit = NULL) scale_radius_units(..., unit = NULL) scale_linewidth_units(..., unit = NULL)
... |
arguments passed on to the corresponding continuous scale
(see the manual page for each |
sec.axis |
|
unit |
A unit specification to use for the guide. If given, the values will be converted to this unit before plotting. An error will be thrown if the specified unit is incompatible with the unit of the data. |
The scale internally uses make_unit_label, which by
default parses the scale (e.g. typically needed for superscripts). However,
if the scale name contains reserved words in R (e.g. "in"), the parsing may
fail. In that case, the user can set units_options(parse = FALSE) to
get a simple character string as label instead of a parseable expression.
if (requireNamespace("ggplot2", quietly=TRUE)) { library(ggplot2) mtcars$consumption <- set_units(mtcars$mpg, mi / gallon) mtcars$power <- set_units(mtcars$hp, hp) # Use units encoded into the data ggplot(mtcars) + geom_point(aes(power, consumption)) # Convert units on the fly during plotting ggplot(mtcars) + geom_point(aes(power, consumption)) + scale_x_units(unit = "W") + scale_y_units(unit = "km/l") # Resolve units when transforming data ggplot(mtcars) + geom_point(aes(power, 1 / consumption)) # Reverse the y axis ggplot(mtcars) + geom_point(aes(power, consumption)) + scale_y_units(transform="reverse") }if (requireNamespace("ggplot2", quietly=TRUE)) { library(ggplot2) mtcars$consumption <- set_units(mtcars$mpg, mi / gallon) mtcars$power <- set_units(mtcars$hp, hp) # Use units encoded into the data ggplot(mtcars) + geom_point(aes(power, consumption)) # Convert units on the fly during plotting ggplot(mtcars) + geom_point(aes(power, consumption)) + scale_x_units(unit = "W") + scale_y_units(unit = "km/l") # Resolve units when transforming data ggplot(mtcars) + geom_point(aes(power, 1 / consumption)) # Reverse the y axis ggplot(mtcars) + geom_point(aes(power, consumption)) + scale_y_units(transform="reverse") }
seq method for units objects
## S3 method for class 'units' seq(from, to, by = ((to - from)/(length.out - 1)), length.out = NULL, along.with = NULL, ...)## S3 method for class 'units' seq(from, to, by = ((to - from)/(length.out - 1)), length.out = NULL, along.with = NULL, ...)
from |
see seq |
to |
see seq |
by |
see seq |
length.out |
see seq |
along.with |
see seq |
... |
see seq |
arguments with units are converted to have units of the first argument (which is either from or to)
seq(to = set_units(10, m), by = set_units(1, m), length.out = 5) seq(set_units(10, m), by = set_units(1, m), length.out = 5) seq(set_units(10, m), set_units(19, m)) seq(set_units(10, m), set_units(.1, km), set_units(10000, mm))seq(to = set_units(10, m), by = set_units(1, m), length.out = 5) seq(set_units(10, m), by = set_units(1, m), length.out = 5) seq(set_units(10, m), set_units(19, m)) seq(set_units(10, m), set_units(.1, km), set_units(10000, mm))
Some udunits2 utilities are exposed to the user. These functions are useful for checking whether units are convertible or converting between units without having to create units objects. Arguments are recycled if necessary.
ud_are_convertible(from, to, ...) ud_convert(x, from, to)ud_are_convertible(from, to, ...) ud_convert(x, from, to)
from, to
|
character vector or object of class |
... |
unused. |
x |
numeric vector |
ud_are_convertible
returns TRUE if both units exist and are convertible,
FALSE otherwise.
ud_convert
returns a numeric vector with x converted to new unit.
ud_are_convertible(c("m", "mm"), "km") ud_convert(c(100, 100000), c("m", "mm"), "km") a <- set_units(1:3, m/s) ud_are_convertible(units(a), "km/h") ud_convert(1:3, units(a), "km/h") ud_are_convertible("degF", "degC") ud_convert(32, "degF", "degC")ud_are_convertible(c("m", "mm"), "km") ud_convert(c(100, 100000), c("m", "mm"), "km") a <- set_units(1:3, m/s) ud_are_convertible(units(a), "km/h") ud_convert(1:3, units(a), "km/h") ud_are_convertible("degF", "degC") ud_convert(32, "degF", "degC")
The "unit" type for vectors that are actually dimension-less.
unitlessunitless
An object of class symbolic_units of length 2.
A number of functions are provided for handling unit objects.
`units<-` and units are the basic functions to set
and retrieve units.
as_units, a generic with methods for a
character string and for quoted language. Note, direct usage of this function
by users is typically not necessary, as coercion via as_units is
automatically done with `units<-` and set_units.
make_units, constructs units from bare expressions.
make_units(m/s) is equivalent to as_units(quote(m/s)).
set_units, a pipe-friendly version of `units<-`. By
default it operates with bare expressions, but this
behavior can be disabled by specifying mode = "standard" or setting
units_options(set_units_mode = "standard").
If value is missing or set to 1, the object becomes unitless.
## S3 replacement method for class 'numeric' units(x) <- value ## S3 replacement method for class 'units' units(x) <- value ## S3 replacement method for class 'logical' units(x) <- value ## S3 method for class 'units' units(x) ## S3 method for class 'symbolic_units' units(x) set_units(x, value, ..., mode = units_options("set_units_mode")) make_units(bare_expression, check_is_valid = TRUE) as_units(x, ...) ## Default S3 method: as_units(x, value = unitless, ...) ## S3 method for class 'units' as_units(x, value, ...) ## S3 method for class 'symbolic_units' as_units(x, value, ...) ## S3 method for class 'difftime' as_units(x, value, ...) ## S3 method for class 'character' as_units(x, ..., check_is_valid = TRUE, force_single_symbol = FALSE) ## S3 method for class 'call' as_units(x, ...) ## S3 method for class 'expression' as_units(x, ...) ## S3 method for class 'name' as_units(x, ...) ## S3 method for class 'POSIXt' as_units(x, value, ...) ## S3 method for class 'Date' as_units(x, value, ...)## S3 replacement method for class 'numeric' units(x) <- value ## S3 replacement method for class 'units' units(x) <- value ## S3 replacement method for class 'logical' units(x) <- value ## S3 method for class 'units' units(x) ## S3 method for class 'symbolic_units' units(x) set_units(x, value, ..., mode = units_options("set_units_mode")) make_units(bare_expression, check_is_valid = TRUE) as_units(x, ...) ## Default S3 method: as_units(x, value = unitless, ...) ## S3 method for class 'units' as_units(x, value, ...) ## S3 method for class 'symbolic_units' as_units(x, value, ...) ## S3 method for class 'difftime' as_units(x, value, ...) ## S3 method for class 'character' as_units(x, ..., check_is_valid = TRUE, force_single_symbol = FALSE) ## S3 method for class 'call' as_units(x, ...) ## S3 method for class 'expression' as_units(x, ...) ## S3 method for class 'name' as_units(x, ...) ## S3 method for class 'POSIXt' as_units(x, value, ...) ## S3 method for class 'Date' as_units(x, value, ...)
x |
numeric vector, or object of class |
value |
object of class |
... |
passed on to other methods. |
mode |
if |
bare_expression |
a bare R expression describing units. Must be valid R
syntax (reserved R syntax words like |
check_is_valid |
throw an error if all the unit symbols are either not
recognized by udunits2 or not custom units
defined via |
force_single_symbol |
whether to perform no string parsing and force treatment of the string as a single symbol. |
If value is of class units and has a value unequal to 1, this
value is ignored unless units_options("simplify") is TRUE. If
simplify is TRUE, x is multiplied by this value.
An object of class units.
The units method retrieves the units attribute, which is of
class symbolic_units.
Generally speaking, there are 3 types of unit strings are accepted in
as_units (and by extension, `units<-`).
The first type, and likely most common, is a "standard" format unit
specification where the relationship between unit symbols or names is
specified explicitly with arithmetic symbols for division /,
multiplication * and power exponents ^.
The second type of unit string accepted is one with implicit exponents. In
this format, /, *, and ^, may not be present in the
string, and unit symbol or names must be separated by a space. Each unit
symbol may optionally be followed by a single number, specifying the power.
For example "m2 s-2" is equivalent to "(m^2)*(s^-2)".
If the string supplied fails to parse, then the string is treated as a
single symbolic unit and symbolic_unit(chr) is used as a fallback
with a warning. In that case, automatic unit simplification may not work
properly when performing operations on unit objects, but unit conversion
and other Math operations should still give correct results so long as
the unit string supplied returns TRUE for ud_is_parsable().
It must be noted that prepended numbers are supported too, but are not
treated as magnitudes. For example, "1000 m" is interpreted as
a prefixed unit, and it is equivalent to "km" to all effects.
The third type of unit string format accepted is the special case of
udunits time duration with a reference origin, for example "hours
since 1970-01-01 00:00:00". Note, that the handling of time and calendar
operations via the udunits library is subtly different from the way R
handles date and time operations. This functionality is mostly exported for
users that work with udunits time data, e.g., with NetCDF files. Users are
otherwise encouraged to use R's date and time functionality provided
by Date and POSIXt classes.
By default, unit names are automatically substituted with unit symbols
(e.g., kilogram –> kg). To turn off this behavior, set
units_options(auto_convert_names_to_symbols = FALSE)
x = 1:3 class(x) units(x) <- as_units("m/s") class(x) y = 2:5 a <- set_units(1:3, m/s) units(a) <- make_units(km/h) a # convert to a mixed_units object: units(a) <- c("m/s", "km/h", "km/h") a # The easiest way to assign units to a numeric vector is like this: x <- y <- 1:4 units(x) <- "m/s" # meters / second # Alternatively, the easiest pipe-friendly way to set units: if(requireNamespace("magrittr", quietly = TRUE)) { library(magrittr) y %>% set_units(m/s) } # these are different ways of creating the same unit: # meters per second squared, i.e, acceleration x1 <- make_units(m/s^2) x2 <- as_units(quote(m/s^2)) x2 <- as_units("m/s^2") x3 <- as_units("m s-2") # in product power form, i.e., implicit exponents = T x4 <- set_units(1, m/s^2) # by default, mode = "symbols" x5 <- set_units(1, "m/s^2", mode = "standard") x6 <- set_units(1, x1, mode = "standard") x7 <- set_units(1, units(x1), mode = "standard") x8 <- as_units("m") / as_units("s")^2 all_identical <- function(...) { l <- list(...) for(i in seq_along(l)[-1]) if(!identical(l[[1]], l[[i]])) return(FALSE) TRUE } all_identical(x1, x2, x3, x4, x5, x6, x7, x8) # Note, direct usage of these unit creation functions is typically not # necessary, since coercion is automatically done via as_units(). Again, # these are all equivalent ways to generate the same result. x1 <- x2 <- x3 <- x4 <- x5 <- x6 <- x7 <- x8 <- 1:4 units(x1) <- "m/s^2" units(x2) <- "m s-2" units(x3) <- quote(m/s^2) units(x4) <- make_units(m/s^2) units(x5) <- as_units(quote(m/s^2)) x6 <- set_units(x6, m/s^2) x7 <- set_units(x7, "m/s^2", mode = "standard") x8 <- set_units(x8, units(x1), mode = "standard") all_identical(x1, x2, x3, x4, x5, x6, x7, x8) # Both unit names or symbols can be used. By default, unit names are # automatically converted to unit symbols. make_units(degree_C) make_units(kilogram) make_units(ohm) ## Arithmetic operations and units # conversion between unit objects that were defined as symbols and names will # work correctly, although unit simplification in printing may not always occur. x <- 500 * make_units(micrograms/liter) y <- set_units(200, ug/l) x + y x * y # numeric result is correct, but units not simplified completely # note, plural form of unit name accepted too ('liters' vs 'liter'), and # denominator simplification can be performed correctly x * set_units(5, liters) # unit conversion works too set_units(x, grams/gallon) ## Creating custom, user defined units # For example, a microbiologist might work with counts of bacterial cells # make_units(cells/ml) # by default, throws an ERROR # First define the unit, then the newly defined unit is accepted. install_unit("cells") make_units(cells/ml) # Note that install_unit() adds support for defining relationships between # the newly created symbols or names and existing units. ## set_units() # set_units is a pipe friendly version of `units<-`. if(requireNamespace("magrittr", quietly = TRUE)) { library(magrittr) 1:5 %>% set_units(N/m^2) # first sets to m, then converts to km 1:5 %>% set_units(m) %>% set_units(km) } # set_units has two modes of operation. By default, it operates with # bare symbols to define the units. set_units(1:5, m/s) # use `mode = "standard"` to use the value of supplied argument, rather than # the bare symbols of the expression. In this mode, set_units() can be # thought of as a simple alias for `units<-` that is pipe friendly. set_units(1:5, "m/s", mode = "standard") set_units(1:5, make_units(m/s), mode = "standard") # the mode of set_units() can be controlled via a global option # units_options(set_units_mode = "standard") # To remove units use units(x) <- NULL # or set_units(x, NULL) # or drop_units(y) # Use difftime object as units s = Sys.time() d = s - (s+1) as_units(d)x = 1:3 class(x) units(x) <- as_units("m/s") class(x) y = 2:5 a <- set_units(1:3, m/s) units(a) <- make_units(km/h) a # convert to a mixed_units object: units(a) <- c("m/s", "km/h", "km/h") a # The easiest way to assign units to a numeric vector is like this: x <- y <- 1:4 units(x) <- "m/s" # meters / second # Alternatively, the easiest pipe-friendly way to set units: if(requireNamespace("magrittr", quietly = TRUE)) { library(magrittr) y %>% set_units(m/s) } # these are different ways of creating the same unit: # meters per second squared, i.e, acceleration x1 <- make_units(m/s^2) x2 <- as_units(quote(m/s^2)) x2 <- as_units("m/s^2") x3 <- as_units("m s-2") # in product power form, i.e., implicit exponents = T x4 <- set_units(1, m/s^2) # by default, mode = "symbols" x5 <- set_units(1, "m/s^2", mode = "standard") x6 <- set_units(1, x1, mode = "standard") x7 <- set_units(1, units(x1), mode = "standard") x8 <- as_units("m") / as_units("s")^2 all_identical <- function(...) { l <- list(...) for(i in seq_along(l)[-1]) if(!identical(l[[1]], l[[i]])) return(FALSE) TRUE } all_identical(x1, x2, x3, x4, x5, x6, x7, x8) # Note, direct usage of these unit creation functions is typically not # necessary, since coercion is automatically done via as_units(). Again, # these are all equivalent ways to generate the same result. x1 <- x2 <- x3 <- x4 <- x5 <- x6 <- x7 <- x8 <- 1:4 units(x1) <- "m/s^2" units(x2) <- "m s-2" units(x3) <- quote(m/s^2) units(x4) <- make_units(m/s^2) units(x5) <- as_units(quote(m/s^2)) x6 <- set_units(x6, m/s^2) x7 <- set_units(x7, "m/s^2", mode = "standard") x8 <- set_units(x8, units(x1), mode = "standard") all_identical(x1, x2, x3, x4, x5, x6, x7, x8) # Both unit names or symbols can be used. By default, unit names are # automatically converted to unit symbols. make_units(degree_C) make_units(kilogram) make_units(ohm) ## Arithmetic operations and units # conversion between unit objects that were defined as symbols and names will # work correctly, although unit simplification in printing may not always occur. x <- 500 * make_units(micrograms/liter) y <- set_units(200, ug/l) x + y x * y # numeric result is correct, but units not simplified completely # note, plural form of unit name accepted too ('liters' vs 'liter'), and # denominator simplification can be performed correctly x * set_units(5, liters) # unit conversion works too set_units(x, grams/gallon) ## Creating custom, user defined units # For example, a microbiologist might work with counts of bacterial cells # make_units(cells/ml) # by default, throws an ERROR # First define the unit, then the newly defined unit is accepted. install_unit("cells") make_units(cells/ml) # Note that install_unit() adds support for defining relationships between # the newly created symbols or names and existing units. ## set_units() # set_units is a pipe friendly version of `units<-`. if(requireNamespace("magrittr", quietly = TRUE)) { library(magrittr) 1:5 %>% set_units(N/m^2) # first sets to m, then converts to km 1:5 %>% set_units(m) %>% set_units(km) } # set_units has two modes of operation. By default, it operates with # bare symbols to define the units. set_units(1:5, m/s) # use `mode = "standard"` to use the value of supplied argument, rather than # the bare symbols of the expression. In this mode, set_units() can be # thought of as a simple alias for `units<-` that is pipe friendly. set_units(1:5, "m/s", mode = "standard") set_units(1:5, make_units(m/s), mode = "standard") # the mode of set_units() can be controlled via a global option # units_options(set_units_mode = "standard") # To remove units use units(x) <- NULL # or set_units(x, NULL) # or drop_units(y) # Use difftime object as units s = Sys.time() d = s - (s+1) as_units(d)
set units global options, mostly related how units are printed and plotted
units_options(..., sep, group, negative_power, parse, set_units_mode, strict_tokenizer, auto_convert_names_to_symbols, simplify, allow_mixed, unitless_symbol, define_bel)units_options(..., sep, group, negative_power, parse, set_units_mode, strict_tokenizer, auto_convert_names_to_symbols, simplify, allow_mixed, unitless_symbol, define_bel)
... |
named options (character) for which the value is queried |
sep |
character length two; default |
group |
character length two; start and end group, may be two empty strings, a parenthesis pair, or square brackets; default: square brackets. |
negative_power |
logical, default |
parse |
logical, default |
set_units_mode |
character; either |
strict_tokenizer |
logical, default |
auto_convert_names_to_symbols |
logical, default |
simplify |
logical, default |
allow_mixed |
logical; if |
unitless_symbol |
character; set the symbol to use for unitless (1) units |
define_bel |
logical; if |
This sets or gets units options. Set them by using named arguments, get them by passing the option name.
The default NA value for simplify means units are not simplified in set_units or as_units, but are simplified in arithmetical expressions.
in case options are set, invisibly a named list with the option values that are being set; if an option is queried, the current option value.
old = units_options(sep = c("~~~", "~"), group = c("", "")) # more space, parenthesis old ## set back to defaults: units_options(sep = c("~", "~"), group = c("[", "]"), negative_power = FALSE, parse = TRUE) units_options("group")old = units_options(sep = c("~~~", "~"), group = c("", "")) # more space, parenthesis old ## set back to defaults: units_options(sep = c("~", "~"), group = c("[", "]"), negative_power = FALSE, parse = TRUE) units_options("group")
These functions are no longer available.
ud_units: Use as_units instead.
as.units: Use as_units instead.
make_unit: Use as_units instead.
parse_unit: Use as_units instead.
as_cf: Use deparse_unit instead.
install_symbolic_unit: Use install_unit instead.
remove_symbolic_unit: Use remove_unit instead.
install_conversion_constant: Use install_unit instead.
install_conversion_offset: Use install_unit instead.
These functions require the xml2 package, and return data frames with
complete information about pre-defined units from UDUNITS2. Inspect these
data frames to determine what inputs are accepted by as_units (and the
other functions it powers: as_units, set_units, units<-).
valid_udunits(quiet = FALSE) valid_udunits_prefixes(quiet = FALSE)valid_udunits(quiet = FALSE) valid_udunits_prefixes(quiet = FALSE)
quiet |
logical; |
Any entry listed under symbol, symbol_aliases,
name_singular, name_singular_aliases, name_plural or
name_plural_aliases is valid. Additionally, any entry under
symbol or symbol_aliases may also contain a valid prefix,
as specified by valid_udunits_prefixes() .
Note, this is primarily intended for interactive use. The exact format of the returned data frames may change in the future.
A data frame with columns symbol, symbol_aliases,
name_singular, name_singular_aliases, name_plural,
name_plural_aliases, def, definition,
comment, dimensionless and source_xml.
if (requireNamespace("xml2", quietly = TRUE)) { valid_udunits() valid_udunits_prefixes() if(interactive()) View(valid_udunits()) }if (requireNamespace("xml2", quietly = TRUE)) { valid_udunits() valid_udunits_prefixes() if(interactive()) View(valid_udunits()) }