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] , Thomas Mailund [aut], Tomasz Kalinowski [aut], James Hiebert [ctb], Iñaki Ucar [aut] , Thomas Lin Pedersen [ctb] |
Maintainer: | Edzer Pebesma <[email protected]> |
License: | GPL-2 |
Version: | 0.8-5.6 |
Built: | 2024-11-20 05:06:06 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) dt
t1 = Sys.time() t2 = t1 + 3600 d = t2 - t1 du <- as_units(d) dt = as_difftime(du) class(dt) dt
boxplot for unit objects
## S3 method for class 'units' boxplot(x, ..., horizontal = FALSE)
## S3 method for class 'units' boxplot(x, ..., horizontal = FALSE)
x |
object of class units, for which we want to plot the boxplot |
... |
parameters passed on to boxplot.default |
horizontal |
logical indicating if the boxplots should be horizontal; default FALSE means vertical boxes. |
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)
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", implicit_exponents = TRUE) u deparse_unit(u)
u = as_units("kg m-2 s-1", implicit_exponents = TRUE) 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)
histogram for unit 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 units, for which we want to plot the histogram |
xlab |
character; x axis label |
main |
character; title of histogram |
... |
parameters passed on to hist.default |
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.
An object of class units
.
x <- set_units(1:5, m) keep_units(drop_units, x)
x <- set_units(1:5, m) keep_units(drop_units, x)
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 (i.e., 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),
and so on and so forth (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
Create or convert to a mixed units list-column
mixed_units(x, values, ...) ## S3 replacement method for class 'mixed_units' units(x) <- value
mixed_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 representation
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 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 |
length one 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 super scripts), or a simple character string without special formatting. |
x |
object of class units, to plot along the x axis, or, if y is missing, along the y axis |
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("", "")) # no brackets; extra space 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("", "")) # no brackets; extra space 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_*_units
.
scale_x_units(..., guide = ggplot2::waiver(), position = "bottom", sec.axis = ggplot2::waiver(), unit = NULL) scale_y_units(..., guide = ggplot2::waiver(), position = "left", sec.axis = ggplot2::waiver(), unit = NULL)
scale_x_units(..., guide = ggplot2::waiver(), position = "bottom", sec.axis = ggplot2::waiver(), unit = NULL) scale_y_units(..., guide = ggplot2::waiver(), position = "left", sec.axis = ggplot2::waiver(), unit = NULL)
... |
arguments passed on to |
guide |
A function used to create a guide or its name. See
|
position |
For position scales, The position of the axis.
|
sec.axis |
|
unit |
A unit specification to use for the axis. 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. |
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(trans="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(trans="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))
Parses and checks whether units can be converted by UDUNITS-2. Units may not be convertible either because they are different magnitudes or because one (or both) units are not defined in the database.
ud_are_convertible(x, y)
ud_are_convertible(x, y)
x |
character or object of class |
y |
character or object of class |
boolean, TRUE
if both units exist and are convertible.
ud_are_convertible("m", "km") a <- set_units(1:3, m/s) ud_are_convertible(units(a), "km/h") ud_are_convertible("s", "kg")
ud_are_convertible("m", "km") a <- set_units(1:3, m/s) ud_are_convertible(units(a), "km/h") ud_are_convertible("s", "kg")
The "unit" type for vectors that are actually dimension-less.
unitless
unitless
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 a 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, implicit_exponents = NULL, force_single_symbol = FALSE, ...) ## S3 method for class 'call' as_units(x, check_is_valid = TRUE, ...) ## S3 method for class 'expression' as_units(x, check_is_valid = TRUE, ...) ## S3 method for class 'name' as_units(x, check_is_valid = TRUE, ...) ## 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, implicit_exponents = NULL, force_single_symbol = FALSE, ...) ## S3 method for class 'call' as_units(x, check_is_valid = TRUE, ...) ## S3 method for class 'expression' as_units(x, check_is_valid = TRUE, ...) ## S3 method for class 'name' as_units(x, check_is_valid = TRUE, ...) ## 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 not either
recognized by udunits2 via |
implicit_exponents |
If the unit string is in product power form (e.g.
|
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("simplifiy")
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, 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 ^
, or other mathematical
functions like log()
. In this case, the string is parsed as an R
expression via parse(text = )
after backticking all unit symbols and
names, and then passed on to as_units.call()
. A heuristic is used to
perform backticking, such that any continuous set of characters
uninterrupted by one of ()\*^-
are backticked (unless the character
sequence consists solely of numbers 0-9
), with some care to not
double up on pre-existing backticks. This heuristic appears to be quite
robust, and works for units would otherwise not be valid R syntax. For
example, percent ("%"
), feet ("'"
), inches ("in"
),
and Tesla ("T"
) are all backticked and parsed correctly.
Nevertheless, for certain complex unit expressions, this backticking heuristic
may give incorrect results. If the string supplied fails to parse as an R
expression, 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()
.
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)"
.
It must be noted that prepended numbers are supported too, but their
interpretation slightly varies depending on whether they are separated from
the unit string or not. E.g., "1000 m"
is interpreted as magnitude
and unit, but "1000m"
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.
In as_units()
, each of the symbols in the unit expression is treated
individually, such that each symbol must be recognized by the udunits
database (checked by ud_is_parseable()
, or be a custom,
user-defined unit symbol that was defined by install_unit()
. To
see which symbols and names are currently recognized by the udunits
database, see valid_udunits()
.
By default, unit names are automatically substituted with unit names
(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) 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) 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, auto_convert_names_to_symbols, simplify, allow_mixed, unitless_symbol, define_bel)
units_options(..., sep, group, negative_power, parse, set_units_mode, 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 |
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 this 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, defaults |
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 can 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
,
or 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()) }