File: units.R

package info (click to toggle)
r-cran-units 0.8-7%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,172 kB
  • sloc: xml: 2,437; cpp: 211; sh: 13; makefile: 2
file content (153 lines) | stat: -rw-r--r-- 4,727 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
## ----echo=FALSE---------------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, fig.asp = 0.7, fig.width = 7)

## ----echo=FALSE---------------------------------------------------------------
units:::units_options(negative_power = FALSE)

## -----------------------------------------------------------------------------
t1 = Sys.time() 
t2 = t1 + 3600 
d = t2 - t1
class(d)
units(d)
d
units(d) = "secs"
d

## -----------------------------------------------------------------------------
library(units)
(a <- set_units(runif(10),  m/s))

## -----------------------------------------------------------------------------
set_units(10, m/s)

## -----------------------------------------------------------------------------
b = a
units(b) <- make_units(km/h)
b

## -----------------------------------------------------------------------------
a + a
a * a
a ^ 2
a ** -2

## -----------------------------------------------------------------------------
a + b # m/s + km/h -> m/s

## -----------------------------------------------------------------------------
t <- make_units(s)
a * t

## -----------------------------------------------------------------------------
t <- make_units(min)
a * t

## -----------------------------------------------------------------------------
m <- make_units(m)
a * t / m

## -----------------------------------------------------------------------------
signif(a ** 2 / 3, 3)
cumsum(a)
log(a) # base defaults to exp(1)
log(a, base = 10)
log(a, base = 2)

## -----------------------------------------------------------------------------
sum(a)
min(a)
max(a)
range(a)
make_units(min(m/s, km/h)) # converts to first unit:

## -----------------------------------------------------------------------------
a
a[1]

## -----------------------------------------------------------------------------
a[2:5]
a[-(1:9)]

## -----------------------------------------------------------------------------
c(a,a)

## -----------------------------------------------------------------------------
c(a,b) # m/s, km/h -> m/s
c(b,a) # km/h, m/s -> km/h

## -----------------------------------------------------------------------------
t1 = Sys.time() 
t2 = t1 + 3600 
d = t2 - t1
(du = as_units(d))

## -----------------------------------------------------------------------------
(dt = as_difftime(du))
class(dt)

## -----------------------------------------------------------------------------
set_units(matrix(1:4,2,2), m/s)
set_units(matrix(1:4,2,2), m/s * m/s)

## -----------------------------------------------------------------------------
set_units(matrix(1:4,2,2), m/s) %*% set_units(4:3, m/s)

## -----------------------------------------------------------------------------
set.seed(131)
d <- data.frame(x = runif(4), 
                    y = set_units(runif(4), s), 
                    z = set_units(1:4, m/s))
d
summary(d)
d$yz = with(d, y * z)
d
d[1, "yz"]

## -----------------------------------------------------------------------------
(x = 1:10 * as_units("m2 s-1"))

## -----------------------------------------------------------------------------
y = 1:10 * make_units(m^2/s)
x + y

## -----------------------------------------------------------------------------
deparse_unit(x)

## -----------------------------------------------------------------------------
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)

## -----------------------------------------------------------------------------
units_options(group = c("(", ")") )  # parenthesis instead of square brackets
par(mar = mar)
plot(weight, displacement)

## -----------------------------------------------------------------------------
units_options(sep = c("~~~", "~"), group = c("", ""))  # no brackets; extra space
par(mar = mar)
plot(weight, displacement)

## -----------------------------------------------------------------------------
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 consumption
units_options(negative_power = TRUE) # division becomes ^-1
plot(displacement, consumption) # division in consumption

## -----------------------------------------------------------------------------
units_options(negative_power = TRUE) # division becomes ^-1
par(mar = mar)
plot(displacement, consumption)
plot(1/displacement, 1/consumption)

## ----echo=FALSE---------------------------------------------------------------
units_options(negative_power = FALSE) # division becomes /