| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/conversion.R, R/make_units.R |
| 3 |
\name{units} |
| 4 |
\alias{units} |
| 5 |
\alias{units<-.numeric} |
| 6 |
\alias{units<-.units} |
| 7 |
\alias{units<-.logical} |
| 8 |
\alias{units.units} |
| 9 |
\alias{units.symbolic_units} |
| 10 |
\alias{set_units} |
| 11 |
\alias{make_units} |
| 12 |
\alias{as_units} |
| 13 |
\alias{as_units.default} |
| 14 |
\alias{as_units.units} |
| 15 |
\alias{as_units.symbolic_units} |
| 16 |
\alias{as_units.difftime} |
| 17 |
\alias{as_units.character} |
| 18 |
\alias{as_units.call} |
| 19 |
\alias{as_units.expression} |
| 20 |
\alias{as_units.name} |
| 21 |
\alias{as_units.POSIXt} |
| 22 |
\alias{as_units.Date} |
| 23 |
\title{Handle measurement units} |
| 24 |
\usage{ |
| 25 |
\method{units}{numeric}(x) <- value |
| 26 |
|
| 27 |
\method{units}{units}(x) <- value |
| 28 |
|
| 29 |
\method{units}{logical}(x) <- value |
| 30 |
|
| 31 |
\method{units}{units}(x) |
| 32 |
|
| 33 |
\method{units}{symbolic_units}(x) |
| 34 |
|
| 35 |
set_units(x, value, ..., mode = units_options("set_units_mode")) |
| 36 |
|
| 37 |
make_units(bare_expression, check_is_valid = TRUE) |
| 38 |
|
| 39 |
as_units(x, ...) |
| 40 |
|
| 41 |
\method{as_units}{default}(x, value = unitless, ...) |
| 42 |
|
| 43 |
\method{as_units}{units}(x, value, ...) |
| 44 |
|
| 45 |
\method{as_units}{symbolic_units}(x, value, ...) |
| 46 |
|
| 47 |
\method{as_units}{difftime}(x, value, ...) |
| 48 |
|
| 49 |
\method{as_units}{character}(x, ..., check_is_valid = TRUE, |
| 50 |
force_single_symbol = FALSE) |
| 51 |
|
| 52 |
\method{as_units}{call}(x, ...) |
| 53 |
|
| 54 |
\method{as_units}{expression}(x, ...) |
| 55 |
|
| 56 |
\method{as_units}{name}(x, ...) |
| 57 |
|
| 58 |
\method{as_units}{POSIXt}(x, value, ...) |
| 59 |
|
| 60 |
\method{as_units}{Date}(x, value, ...) |
| 61 |
} |
| 62 |
\arguments{ |
| 63 |
\item{x}{numeric vector, or object of class \code{units}.} |
| 64 |
|
| 65 |
\item{value}{object of class \code{units} or \code{symbolic_units}, or in the |
| 66 |
case of \code{set_units} expression with symbols (see examples).} |
| 67 |
|
| 68 |
\item{...}{passed on to other methods.} |
| 69 |
|
| 70 |
\item{mode}{if \code{"symbols"} (the default), then unit is constructed from |
| 71 |
the expression supplied. Otherwise, if\code{mode = "standard"}, |
| 72 |
standard evaluation is used for the supplied value This argument can be set |
| 73 |
via a global option \code{units_options(set_units_mode = "standard")}} |
| 74 |
|
| 75 |
\item{bare_expression}{a bare R expression describing units. Must be valid R |
| 76 |
syntax (reserved R syntax words like \code{in} must be backticked)} |
| 77 |
|
| 78 |
\item{check_is_valid}{throw an error if all the unit symbols are not either |
| 79 |
recognized by udunits2, or a custom |
| 80 |
user defined via \code{install_unit()}. If \code{FALSE}, no check |
| 81 |
for validity is performed.} |
| 82 |
|
| 83 |
\item{force_single_symbol}{Whether to perform no string parsing and force |
| 84 |
treatment of the string as a single symbol.} |
| 85 |
} |
| 86 |
\value{ |
| 87 |
An object of class \code{units}. |
| 88 |
|
| 89 |
The \code{units} method retrieves the units attribute, which is of |
| 90 |
class \code{symbolic_units}. |
| 91 |
} |
| 92 |
\description{ |
| 93 |
A number of functions are provided for handling unit objects. |
| 94 |
\itemize{ |
| 95 |
\item \code{`units<-`} and \code{units} are the basic functions to set |
| 96 |
and retrieve units. |
| 97 |
\item \code{as_units}, a generic with methods for a |
| 98 |
character string and for quoted language. Note, direct usage of this function |
| 99 |
by users is typically not necessary, as coercion via \code{as_units} is |
| 100 |
automatically done with \code{`units<-`} and \code{set_units}. |
| 101 |
\item \code{make_units}, constructs units from bare expressions. |
| 102 |
\code{make_units(m/s)} is equivalent to \code{as_units(quote(m/s))}. |
| 103 |
\item \code{set_units}, a pipe-friendly version of \code{`units<-`}. By |
| 104 |
default it operates with bare expressions, but this |
| 105 |
behavior can be disabled by a specifying \code{mode = "standard"} or setting |
| 106 |
\code{units_options(set_units_mode = "standard")}. |
| 107 |
If \code{value} is missing or set to \code{1}, the object becomes unitless. |
| 108 |
} |
| 109 |
} |
| 110 |
\details{ |
| 111 |
If \code{value} is of class \code{units} and has a value unequal to 1, this |
| 112 |
value is ignored unless \code{units_options("simplifiy")} is \code{TRUE}. If |
| 113 |
\code{simplify} is \code{TRUE}, \code{x} is multiplied by this value. |
| 114 |
} |
| 115 |
\note{ |
| 116 |
By default, unit names are automatically substituted with unit names |
| 117 |
(e.g., kilogram --> kg). To turn off this behavior, set |
| 118 |
\code{units_options(auto_convert_names_to_symbols = FALSE)} |
| 119 |
} |
| 120 |
\section{Character strings}{ |
| 121 |
|
| 122 |
|
| 123 |
Generally speaking, there are 3 types of unit strings are accepted in |
| 124 |
\code{as_units} (and by extension, \code{`units<-`}). |
| 125 |
|
| 126 |
The first type, and likely most common, is a "standard" format unit |
| 127 |
specification where the relationship between unit symbols or names is |
| 128 |
specified explicitly with arithmetic symbols for division \code{/}, |
| 129 |
multiplication \code{*} and power exponents \code{^}. |
| 130 |
|
| 131 |
The second type of unit string accepted is one with implicit exponents. In |
| 132 |
this format, \code{/}, \code{*}, and \code{^}, may not be present in the |
| 133 |
string, and unit symbol or names must be separated by a space. Each unit |
| 134 |
symbol may optionally be followed by a single number, specifying the power. |
| 135 |
For example \code{"m2 s-2"} is equivalent to \code{"(m^2)*(s^-2)"}. |
| 136 |
|
| 137 |
If the string supplied fails to parse, then the string is treated as a |
| 138 |
single symbolic unit and \code{symbolic_unit(chr)} is used as a fallback |
| 139 |
with a warning. In that case, automatic unit simplification may not work |
| 140 |
properly when performing operations on unit objects, but unit conversion |
| 141 |
and other Math operations should still give correct results so long as |
| 142 |
the unit string supplied returns \code{TRUE} for \code{ud_is_parsable()}. |
| 143 |
|
| 144 |
It must be noted that prepended numbers are supported too, but are not |
| 145 |
treated as magnitudes. For example, \code{"1000 m"} is interpreted as |
| 146 |
a prefixed unit, and it is equivalent to \code{"km"} to all effects. |
| 147 |
|
| 148 |
The third type of unit string format accepted is the special case of |
| 149 |
udunits time duration with a reference origin, for example \code{"hours |
| 150 |
since 1970-01-01 00:00:00"}. Note, that the handling of time and calendar |
| 151 |
operations via the udunits library is subtly different from the way R |
| 152 |
handles date and time operations. This functionality is mostly exported for |
| 153 |
users that work with udunits time data, e.g., with NetCDF files. Users are |
| 154 |
otherwise encouraged to use \code{R}'s date and time functionality provided |
| 155 |
by \code{Date} and \code{POSIXt} classes. |
| 156 |
} |
| 157 |
|
| 158 |
\examples{ |
| 159 |
x = 1:3 |
| 160 |
class(x) |
| 161 |
units(x) <- as_units("m/s") |
| 162 |
class(x) |
| 163 |
y = 2:5 |
| 164 |
a <- set_units(1:3, m/s) |
| 165 |
units(a) <- make_units(km/h) |
| 166 |
a |
| 167 |
# convert to a mixed_units object: |
| 168 |
units(a) <- c("m/s", "km/h", "km/h") |
| 169 |
a |
| 170 |
# The easiest way to assign units to a numeric vector is like this: |
| 171 |
x <- y <- 1:4 |
| 172 |
units(x) <- "m/s" # meters / second |
| 173 |
|
| 174 |
# Alternatively, the easiest pipe-friendly way to set units: |
| 175 |
if(requireNamespace("magrittr", quietly = TRUE)) { |
| 176 |
library(magrittr) |
| 177 |
y \%>\% set_units(m/s) |
| 178 |
} |
| 179 |
|
| 180 |
# these are different ways of creating the same unit: |
| 181 |
# meters per second squared, i.e, acceleration |
| 182 |
x1 <- make_units(m/s^2) |
| 183 |
x2 <- as_units(quote(m/s^2)) |
| 184 |
x2 <- as_units("m/s^2") |
| 185 |
x3 <- as_units("m s-2") # in product power form, i.e., implicit exponents = T |
| 186 |
x4 <- set_units(1, m/s^2) # by default, mode = "symbols" |
| 187 |
x5 <- set_units(1, "m/s^2", mode = "standard") |
| 188 |
x6 <- set_units(1, x1, mode = "standard") |
| 189 |
x7 <- set_units(1, units(x1), mode = "standard") |
| 190 |
x8 <- as_units("m") / as_units("s")^2 |
| 191 |
|
| 192 |
all_identical <- function(...) { |
| 193 |
l <- list(...) |
| 194 |
for(i in seq_along(l)[-1]) |
| 195 |
if(!identical(l[[1]], l[[i]])) |
| 196 |
return(FALSE) |
| 197 |
TRUE |
| 198 |
} |
| 199 |
all_identical(x1, x2, x3, x4, x5, x6, x7, x8) |
| 200 |
|
| 201 |
# Note, direct usage of these unit creation functions is typically not |
| 202 |
# necessary, since coercion is automatically done via as_units(). Again, |
| 203 |
# these are all equivalent ways to generate the same result. |
| 204 |
|
| 205 |
x1 <- x2 <- x3 <- x4 <- x5 <- x6 <- x7 <- x8 <- 1:4 |
| 206 |
units(x1) <- "m/s^2" |
| 207 |
units(x2) <- "m s-2" |
| 208 |
units(x3) <- quote(m/s^2) |
| 209 |
units(x4) <- make_units(m/s^2) |
| 210 |
units(x5) <- as_units(quote(m/s^2)) |
| 211 |
x6 <- set_units(x6, m/s^2) |
| 212 |
x7 <- set_units(x7, "m/s^2", mode = "standard") |
| 213 |
x8 <- set_units(x8, units(x1), mode = "standard") |
| 214 |
|
| 215 |
all_identical(x1, x2, x3, x4, x5, x6, x7, x8) |
| 216 |
|
| 217 |
|
| 218 |
# Both unit names or symbols can be used. By default, unit names are |
| 219 |
# automatically converted to unit symbols. |
| 220 |
make_units(degree_C) |
| 221 |
make_units(kilogram) |
| 222 |
make_units(ohm) |
| 223 |
|
| 224 |
## Arithmetic operations and units |
| 225 |
# conversion between unit objects that were defined as symbols and names will |
| 226 |
# work correctly, although unit simplification in printing may not always occur. |
| 227 |
x <- 500 * make_units(micrograms/liter) |
| 228 |
y <- set_units(200, ug/l) |
| 229 |
x + y |
| 230 |
x * y # numeric result is correct, but units not simplified completely |
| 231 |
|
| 232 |
# note, plural form of unit name accepted too ('liters' vs 'liter'), and |
| 233 |
# denominator simplification can be performed correctly |
| 234 |
x * set_units(5, liters) |
| 235 |
|
| 236 |
# unit conversion works too |
| 237 |
set_units(x, grams/gallon) |
| 238 |
|
| 239 |
## Creating custom, user defined units |
| 240 |
# For example, a microbiologist might work with counts of bacterial cells |
| 241 |
# make_units(cells/ml) # by default, throws an ERROR |
| 242 |
# First define the unit, then the newly defined unit is accepted. |
| 243 |
install_unit("cells") |
| 244 |
make_units(cells/ml) |
| 245 |
|
| 246 |
# Note that install_unit() adds support for defining relationships between |
| 247 |
# the newly created symbols or names and existing units. |
| 248 |
|
| 249 |
## set_units() |
| 250 |
# set_units is a pipe friendly version of `units<-`. |
| 251 |
if(requireNamespace("magrittr", quietly = TRUE)) { |
| 252 |
library(magrittr) |
| 253 |
1:5 \%>\% set_units(N/m^2) |
| 254 |
# first sets to m, then converts to km |
| 255 |
1:5 \%>\% set_units(m) \%>\% set_units(km) |
| 256 |
} |
| 257 |
|
| 258 |
# set_units has two modes of operation. By default, it operates with |
| 259 |
# bare symbols to define the units. |
| 260 |
set_units(1:5, m/s) |
| 261 |
|
| 262 |
# use `mode = "standard"` to use the value of supplied argument, rather than |
| 263 |
# the bare symbols of the expression. In this mode, set_units() can be |
| 264 |
# thought of as a simple alias for `units<-` that is pipe friendly. |
| 265 |
set_units(1:5, "m/s", mode = "standard") |
| 266 |
set_units(1:5, make_units(m/s), mode = "standard") |
| 267 |
|
| 268 |
# the mode of set_units() can be controlled via a global option |
| 269 |
# units_options(set_units_mode = "standard") |
| 270 |
|
| 271 |
# To remove units use |
| 272 |
units(x) <- NULL |
| 273 |
# or |
| 274 |
set_units(x, NULL) |
| 275 |
# or |
| 276 |
drop_units(y) |
| 277 |
s = Sys.time() |
| 278 |
d = s - (s+1) |
| 279 |
as_units(d) |
| 280 |
|
| 281 |
} |
| 282 |
\seealso{ |
| 283 |
\code{\link{install_unit}}, \code{\link{valid_udunits}} |
| 284 |
} |