Title: | Quantity Calculus for R Vectors |
---|---|
Description: | Integration of the 'units' and 'errors' packages for a complete quantity calculus system for R vectors, matrices and arrays, with automatic propagation, conversion, derivation and simplification of magnitudes and uncertainties. Documentation about 'units' and 'errors' is provided in the papers by Pebesma, Mailund & Hiebert (2016, <doi:10.32614/RJ-2016-061>) and by Ucar, Pebesma & Azcorra (2018, <doi:10.32614/RJ-2018-075>), included in those packages as vignettes; see 'citation("quantities")' for details. |
Authors: | Iñaki Ucar [aut, cph, cre] |
Maintainer: | Iñaki Ucar <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2.2 |
Built: | 2024-10-27 05:05:04 UTC |
Source: | https://github.com/r-quantities/quantities |
Support for painless automatic units and uncertainty propagation in numerical
operations. Both units and errors are integrated into a complete
quantity calculus system within the R language. R vectors, matrices and arrays
automatically propagate those attributes when you operate with quantities
objects.
Iñaki Ucar
Edzer Pebesma, Thomas Mailund and James Hiebert (2016). Measurement Units in R. The R Journal, 8(2), 486–494. doi:10.32614/RJ-2016-061
Iñaki Ucar, Edzer Pebesma and Arturo Azcorra (2018). Measurement Errors in R. The R Journal, 10(2), 549–557. doi:10.32614/RJ-2018-075
Useful links:
Report bugs at https://github.com/r-quantities/quantities/issues
S3 method for quantities
objects (see as.data.frame
).
## S3 method for class 'quantities' as.data.frame(x, ...)
## S3 method for class 'quantities' as.data.frame(x, ...)
x |
any R object. |
... |
additional arguments to be passed to or from methods. |
x <- set_quantities(1:3, m/s, 0.1) y <- set_quantities(4:6, m/s, 0.2) (z <- cbind(x, y)) as.data.frame(z)
x <- set_quantities(1:3, m/s, 0.1) y <- set_quantities(4:6, m/s, 0.2) (z <- cbind(x, y)) as.data.frame(z)
S3 method for quantities
objects (see as.list
).
## S3 method for class 'quantities' as.list(x, ...)
## S3 method for class 'quantities' as.list(x, ...)
x |
object to be coerced or tested. |
... |
objects, possibly named. |
x <- set_quantities(1:3, m/s, 0.1) as.list(x)
x <- set_quantities(1:3, m/s, 0.1) as.list(x)
S3 method for quantities
objects (see as.matrix
).
## S3 method for class 'quantities' as.matrix(x, ...)
## S3 method for class 'quantities' as.matrix(x, ...)
x |
an R object. |
... |
additional arguments to be passed to or from methods. |
as.matrix(set_quantities(1:3, m/s, 0.1))
as.matrix(set_quantities(1:3, m/s, 0.1))
S3 method for quantities
objects (see c
).
## S3 method for class 'quantities' c(...)
## S3 method for class 'quantities' c(...)
... |
objects to be concatenated. All |
c(set_quantities(1, m/s, 0.2), set_quantities(30, km/h, 0.1))
c(set_quantities(1, m/s, 0.2), set_quantities(30, km/h, 0.1))
S3 methods for quantities
objects (see cbind
).
## S3 method for class 'quantities' cbind(..., deparse.level = 1) ## S3 method for class 'quantities' rbind(..., deparse.level = 1)
## S3 method for class 'quantities' cbind(..., deparse.level = 1) ## S3 method for class 'quantities' 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_quantities(1, m/s, 0.1) y <- set_quantities(1:3, m/s, 0.2) z <- set_quantities(8:10, m/s, 0.1) (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
x <- set_quantities(1, m/s, 0.1) y <- set_quantities(1:3, m/s, 0.2) z <- set_quantities(8:10, m/s, 0.1) (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
quantities
ObjectsMethods to set or retrieve correlations or covariances between
quantities
objects.
## S3 replacement method for class 'quantities' correl(x, y) <- value ## S3 replacement method for class 'quantities' covar(x, y) <- value
## S3 replacement method for class 'quantities' correl(x, y) <- value ## S3 replacement method for class 'quantities' covar(x, y) <- value
x |
an object of class |
y |
an object of class |
value |
a compatible object of class |
x <- set_quantities(1:10, m/s, 0.1) y <- set_quantities(10:1, km/h, 0.2) correl(x, y) <- 0.1 # accepted correl(x, y) <- set_units(0.1) # recommended correl(x, y) covar(x, y)
x <- set_quantities(1:10, m/s, 0.1) y <- set_quantities(10:1, km/h, 0.2) correl(x, y) <- 0.1 # accepted correl(x, y) <- set_units(0.1) # recommended correl(x, y) covar(x, y)
S3 method for quantities
objects (see diff
).
## S3 method for class 'quantities' diff(x, lag = 1L, differences = 1L, ...)
## S3 method for class 'quantities' diff(x, lag = 1L, differences = 1L, ...)
x |
a numeric vector or matrix containing the values to be differenced. |
lag |
an integer indicating which lag to use. |
differences |
an integer indicating the order of the difference. |
... |
further arguments to be passed to or from methods. |
diff(set_quantities(1:10, m/s, 0.1), 2) diff(set_quantities(1:10, m/s, 0.1), 2, 2) x <- cumsum(cumsum(set_quantities(1:10, m/s, 0.1))) diff(x, lag = 2) diff(x, differences = 2)
diff(set_quantities(1:10, m/s, 0.1), 2) diff(set_quantities(1:10, m/s, 0.1), 2, 2) x <- cumsum(cumsum(set_quantities(1:10, m/s, 0.1))) diff(x, lag = 2) diff(x, differences = 2)
Drop Units and Errors
drop_quantities(x) ## S3 method for class 'quantities' drop_units(x) ## S3 method for class 'quantities' drop_errors(x) ## S3 method for class 'data.frame' drop_quantities(x)
drop_quantities(x) ## S3 method for class 'quantities' drop_units(x) ## S3 method for class 'quantities' drop_errors(x) ## S3 method for class 'data.frame' drop_quantities(x)
x |
a |
drop_quantities
is equivalent to quantities(x) <- NULL
or set_quantities(x, NULL, NULL)
. drop_units
is equivalent to
units(x) <- NULL
or set_units(x, NULL)
. drop_errors
is
equivalent to errors(x) <- NULL
or set_errors(x, NULL)
.
the numeric without any units
or errors
attributes,
while preserving other attributes like dimensions or other classes.
Set or retrieve measurement uncertainty to/from numeric vectors (extensions
to the errors package for quantities
and units
objects).
## S3 method for class 'units' errors(x) ## S3 method for class 'mixed_units' errors(x) ## S3 replacement method for class 'units' errors(x) <- value ## S3 replacement method for class 'mixed_units' errors(x) <- value ## S3 method for class 'units' set_errors(x, value = 0) ## S3 method for class 'mixed_units' set_errors(x, value = 0) ## S3 method for class 'units' errors_max(x) ## S3 method for class 'units' errors_min(x)
## S3 method for class 'units' errors(x) ## S3 method for class 'mixed_units' errors(x) ## S3 replacement method for class 'units' errors(x) <- value ## S3 replacement method for class 'mixed_units' errors(x) <- value ## S3 method for class 'units' set_errors(x, value = 0) ## S3 method for class 'mixed_units' set_errors(x, value = 0) ## S3 method for class 'units' errors_max(x) ## S3 method for class 'units' errors_min(x)
x |
a numeric object, or object of class |
value |
a numeric vector or |
For objects of class quantities
or units
, the
errors()
method returns a units
object that matches the units
of x
. Methods `errors<-`()
and set_errors()
assume that
the provided uncertainty (value
) has the same units as x
.
However, it is a best practice to provide a value
with explicit units.
In this way, uncertainty can be provided in different (but compatible) units,
and it will be automatically converted to the units of x
(see examples
below).
x <- set_units(1:5, m) errors(x) <- 0.01 # implicit units, same as x errors(x) errors(x) <- set_units(1, cm) # explicit units errors(x)
x <- set_units(1:5, m) errors(x) <- 0.01 # implicit units, same as x errors(x) errors(x) <- set_units(1, cm) # explicit units errors(x)
S3 operators to extract or replace parts of quantities
objects.
## S3 method for class 'quantities' x[...] ## S3 method for class 'quantities' x[[...]] ## S3 replacement method for class 'quantities' x[...] <- value ## S3 replacement method for class 'quantities' x[[...]] <- value
## S3 method for class 'quantities' x[...] ## S3 method for class 'quantities' x[[...]] ## S3 replacement method for class 'quantities' x[...] <- value ## S3 replacement method for class 'quantities' x[[...]] <- value
x |
object from which to extract element(s) or in which to replace element(s). |
... |
additional arguments to be passed to base methods
(see |
value |
typically an array-like R object of a similar class as |
x <- set_quantities(1:3, m/s, 0.1) y <- set_quantities(4:6, m/s, 0.2) (z <- rbind(x, y)) z[2, 2] z[2, 2] <- -1 errors(z[[1, 2]]) <- 0.8 # assumes same unit errors(z[[2, 2]]) <- set_units(80, cm/s) z[, 2]
x <- set_quantities(1:3, m/s, 0.1) y <- set_quantities(4:6, m/s, 0.2) (z <- rbind(x, y)) z[2, 2] z[2, 2] <- -1 errors(z[[1, 2]]) <- 0.8 # assumes same unit errors(z[[2, 2]]) <- set_units(80, cm/s) z[, 2]
Math
, Ops
and Summary
group generic methods for
quantities
objects (see groupGeneric
for a
comprehensive list of available methods).
## S3 method for class 'quantities' Math(x, ...) ## S3 method for class 'quantities' Ops(e1, e2) ## S3 method for class 'quantities' Summary(..., na.rm = FALSE)
## S3 method for class 'quantities' Math(x, ...) ## S3 method for class 'quantities' Ops(e1, e2) ## S3 method for class 'quantities' Summary(..., na.rm = FALSE)
x , e1 , e2
|
objects. |
... |
further arguments passed to methods. |
na.rm |
logical: should missing values be removed? |
See groupGeneric.errors
,
Ops.units
, Math.units
, for further details.
x <- set_quantities(1:3, m/s, 0.1) log(x) cumsum(x) cumprod(x) a <- set_quantities(1:3, m/s, 0.1) b <- set_quantities(1:3, m/s, 0.1) a + b a * b a / b a = set_quantities(1:5, m, 0.1) a %/% a a %/% set_quantities(2) set_quantities(1:5, m^2, 0.1) %/% set_quantities(2, m, 0.1) a %% a a %% set_quantities(2) c(min(x), max(x)) range(x) sum(x)
x <- set_quantities(1:3, m/s, 0.1) log(x) cumsum(x) cumprod(x) a <- set_quantities(1:3, m/s, 0.1) b <- set_quantities(1:3, m/s, 0.1) a + b a * b a / b a = set_quantities(1:5, m, 0.1) a %/% a a %/% set_quantities(2) set_quantities(1:5, m^2, 0.1) %/% set_quantities(2, m, 0.1) a %% a a %% set_quantities(2) c(min(x), max(x)) range(x) sum(x)
Functions to parse character vectors into quantities.
parse_quantities(x, decimal_mark) parse_units(x, decimal_mark) parse_errors(x, decimal_mark)
parse_quantities(x, decimal_mark) parse_units(x, decimal_mark) parse_errors(x, decimal_mark)
x |
a character vector to parse. |
decimal_mark |
the dot ( |
Each parse_*()
function returns an object of the corresponding
type, no matter what it is found. This means that, for parse_units
, if
errors are found, they are dropped with a warning. Similarly for
parse_errors
, if units are found, they are dropped with a warning.
On the other hand, parse_quantities
always returns a valid
quantities
object, even if no errors or units are found (then, zero
error and dimensionless units are applied).
A quantities
, units
or errors
object respectively.
parse_quantities("(1.6021766208 +/- .0000000098) e-19 C") parse_quantities("1.6021766208(98) e-19 C") parse_units("1.6021766208 e-19 C") parse_errors("1.6021766208(98) e-19") # quantities are converted to the first unit parse_quantities(c("12.34(2) m/s", "36.5(1) km/h")) # or kept as a list of mixed units parse_quantities(c("1.02(5) g", "2.51(0.01) V", "(3.23 +/- 0.12) m"))
parse_quantities("(1.6021766208 +/- .0000000098) e-19 C") parse_quantities("1.6021766208(98) e-19 C") parse_units("1.6021766208 e-19 C") parse_errors("1.6021766208(98) e-19") # quantities are converted to the first unit parse_quantities(c("12.34(2) m/s", "36.5(1) km/h")) # or kept as a list of mixed units parse_quantities(c("1.02(5) g", "2.51(0.01) V", "(3.23 +/- 0.12) m"))
Set or retrieve measurement units and uncertainty to/from numeric vectors.
quantities(x) quantities(x) <- value set_quantities(x, unit, errors = 0, ..., mode = units_options("set_units_mode"))
quantities(x) quantities(x) <- value set_quantities(x, unit, errors = 0, ..., mode = units_options("set_units_mode"))
x |
a numeric object, or object of class |
value |
a list of two components: an object of class |
unit |
a |
errors |
a numeric vector of length 1 or the same length as |
... |
passed on to other methods. |
mode |
if |
quantities
returns a named list with the units
and
errors
attributes.
`quantities<-`
sets the units and error values (and converts x
into an object of class quantities
). set_quantities
is a
pipe-friendly version of `quantities<-`
and returns an object of class
quantities
.
errors
, units
, groupGeneric.quantities
.
Extract.quantities
, c.quantities
,
rep.quantities
, cbind.quantities
.
as.data.frame.quantities
, as.matrix.quantities
,
t.quantities
.
x = 1:3 class(x) x quantities(x) <- list("m/s", 0.1) class(x) x (x <- set_quantities(x, m/s, seq(0.1, 0.3, 0.1)))
x = 1:3 class(x) x quantities(x) <- list("m/s", 0.1) class(x) x (x <- set_quantities(x, m/s, seq(0.1, 0.3, 0.1)))
S3 method for quantities
objects (see rep
).
## S3 method for class 'quantities' rep(x, ...)
## S3 method for class 'quantities' rep(x, ...)
x |
a vector (of any mode including a |
... |
further arguments to be passed to or from other methods. For the internal default method these can include:
|
rep(set_quantities(1, m/s, 0.1), 4)
rep(set_quantities(1, m/s, 0.1), 4)
S3 method for quantities
objects (see t
).
## S3 method for class 'quantities' t(x)
## S3 method for class 'quantities' t(x)
x |
a matrix or data frame, typically. |
a <- matrix(1:30, 5, 6) quantities(a) <- list("m/s", 1:30) t(a)
a <- matrix(1:30, 5, 6) quantities(a) <- list("m/s", 1:30) t(a)
Set or retrieve measurement units to/from numeric vectors and convert units
(extensions to the units package for quantities
and
errors
objects).
## S3 replacement method for class 'quantities' units(x) <- value ## S3 replacement method for class 'errors' units(x) <- value ## S3 method for class 'errors' set_units(x, value, ..., mode = units_options("set_units_mode")) ## S3 method for class 'quantities' mixed_units(x, values, ...) ## S3 method for class 'errors' mixed_units(x, values, ...)
## S3 replacement method for class 'quantities' units(x) <- value ## S3 replacement method for class 'errors' units(x) <- value ## S3 method for class 'errors' set_units(x, value, ..., mode = units_options("set_units_mode")) ## S3 method for class 'quantities' mixed_units(x, values, ...) ## S3 method for class 'errors' mixed_units(x, values, ...)
x |
a numeric object, or object of class |
value |
object of class |
... |
passed on to other methods. |
mode |
if |
values |
character vector with units encodings, or list with symbolic units of class |
For objects of class quantities
, methods `units<-`()
and set_units()
automatically convert the associated uncertainty to
the new unit (see examples below).
(x <- set_quantities(1:5, m, 0.01)) set_units(x, cm)
(x <- set_quantities(1:5, m, 0.01)) set_units(x, cm)