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:
Sys.time()
t1 = t1 + 3600
t2 = t2 - t1
d =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)
set_units(runif(10), m/s))
(a <-## Units: [m/s]
## [1] 0.08207800 0.50778951 0.51771894 0.35030482 0.94065616 0.97227437
## [7] 0.53968563 0.32321496 0.07407025 0.45797288
the result, e.g.
set_units(10, m/s)
## 10 [m/s]
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
a
b =units(b) <- make_units(km/h)
b## Units: [km/h]
## [1] 0.2954808 1.8280422 1.8637882 1.2610973 3.3863622 3.5001877 1.9428683
## [8] 1.1635739 0.2666529 1.6487024
Arithmetic operations verify units, and create new ones
+ a
a ## Units: [m/s]
## [1] 0.1641560 1.0155790 1.0354379 0.7006096 1.8813123 1.9445487 1.0793713
## [8] 0.6464299 0.1481405 0.9159458
* a
a ## Units: [m^2/s^2]
## [1] 0.006736798 0.257850182 0.268032901 0.122713464 0.884834019 0.945317452
## [7] 0.291260583 0.104467909 0.005486402 0.209739155
^ 2
a ## Units: [m^2/s^2]
## [1] 0.006736798 0.257850182 0.268032901 0.122713464 0.884834019 0.945317452
## [7] 0.291260583 0.104467909 0.005486402 0.209739155
** -2
a ## Units: [s^2/m^2]
## [1] 148.438470 3.878221 3.730885 8.149065 1.130155 1.057846
## [7] 3.433352 9.572318 182.268809 4.767827
and convert to the units of the first argument if necessary:
+ b # m/s + km/h -> m/s
a ## Units: [m/s]
## [1] 0.1641560 1.0155790 1.0354379 0.7006096 1.8813123 1.9445487 1.0793713
## [8] 0.6464299 0.1481405 0.9159458
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:
make_units(s)
t <-* t
a ## Units: [m]
## [1] 0.08207800 0.50778951 0.51771894 0.35030482 0.94065616 0.97227437
## [7] 0.53968563 0.32321496 0.07407025 0.45797288
which also work when units need to be converted before they can be simplified:
make_units(min)
t <-* t
a ## Units: [m]
## [1] 4.924680 30.467370 31.063136 21.018289 56.439370 58.336462 32.381138
## [8] 19.392898 4.444215 27.478373
Simplification to unit-less values gives the “1” as unit:
make_units(m)
m <-* t / m
a ## Units: [1]
## [1] 4.924680 30.467370 31.063136 21.018289 56.439370 58.336462 32.381138
## [8] 19.392898 4.444215 27.478373
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.00225 0.08600 0.08930 0.04090 0.29500 0.31500 0.09710 0.03480 0.00183
## [10] 0.06990
cumsum(a)
## Units: [m/s]
## [1] 0.0820780 0.5898675 1.1075864 1.4578913 2.3985474 3.3708218 3.9105074
## [8] 4.2337224 4.3077926 4.7657655
log(a) # base defaults to exp(1)
## Units: [(ln(re 1 m.s-1))]
## [1] -2.50008526 -0.67768828 -0.65832277 -1.04895160 -0.06117760 -0.02811724
## [7] -0.61676847 -1.12943767 -2.60274128 -0.78094532
log(a, base = 10)
## Units: [(lg(re 1 m.s-1))]
## [1] -1.08577323 -0.29431628 -0.28590595 -0.45555389 -0.02656909 -0.01221116
## [7] -0.26785914 -0.49050855 -1.13035618 -0.33916024
log(a, base = 2)
## Units: [(lb(re 1 m.s-1))]
## [1] -3.60686061 -0.97769752 -0.94975900 -1.51331728 -0.08826062 -0.04056460
## [7] -0.88980881 -1.62943413 -3.75496194 -1.12666594
Summary functions sum
, min
, max
, and range
are allowed:
sum(a)
## 4.765766 [m/s]
min(a)
## 0.07407025 [m/s]
max(a)
## 0.9722744 [m/s]
range(a)
## Units: [m/s]
## [1] 0.07407025 0.97227437
make_units(min(m/s, km/h)) # converts to first unit:
## 0.2777778 [m/s]
Following difftime
, printing behaves differently for length-one vectors:
a## Units: [m/s]
## [1] 0.08207800 0.50778951 0.51771894 0.35030482 0.94065616 0.97227437
## [7] 0.53968563 0.32321496 0.07407025 0.45797288
1]
a[## 0.082078 [m/s]
The usual subsetting rules work:
2:5]
a[## Units: [m/s]
## [1] 0.5077895 0.5177189 0.3503048 0.9406562
-(1:9)]
a[## 0.4579729 [m/s]
c(a,a)
## Units: [m/s]
## [1] 0.08207800 0.50778951 0.51771894 0.35030482 0.94065616 0.97227437
## [7] 0.53968563 0.32321496 0.07407025 0.45797288 0.08207800 0.50778951
## [13] 0.51771894 0.35030482 0.94065616 0.97227437 0.53968563 0.32321496
## [19] 0.07407025 0.45797288
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.08207800 0.50778951 0.51771894 0.35030482 0.94065616 0.97227437
## [7] 0.53968563 0.32321496 0.07407025 0.45797288 0.08207800 0.50778951
## [13] 0.51771894 0.35030482 0.94065616 0.97227437 0.53968563 0.32321496
## [19] 0.07407025 0.45797288
c(b,a) # km/h, m/s -> km/h
## Units: [km/h]
## [1] 0.2954808 1.8280422 1.8637882 1.2610973 3.3863622 3.5001877 1.9428683
## [8] 1.1635739 0.2666529 1.6487024 0.2954808 1.8280422 1.8637882 1.2610973
## [15] 3.3863622 3.5001877 1.9428683 1.1635739 0.2666529 1.6487024
difftime
From difftime
to units
:
Sys.time()
t1 = t1 + 3600
t2 = t2 - t1
d =du = as_units(d))
(## 1 [h]
vice versa:
dt = as_difftime(du))
(## Time difference of 1 hours
class(dt)
## [1] "difftime"
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)
## [,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)
data.frame(x = runif(4),
d <-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
$yz = with(d, y * z)
d
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]
1, "yz"]
d[## 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
:
x = 1:10 * as_units("m2 s-1"))
(## Units: [m^2/s]
## [1] 1 2 3 4 5 6 7 8 9 10
udunits understands such string, and can convert them
1:10 * make_units(m^2/s)
y =+ y
x ## Units: [m^2/s]
## [1] 2 4 6 8 10 12 14 16 18 20
Printing units in this form is done by
deparse_unit(x)
## [1] "m2 s-1"
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.)
par("mar") + c(0, .3, 0, 0)
mar = mtcars$disp * as_units("in")^3
displacement =units(displacement) = make_units(cm^3)
mtcars$wt * 1000 * make_units(lb)
weight =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
:
as_units("gallon")
gallon = mtcars$mpg * make_units(mi/gallon)
consumption =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)
plot(1/displacement, 1/consumption)