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 R package, which in turn is build 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 R package udunits2
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.
Existing units are resolved from a database in the units
package, called ud_units
. We can see the first three elements of it by
library(units)
ud_units[1:3]
## $m
## 1 m
##
## $kg
## 1 kg
##
## $s
## 1 s
We can set units to numerical values by set_units
:
(a <- set_units(runif(10), m/s))
## Units: m/s
## [1] 0.1848452 0.7334908 0.9317889 0.6658218 0.1575686 0.8430491 0.5489514
## [8] 0.1195950 0.5411970 0.7763862
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.
The units
package comes with a list of over 3000 predefined units,
length(ud_units)
## [1] 3225
We can retrieve a single unit from the ud_units
database by
with(ud_units, km/h)
## 1 km/h
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
b = a
units(b) <- with(ud_units, km/h)
b
## Units: km/h
## [1] 0.6654425 2.6405669 3.3544400 2.3969585 0.5672469 3.0349768 1.9762249
## [8] 0.4305419 1.9483093 2.7949902
Arithmetic operations verify units, and create new ones
a + a
## Units: m/s
## [1] 0.3696903 1.4669816 1.8635778 1.3316436 0.3151371 1.6860982 1.0979027
## [8] 0.2391899 1.0823941 1.5527724
a * a
## Units: m^2/s^2
## [1] 0.03416773 0.53800877 0.86823051 0.44331868 0.02482785 0.71073182
## [7] 0.30134760 0.01430296 0.29289423 0.60277550
a ^ 2
## Units: m^2/s^2
## [1] 0.03416773 0.53800877 0.86823051 0.44331868 0.02482785 0.71073182
## [7] 0.30134760 0.01430296 0.29289423 0.60277550
a ** -2
## Units: s^2/m^2
## [1] 29.267382 1.858706 1.151768 2.255714 40.277343 1.407000 3.318427
## [8] 69.915615 3.414202 1.658992
and convert to the units of the first argument if necessary:
a + b # m/s + km/h -> m/s
## Units: m/s
## [1] 0.3696903 1.4669816 1.8635778 1.3316436 0.3151371 1.6860982 1.0979027
## [8] 0.2391899 1.0823941 1.5527724
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 <- with(ud_units, s)
a * t
## Units: m
## [1] 0.1848452 0.7334908 0.9317889 0.6658218 0.1575686 0.8430491 0.5489514
## [8] 0.1195950 0.5411970 0.7763862
which also work when units need to be converted before they can be simplified:
t <- with(ud_units, min)
a * t
## Units: m
## [1] 11.090709 44.009449 55.907333 39.949309 9.454114 50.582947 32.937082
## [8] 7.175698 32.471822 46.583171
Simplification to unit-less values gives the “1” as unit:
m <- with(ud_units, m)
a * t / m
## Units: 1
## [1] 11.090709 44.009449 55.907333 39.949309 9.454114 50.582947 32.937082
## [8] 7.175698 32.471822 46.583171
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.01140 0.17900 0.28900 0.14800 0.00828 0.23700 0.10000 0.00477
## [9] 0.09760 0.20100
cumsum(a)
## Units: m/s
## [1] 0.1848452 0.9183360 1.8501248 2.5159466 2.6735152 3.5165643 4.0655157
## [8] 4.1851107 4.7263077 5.5026939
log(a) # base defaults to exp(1)
## Units: ln(m/s)
## [1] -1.68823683 -0.30994021 -0.07064902 -0.40673320 -1.84789455
## [6] -0.17073005 -0.59974544 -2.12364451 -0.61397186 -0.25310523
log(a, base = 10)
## Units: lg(m/s)
## [1] -0.73319194 -0.13460532 -0.03068248 -0.17664198 -0.80253041
## [6] -0.07414712 -0.26046613 -0.92228709 -0.26664459 -0.10992220
log(a, base = 2)
## Units: lb(m/s)
## [1] -2.4356109 -0.4471492 -0.1019250 -0.5867920 -2.6659483 -0.2463114
## [7] -0.8652498 -3.0637714 -0.8857742 -0.3651537
Summary functions sum
, min
, max
, and range
are allowed:
sum(a)
## 5.502694 m/s
min(a)
## 0.119595 m/s
max(a)
## 0.9317889 m/s
range(a)
## Units: m/s
## [1] 0.1195950 0.9317889
with(ud_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.1848452 0.7334908 0.9317889 0.6658218 0.1575686 0.8430491 0.5489514
## [8] 0.1195950 0.5411970 0.7763862
a[1]
## 0.1848452 m/s
The usual subsetting rules work:
a[2:5]
## Units: m/s
## [1] 0.7334908 0.9317889 0.6658218 0.1575686
a[-(1:9)]
## 0.7763862 m/s
c(a,a)
## Units: m/s
## [1] 0.1848452 0.7334908 0.9317889 0.6658218 0.1575686 0.8430491 0.5489514
## [8] 0.1195950 0.5411970 0.7763862 0.1848452 0.7334908 0.9317889 0.6658218
## [15] 0.1575686 0.8430491 0.5489514 0.1195950 0.5411970 0.7763862
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.1848452 0.7334908 0.9317889 0.6658218 0.1575686 0.8430491 0.5489514
## [8] 0.1195950 0.5411970 0.7763862 0.1848452 0.7334908 0.9317889 0.6658218
## [15] 0.1575686 0.8430491 0.5489514 0.1195950 0.5411970 0.7763862
c(b,a) # km/h, m/s -> km/h
## Units: km/h
## [1] 0.6654425 2.6405669 3.3544400 2.3969585 0.5672469 3.0349768 1.9762249
## [8] 0.4305419 1.9483093 2.7949902 0.6654425 2.6405669 3.3544400 2.3969585
## [15] 0.5672469 3.0349768 1.9762249 0.4305419 1.9483093 2.7949902
difftime
From difftime
to units
:
t1 = Sys.time()
t2 = t1 + 3600
d = t2 - t1
(du = as.units(d))
## 1 h
vice versa:
(dt = as.dt(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 * 4 * m/s)
## Units: m^2/s^2
## [,1] [,2]
## [1,] 4 12
## [2,] 8 16
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)
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, but is not interpreted by R:
(x = 1:10 * make_unit("m2 s-1"))
## Units: (m2 s-1)
## [1] 1 2 3 4 5 6 7 8 9 10
udunits understands such string, and can convert them
y = 1:10 * with(ud_units, m^2/s)
x + y
## Units: (m2 s-1)
## [1] 2 4 6 8 10 12 14 16 18 20
but R cannot simplify them:
x/y
## Units: (m2 s-1)*s/m^2
## [1] 1 1 1 1 1 1 1 1 1 1
Instead, we can tell R to parse such a string, which then allows simplification:
(z = 1:10 * parse_unit("m2 s-1"))
## Units: m^2/s
## [1] 1 2 3 4 5 6 7 8 9 10
z + y
## Units: m^2/s
## [1] 2 4 6 8 10 12 14 16 18 20
z / y
## Units: 1
## [1] 1 1 1 1 1 1 1 1 1 1
Printing units in this form is done by
as_cf(z)
## [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.)
mar = par("mar") + c(0, .3, 0, 0)
displacement = mtcars$disp * ud_units[["in"]]^3
units(displacement) = with(ud_units, cm^3)
weight = mtcars$wt * 1000 * with(ud_units, lb)
units(weight) = with(ud_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 = make_unit("gallon")
consumption = mtcars$mpg * with(ud_units, mi/gallon)
units(consumption) = with(ud_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)