| 1 |
## ----echo=FALSE--------------------------------------------------------------- |
| 2 |
knitr::opts_chunk$set(collapse = TRUE, fig.asp = 0.7, fig.width = 7) |
| 3 |
|
| 4 |
## ----echo=FALSE--------------------------------------------------------------- |
| 5 |
units:::units_options(negative_power = FALSE) |
| 6 |
|
| 7 |
## ----------------------------------------------------------------------------- |
| 8 |
t1 = Sys.time() |
| 9 |
t2 = t1 + 3600 |
| 10 |
d = t2 - t1 |
| 11 |
class(d) |
| 12 |
units(d) |
| 13 |
d |
| 14 |
units(d) = "secs" |
| 15 |
d |
| 16 |
|
| 17 |
## ----------------------------------------------------------------------------- |
| 18 |
library(units) |
| 19 |
(a <- set_units(runif(10), m/s)) |
| 20 |
|
| 21 |
## ----------------------------------------------------------------------------- |
| 22 |
set_units(10, m/s) |
| 23 |
|
| 24 |
## ----------------------------------------------------------------------------- |
| 25 |
b = a |
| 26 |
units(b) <- make_units(km/h) |
| 27 |
b |
| 28 |
|
| 29 |
## ----------------------------------------------------------------------------- |
| 30 |
a + a |
| 31 |
a * a |
| 32 |
a ^ 2 |
| 33 |
a ** -2 |
| 34 |
|
| 35 |
## ----------------------------------------------------------------------------- |
| 36 |
a + b # m/s + km/h -> m/s |
| 37 |
|
| 38 |
## ----------------------------------------------------------------------------- |
| 39 |
t <- make_units(s) |
| 40 |
a * t |
| 41 |
|
| 42 |
## ----------------------------------------------------------------------------- |
| 43 |
t <- make_units(min) |
| 44 |
a * t |
| 45 |
|
| 46 |
## ----------------------------------------------------------------------------- |
| 47 |
m <- make_units(m) |
| 48 |
a * t / m |
| 49 |
|
| 50 |
## ----------------------------------------------------------------------------- |
| 51 |
signif(a ** 2 / 3, 3) |
| 52 |
cumsum(a) |
| 53 |
log(a) # base defaults to exp(1) |
| 54 |
log(a, base = 10) |
| 55 |
log(a, base = 2) |
| 56 |
|
| 57 |
## ----------------------------------------------------------------------------- |
| 58 |
sum(a) |
| 59 |
min(a) |
| 60 |
max(a) |
| 61 |
range(a) |
| 62 |
|
| 63 |
## ----------------------------------------------------------------------------- |
| 64 |
a |
| 65 |
a[1] |
| 66 |
|
| 67 |
## ----------------------------------------------------------------------------- |
| 68 |
a[2:5] |
| 69 |
a[-(1:9)] |
| 70 |
|
| 71 |
## ----------------------------------------------------------------------------- |
| 72 |
c(a,a) |
| 73 |
|
| 74 |
## ----------------------------------------------------------------------------- |
| 75 |
c(a,b) # m/s, km/h -> m/s |
| 76 |
c(b,a) # km/h, m/s -> km/h |
| 77 |
|
| 78 |
## ----------------------------------------------------------------------------- |
| 79 |
t1 = Sys.time() |
| 80 |
t2 = t1 + 3600 |
| 81 |
d = t2 - t1 |
| 82 |
(du = as_units(d)) |
| 83 |
|
| 84 |
## ----------------------------------------------------------------------------- |
| 85 |
(dt = as_difftime(du)) |
| 86 |
class(dt) |
| 87 |
|
| 88 |
## ----------------------------------------------------------------------------- |
| 89 |
set_units(matrix(1:4,2,2), m/s) |
| 90 |
set_units(matrix(1:4,2,2), m/s * m/s) |
| 91 |
|
| 92 |
## ----------------------------------------------------------------------------- |
| 93 |
set_units(matrix(1:4,2,2), m/s) %*% set_units(4:3, m/s) |
| 94 |
|
| 95 |
## ----------------------------------------------------------------------------- |
| 96 |
set.seed(131) |
| 97 |
d <- data.frame(x = runif(4), |
| 98 |
y = set_units(runif(4), s), |
| 99 |
z = set_units(1:4, m/s)) |
| 100 |
d |
| 101 |
summary(d) |
| 102 |
d$yz = with(d, y * z) |
| 103 |
d |
| 104 |
d[1, "yz"] |
| 105 |
|
| 106 |
## ----------------------------------------------------------------------------- |
| 107 |
(x = 1:10 * as_units("m2 s-1")) |
| 108 |
|
| 109 |
## ----------------------------------------------------------------------------- |
| 110 |
y = 1:10 * make_units(m^2/s) |
| 111 |
x + y |
| 112 |
|
| 113 |
## ----------------------------------------------------------------------------- |
| 114 |
deparse_unit(x) |
| 115 |
|
| 116 |
## ----------------------------------------------------------------------------- |
| 117 |
mar = par("mar") + c(0, .3, 0, 0) |
| 118 |
displacement = mtcars$disp * as_units("in")^3 |
| 119 |
units(displacement) = make_units(cm^3) |
| 120 |
weight = mtcars$wt * 1000 * make_units(lb) |
| 121 |
units(weight) = make_units(kg) |
| 122 |
par(mar = mar) |
| 123 |
plot(weight, displacement) |
| 124 |
|
| 125 |
## ----------------------------------------------------------------------------- |
| 126 |
units_options(group = c("(", ")") ) # parenthesis instead of square brackets |
| 127 |
par(mar = mar) |
| 128 |
plot(weight, displacement) |
| 129 |
|
| 130 |
## ----------------------------------------------------------------------------- |
| 131 |
units_options(sep = c("~~~", "~"), group = c("", "")) # no brackets; extra space |
| 132 |
par(mar = mar) |
| 133 |
plot(weight, displacement) |
| 134 |
|
| 135 |
## ----------------------------------------------------------------------------- |
| 136 |
gallon = as_units("gallon") |
| 137 |
consumption = mtcars$mpg * make_units(mi/gallon) |
| 138 |
units(consumption) = make_units(km/l) |
| 139 |
par(mar = mar) |
| 140 |
plot(displacement, consumption) # division in consumption |
| 141 |
units_options(negative_power = TRUE) # division becomes ^-1 |
| 142 |
plot(displacement, consumption) # division in consumption |
| 143 |
|
| 144 |
## ----------------------------------------------------------------------------- |
| 145 |
units_options(negative_power = TRUE) # division becomes ^-1 |
| 146 |
par(mar = mar) |
| 147 |
plot(displacement, consumption) |
| 148 |
plot(1/displacement, 1/consumption) |
| 149 |
|
| 150 |
## ----echo=FALSE--------------------------------------------------------------- |
| 151 |
units_options(negative_power = FALSE) # division becomes / |
| 152 |
|