R has little support for physical measurement units. The exception is
formed by time differences: time differences objects of class
difftime
have a units
attribute that can be
modified:
t1 = Sys.time()
t2 = t1 + 3600
d = t2 - t1
class(d)
## [1] "difftime"
units(d)
## [1] "hours"
d
## Time difference of 1 hours
units(d) = "secs"
d
## Time difference of 3600 secs
We see here that the units
method is used to retrieve
and modify the unit of time differences.
The units
package generalizes this idea to other
physical units, building upon the udunits2 C
library. The udunits2
library provides the following
operations:
m/s
is a
valid physical unitm/s
and
km/h
are convertibleThe units
R package uses the udunits2 C
library to extend R with functionality for manipulating numeric vectors
that have physical measurement units associated with them, in a similar
way as difftime
objects behave.
We can set units to numerical values by set_units
:
library(units)
(a <- set_units(runif(10), m/s))
## Units: [m/s]
## [1] 0.07506398 0.15119641 0.97304159 0.46543809 0.71133555 0.96022532
## [7] 0.27296833 0.95485651 0.52155555 0.43468387
the result, e.g.
literally means “10 times 1 m divided by 1 s”. In writing, the “1” values are omitted, and the multiplication is implicit.
When conversion is meaningful, such as hours to seconds or meters to kilometers, conversion can be done explicitly by setting the units of a vector
Arithmetic operations verify units, and create new ones
a + a
## Units: [m/s]
## [1] 0.1501280 0.3023928 1.9460832 0.9308762 1.4226711 1.9204506 0.5459367
## [8] 1.9097130 1.0431111 0.8693677
a * a
## Units: [m^2/s^2]
## [1] 0.005634602 0.022860354 0.946809932 0.216632615 0.505998258 0.922032673
## [7] 0.074511708 0.911750954 0.272020196 0.188950069
a ^ 2
## Units: [m^2/s^2]
## [1] 0.005634602 0.022860354 0.946809932 0.216632615 0.505998258 0.922032673
## [7] 0.074511708 0.911750954 0.272020196 0.188950069
a ** -2
## Units: [s^2/m^2]
## [1] 177.474839 43.743855 1.056178 4.616110 1.976291 1.084560
## [7] 13.420710 1.096791 3.676198 5.292403
and convert to the units of the first argument if necessary:
a + b # m/s + km/h -> m/s
## Units: [m/s]
## [1] 0.1501280 0.3023928 1.9460832 0.9308762 1.4226711 1.9204506 0.5459367
## [8] 1.9097130 1.0431111 0.8693677
Currently, powers are only supported for integer powers, so using
a ** 2.5
would result in an error.
There are some basic simplification of units:
t <- make_units(s)
a * t
## Units: [m]
## [1] 0.07506398 0.15119641 0.97304159 0.46543809 0.71133555 0.96022532
## [7] 0.27296833 0.95485651 0.52155555 0.43468387
which also work when units need to be converted before they can be simplified:
t <- make_units(min)
a * t
## Units: [m]
## [1] 4.503839 9.071784 58.382495 27.926285 42.680133 57.613519 16.378100
## [8] 57.291391 31.293333 26.081032
Simplification to unit-less values gives the “1” as unit:
m <- make_units(m)
a * t / m
## Units: [1]
## [1] 4.503839 9.071784 58.382495 27.926285 42.680133 57.613519 16.378100
## [8] 57.291391 31.293333 26.081032
Allowed operations that require convertible units are +
,
-
, ==
, !=
, <
,
>
, <=
, >=
. Operations
that lead to new units are *
, /
, and the power
operations **
and ^
.
Mathematical operations allowed are: abs
,
sign
, floor
, ceiling
,
trunc
, round
, signif
,
log
, cumsum
, cummax
,
cummin
.
signif(a ** 2 / 3, 3)
## Units: [m^2/s^2]
## [1] 0.00188 0.00762 0.31600 0.07220 0.16900 0.30700 0.02480 0.30400 0.09070
## [10] 0.06300
cumsum(a)
## Units: [m/s]
## [1] 0.07506398 0.22626039 1.19930198 1.66474007 2.37607561 3.33630094
## [7] 3.60926926 4.56412577 5.08568133 5.52036520
log(a) # base defaults to exp(1)
## Units: [ln(re 1 m.s-1)]
## [1] -2.58941442 -1.88917557 -0.02732846 -0.76477619 -0.34061103 -0.04058731
## [7] -1.29839951 -0.04619420 -0.65093948 -0.83313624
log(a, base = 10)
## Units: [lg(re 1 m.s-1)]
## [1] -1.12456840 -0.82045853 -0.01186860 -0.33213808 -0.14792549 -0.01762684
## [7] -0.56388774 -0.02006189 -0.28269943 -0.36182647
log(a, base = 2)
## Units: [lb(re 1 m.s-1)]
## [1] -3.73573535 -2.72550423 -0.03942663 -1.10333882 -0.49139784 -0.05855511
## [7] -1.87319453 -0.06664415 -0.93910716 -1.20196153
Summary functions sum
, min
,
max
, and range
are allowed:
Following difftime
, printing behaves differently for
length-one vectors:
The usual subsetting rules work:
c(a,a)
## Units: [m/s]
## [1] 0.07506398 0.15119641 0.97304159 0.46543809 0.71133555 0.96022532
## [7] 0.27296833 0.95485651 0.52155555 0.43468387 0.07506398 0.15119641
## [13] 0.97304159 0.46543809 0.71133555 0.96022532 0.27296833 0.95485651
## [19] 0.52155555 0.43468387
concatenation converts to the units of the first argument, if necessary:
c(a,b) # m/s, km/h -> m/s
## Units: [m/s]
## [1] 0.07506398 0.15119641 0.97304159 0.46543809 0.71133555 0.96022532
## [7] 0.27296833 0.95485651 0.52155555 0.43468387 0.07506398 0.15119641
## [13] 0.97304159 0.46543809 0.71133555 0.96022532 0.27296833 0.95485651
## [19] 0.52155555 0.43468387
c(b,a) # km/h, m/s -> km/h
## Units: [km/h]
## [1] 0.2702303 0.5443071 3.5029497 1.6755771 2.5608080 3.4568112 0.9826860
## [8] 3.4374834 1.8776000 1.5648619 0.2702303 0.5443071 3.5029497 1.6755771
## [15] 2.5608080 3.4568112 0.9826860 3.4374834 1.8776000 1.5648619
difftime
From difftime
to units
:
vice versa:
matrix
objectsset_units(matrix(1:4,2,2), m/s)
## Units: [m/s]
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
set_units(matrix(1:4,2,2), m/s * m/s)
## Units: [m^2/s^2]
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
but
set_units(matrix(1:4,2,2), m/s) %*% set_units(4:3, m/s)
## Units: [m^2/s^2]
## [,1]
## [1,] 13
## [2,] 20
strips units.
data.frame
sunits in data.frame
objects are printed, but do not
appear in summary
:.
set.seed(131)
d <- data.frame(x = runif(4),
y = set_units(runif(4), s),
z = set_units(1:4, m/s))
d
## x y z
## 1 0.2064370 0.8463468 [s] 1 [m/s]
## 2 0.1249422 0.5292048 [s] 2 [m/s]
## 3 0.2932732 0.5186254 [s] 3 [m/s]
## 4 0.3757797 0.2378545 [s] 4 [m/s]
summary(d)
## x y z
## Min. :0.1249 Min. :0.2379 Min. :1.00
## 1st Qu.:0.1861 1st Qu.:0.4484 1st Qu.:1.75
## Median :0.2499 Median :0.5239 Median :2.50
## Mean :0.2501 Mean :0.5330 Mean :2.50
## 3rd Qu.:0.3139 3rd Qu.:0.6085 3rd Qu.:3.25
## Max. :0.3758 Max. :0.8463 Max. :4.00
d$yz = with(d, y * z)
d
## x y z yz
## 1 0.2064370 0.8463468 [s] 1 [m/s] 0.8463468 [m]
## 2 0.1249422 0.5292048 [s] 2 [m/s] 1.0584095 [m]
## 3 0.2932732 0.5186254 [s] 3 [m/s] 1.5558761 [m]
## 4 0.3757797 0.2378545 [s] 4 [m/s] 0.9514180 [m]
d[1, "yz"]
## 0.8463468 [m]
Units are often written in the form m2 s-1
, for square
meter per second. This can be defined as unit, and also parsed by
as_units
:
udunits understands such string, and can convert them
Printing units in this form is done by
Base scatter plots and histograms support automatic unit placement in
axis labels. In the following example we first convert to SI units.
(Unit in
needs a bit special treatment, because
in
is a reserved word in R.)
mar = par("mar") + c(0, .3, 0, 0)
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)
par(mar = mar)
plot(weight, displacement)
We can change grouping symbols from [ ]
into
( )
:
units_options(group = c("(", ")") ) # parenthesis instead of square brackets
par(mar = mar)
plot(weight, displacement)
We can also remove grouping symbols, increase space between variable name and unit by:
units_options(sep = c("~~~", "~"), group = c("", "")) # no brackets; extra space
par(mar = mar)
plot(weight, displacement)
More complex units can be plotted either with negative powers, or as
divisions, by modifying one of units
’s global options using
units_options
:
gallon = as_units("gallon")
consumption = mtcars$mpg * make_units(mi/gallon)
units(consumption) = make_units(km/l)
par(mar = mar)
plot(displacement, consumption) # division in consumption
units_options(negative_power = TRUE) # division becomes ^-1
plot(displacement, consumption) # division in consumption
As usual, units modify automatically in expressions:
units_options(negative_power = TRUE) # division becomes ^-1
par(mar = mar)
plot(displacement, consumption)