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.08685376 0.61245589 0.05604669 0.54152777 0.97808968 0.81963022
## [7] 0.77027959 0.45519471 0.97061713 0.37958466the 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.1737075 1.2249118 0.1120934 1.0830555 1.9561794 1.6392604 1.5405592
## [8] 0.9103894 1.9412343 0.7591693
a * a
## Units: [m^2/s^2]
## [1] 0.007543576 0.375102216 0.003141231 0.293252322 0.956659424 0.671793690
## [7] 0.593330643 0.207202225 0.942097608 0.144084512
a ^ 2
## Units: [m^2/s^2]
## [1] 0.007543576 0.375102216 0.003141231 0.293252322 0.956659424 0.671793690
## [7] 0.593330643 0.207202225 0.942097608 0.144084512
a ** -2
## Units: [s^2/m^2]
## [1] 132.563129 2.665940 318.346512 3.410033 1.045304 1.488552
## [7] 1.685401 4.826203 1.061461 6.940371and convert to the units of the first argument if necessary:
a + b # m/s + km/h -> m/s
## Units: [m/s]
## [1] 0.1737075 1.2249118 0.1120934 1.0830555 1.9561794 1.6392604 1.5405592
## [8] 0.9103894 1.9412343 0.7591693Currently, 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.08685376 0.61245589 0.05604669 0.54152777 0.97808968 0.81963022
## [7] 0.77027959 0.45519471 0.97061713 0.37958466which also work when units need to be converted before they can be simplified:
t <- make_units(min)
a * t
## Units: [m]
## [1] 5.211226 36.747353 3.362801 32.491666 58.685381 49.177813 46.216775
## [8] 27.311683 58.237028 22.775079Simplification to unit-less values gives the “1” as unit:
m <- make_units(m)
a * t / m
## Units: [1]
## [1] 5.211226 36.747353 3.362801 32.491666 58.685381 49.177813 46.216775
## [8] 27.311683 58.237028 22.775079Allowed 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.00251 0.12500 0.00105 0.09780 0.31900 0.22400 0.19800 0.06910 0.31400
## [10] 0.04800
cumsum(a)
## Units: [m/s]
## [1] 0.08685376 0.69930965 0.75535634 1.29688410 2.27497379 3.09460400
## [7] 3.86488359 4.32007830 5.29069543 5.67028008
log(a) # base defaults to exp(1)
## Units: [ln(re 1 m.s-1)]
## [1] -2.44352949 -0.49027836 -2.88157023 -0.61336094 -0.02215391 -0.19890200
## [7] -0.26100173 -0.78703002 -0.02982320 -0.96867763
log(a, base = 10)
## Units: [lg(re 1 m.s-1)]
## [1] -1.061211373 -0.212925185 -1.251450048 -0.266379270 -0.009621323
## [6] -0.086382040 -0.113351611 -0.341802793 -0.012952049 -0.420691351
log(a, base = 2)
## Units: [lb(re 1 m.s-1)]
## [1] -3.52526787 -0.70732215 -4.15722707 -0.88489278 -0.03196134 -0.28695493
## [7] -0.37654590 -1.13544430 -0.04302578 -1.39750642Summary 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.08685376 0.61245589 0.05604669 0.54152777 0.97808968 0.81963022
## [7] 0.77027959 0.45519471 0.97061713 0.37958466 0.08685376 0.61245589
## [13] 0.05604669 0.54152777 0.97808968 0.81963022 0.77027959 0.45519471
## [19] 0.97061713 0.37958466concatenation converts to the units of the first argument, if necessary:
c(a,b) # m/s, km/h -> m/s
## Units: [m/s]
## [1] 0.08685376 0.61245589 0.05604669 0.54152777 0.97808968 0.81963022
## [7] 0.77027959 0.45519471 0.97061713 0.37958466 0.08685376 0.61245589
## [13] 0.05604669 0.54152777 0.97808968 0.81963022 0.77027959 0.45519471
## [19] 0.97061713 0.37958466
c(b,a) # km/h, m/s -> km/h
## Units: [km/h]
## [1] 0.3126735 2.2048412 0.2017681 1.9495000 3.5211229 2.9506688 2.7730065
## [8] 1.6387010 3.4942217 1.3665048 0.3126735 2.2048412 0.2017681 1.9495000
## [15] 3.5211229 2.9506688 2.7730065 1.6387010 3.4942217 1.3665048difftimeFrom 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
set_units(matrix(1:4,2,2), m/s) %*% set_units(4:3, m/s)
## Units: [m^2/s^2]
## [,1]
## [1,] 13
## [2,] 20strips 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)