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 secsWe 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.7372013 0.3390005 0.7452676 0.9311327 0.4083598 0.4076314 0.8607311
## [8] 0.4954463 0.8818099 0.6170621the 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.4744026 0.6780011 1.4905352 1.8622653 0.8167196 0.8152628 1.7214622
## [8] 0.9908925 1.7636197 1.2341242
a * a
## Units: [m^2/s^2]
## [1] 0.5434658 0.1149214 0.5554238 0.8670081 0.1667577 0.1661633 0.7408580
## [8] 0.2454670 0.7775886 0.3807657
a ^ 2
## Units: [m^2/s^2]
## [1] 0.5434658 0.1149214 0.5554238 0.8670081 0.1667577 0.1661633 0.7408580
## [8] 0.2454670 0.7775886 0.3807657
a ** -2
## Units: [s^2/m^2]
## [1] 1.840042 8.701602 1.800427 1.153392 5.996723 6.018175 1.349786 4.073867
## [9] 1.286027 2.626287and convert to the units of the first argument if necessary:
a + b # m/s + km/h -> m/s
## Units: [m/s]
## [1] 1.4744026 0.6780011 1.4905352 1.8622653 0.8167196 0.8152628 1.7214622
## [8] 0.9908925 1.7636197 1.2341242Currently, 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.7372013 0.3390005 0.7452676 0.9311327 0.4083598 0.4076314 0.8607311
## [8] 0.4954463 0.8818099 0.6170621which also work when units need to be converted before they can be simplified:
t <- make_units(min)
a * t
## Units: [m]
## [1] 44.23208 20.34003 44.71606 55.86796 24.50159 24.45788 51.64387 29.72678
## [9] 52.90859 37.02373Simplification to unit-less values gives the “1” as unit:
m <- make_units(m)
a * t / m
## Units: [1]
## [1] 44.23208 20.34003 44.71606 55.86796 24.50159 24.45788 51.64387 29.72678
## [9] 52.90859 37.02373Allowed 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.1810 0.0383 0.1850 0.2890 0.0556 0.0554 0.2470 0.0818 0.2590 0.1270
cumsum(a)
## Units: [m/s]
## [1] 0.7372013 1.0762018 1.8214695 2.7526021 3.1609619 3.5685933 4.4293244
## [8] 4.9247707 5.8065805 6.4236427
log(a) # base defaults to exp(1)
## Units: [ln(re 1 m.s-1)]
## [1] -0.3048943 -1.0817536 -0.2940119 -0.0713535 -0.8956066 -0.8973920
## [7] -0.1499731 -0.7022964 -0.1257788 -0.4827856
log(a, base = 10)
## Units: [lg(re 1 m.s-1)]
## [1] -0.13241391 -0.46979960 -0.12768776 -0.03098843 -0.38895700 -0.38973240
## [7] -0.06513251 -0.30500345 -0.05462505 -0.20967111
log(a, base = 2)
## Units: [lb(re 1 m.s-1)]
## [1] -0.4398695 -1.5606405 -0.4241695 -0.1029413 -1.2920872 -1.2946630
## [7] -0.2163655 -1.0131995 -0.1814605 -0.6965124Summary 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.7372013 0.3390005 0.7452676 0.9311327 0.4083598 0.4076314 0.8607311
## [8] 0.4954463 0.8818099 0.6170621 0.7372013 0.3390005 0.7452676 0.9311327
## [15] 0.4083598 0.4076314 0.8607311 0.4954463 0.8818099 0.6170621concatenation converts to the units of the first argument, if necessary:
c(a,b) # m/s, km/h -> m/s
## Units: [m/s]
## [1] 0.7372013 0.3390005 0.7452676 0.9311327 0.4083598 0.4076314 0.8607311
## [8] 0.4954463 0.8818099 0.6170621 0.7372013 0.3390005 0.7452676 0.9311327
## [15] 0.4083598 0.4076314 0.8607311 0.4954463 0.8818099 0.6170621
c(b,a) # km/h, m/s -> km/h
## Units: [km/h]
## [1] 2.653925 1.220402 2.682963 3.352078 1.470095 1.467473 3.098632 1.783607
## [9] 3.174515 2.221424 2.653925 1.220402 2.682963 3.352078 1.470095 1.467473
## [17] 3.098632 1.783607 3.174515 2.221424difftimeFrom 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 4but
strips units.
data.framesunits 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 consumptionunits_options(negative_power = TRUE) # division becomes ^-1
plot(displacement, consumption) # division in consumptionAs usual, units modify automatically in expressions:
units_options(negative_power = TRUE) # division becomes ^-1
par(mar = mar)
plot(displacement, consumption)