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.990790724 0.225814089 0.065251844 0.008358879 0.700864773 0.917250937
## [7] 0.106108921 0.468381332 0.540414698 0.353407024
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] 1.98158145 0.45162818 0.13050369 0.01671776 1.40172955 1.83450187
## [7] 0.21221784 0.93676266 1.08082940 0.70681405
a * a
## Units: [m^2/s^2]
## [1] 9.816663e-01 5.099200e-02 4.257803e-03 6.987086e-05 4.912114e-01
## [6] 8.413493e-01 1.125910e-02 2.193811e-01 2.920480e-01 1.248965e-01
a ^ 2
## Units: [m^2/s^2]
## [1] 9.816663e-01 5.099200e-02 4.257803e-03 6.987086e-05 4.912114e-01
## [6] 8.413493e-01 1.125910e-02 2.193811e-01 2.920480e-01 1.248965e-01
a ** -2
## Units: [s^2/m^2]
## [1] 1.018676 19.610918 234.862903 14312.118430 2.035783
## [6] 1.188567 88.817021 4.558278 3.424094 8.006628
and convert to the units of the first argument if necessary:
a + b # m/s + km/h -> m/s
## Units: [m/s]
## [1] 1.98158145 0.45162818 0.13050369 0.01671776 1.40172955 1.83450187
## [7] 0.21221784 0.93676266 1.08082940 0.70681405
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.990790724 0.225814089 0.065251844 0.008358879 0.700864773 0.917250937
## [7] 0.106108921 0.468381332 0.540414698 0.353407024
which also work when units need to be converted before they can be simplified:
t <- make_units(min)
a * t
## Units: [m]
## [1] 59.4474434 13.5488453 3.9151106 0.5015327 42.0518864 55.0350562
## [7] 6.3665353 28.1028799 32.4248819 21.2044215
Simplification to unit-less values gives the “1” as unit:
m <- make_units(m)
a * t / m
## Units: [1]
## [1] 59.4474434 13.5488453 3.9151106 0.5015327 42.0518864 55.0350562
## [7] 6.3665353 28.1028799 32.4248819 21.2044215
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] 3.27e-01 1.70e-02 1.42e-03 2.33e-05 1.64e-01 2.80e-01 3.75e-03 7.31e-02
## [9] 9.73e-02 4.16e-02
cumsum(a)
## Units: [m/s]
## [1] 0.9907907 1.2166048 1.2818567 1.2902155 1.9910803 2.9083312 3.0144402
## [8] 3.4828215 4.0232362 4.3766432
log(a) # base defaults to exp(1)
## Units: [(ln(re 1 m.s-1))]
## [1] -0.009251944 -1.488043235 -2.729500976 -4.784430950 -0.355440316
## [6] -0.086374195 -2.243289155 -0.758472503 -0.615418475 -1.040134843
log(a, base = 10)
## Units: [(lg(re 1 m.s-1))]
## [1] -0.004018068 -0.646248966 -1.185407212 -2.077851961 -0.154365768
## [6] -0.037511836 -0.974248101 -0.329400423 -0.267272848 -0.451724823
log(a, base = 2)
## Units: [(lb(re 1 m.s-1))]
## [1] -0.01334773 -2.14679260 -3.93783752 -6.90247481 -0.51279198 -0.12461162
## [7] -3.23638214 -1.09424452 -0.88786118 -1.50059738
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.990790724 0.225814089 0.065251844 0.008358879 0.700864773 0.917250937
## [7] 0.106108921 0.468381332 0.540414698 0.353407024 0.990790724 0.225814089
## [13] 0.065251844 0.008358879 0.700864773 0.917250937 0.106108921 0.468381332
## [19] 0.540414698 0.353407024
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.990790724 0.225814089 0.065251844 0.008358879 0.700864773 0.917250937
## [7] 0.106108921 0.468381332 0.540414698 0.353407024 0.990790724 0.225814089
## [13] 0.065251844 0.008358879 0.700864773 0.917250937 0.106108921 0.468381332
## [19] 0.540414698 0.353407024
c(b,a) # km/h, m/s -> km/h
## Units: [km/h]
## [1] 3.56684660 0.81293072 0.23490664 0.03009196 2.52311318 3.30210337
## [7] 0.38199212 1.68617279 1.94549291 1.27226529 3.56684660 0.81293072
## [13] 0.23490664 0.03009196 2.52311318 3.30210337 0.38199212 1.68617279
## [19] 1.94549291 1.27226529
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
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: