Debian Codegraph @

inst/doc/measurement_units_in_R.R

raw
1 ## ----echo=FALSE---------------------------------------------------------------
2 knitr::opts_chunk$set(collapse = TRUE, fig.asp = 0.7, fig.width = 7)
3
4 ## -----------------------------------------------------------------------------
5 temp_data = subset(read.table("647_Global_Temperature_Data_File.txt",
6 header=TRUE)[1:2], Year >= 1960)
7 temp_data$date = as.Date(paste0(temp_data$Year, "-01-01"))
8 temp_data$time = as.POSIXct(temp_data$date)
9 Sys.setenv(TZ="UTC")
10 head(temp_data, 3)
11 year_duration = diff(temp_data$date)
12 mean(year_duration)
13
14 ## -----------------------------------------------------------------------------
15 year_duration %*% rep(1, length(year_duration)) / length(year_duration)
16
17 ## -----------------------------------------------------------------------------
18 coef(lm(Annual_Mean ~ date, temp_data))
19 coef(lm(Annual_Mean ~ time, temp_data))
20
21 ## ----eval=requireNamespace("measurements", quietly=TRUE)----------------------
22 library(measurements)
23 conv_unit(2.54, "cm", "inch")
24 conv_unit(c("101 44.32","3 19.453"), "deg_dec_min", "deg_min_sec")
25 conv_unit(10, "cm_per_sec", "km_per_day")
26
27 ## ----eval=requireNamespace("measurements", quietly=TRUE)----------------------
28 names(conv_unit_options)
29 conv_unit_options$volume
30
31 ## ----eval=requireNamespace("measurements", quietly=TRUE)----------------------
32 conv_dim(x = 100, x_unit = "m", trans = 3, trans_unit = "ft_per_sec", y_unit = "min")
33
34 ## ----eval=requireNamespace("NISTunits", quietly=TRUE)-------------------------
35 library(NISTunits)
36 NISTwattPerSqrMeterTOwattPerSqrInch(1:5)
37
38 ## -----------------------------------------------------------------------------
39 m100_a = paste(rep("m", 100), collapse = "*")
40 dm100_b = "dm^100"
41 units::ud_are_convertible(m100_a, dm100_b)
42
43 ## -----------------------------------------------------------------------------
44 library(units)
45 x = set_units(1:5, m/s)
46 str(x)
47
48 ## -----------------------------------------------------------------------------
49 set_units(1:3, m/s^2)
50
51 ## -----------------------------------------------------------------------------
52 x = set_units(1:3, m/s)
53 x + 2 * x
54
55 ## -----------------------------------------------------------------------------
56 (x = set_units(x, cm/s))
57 as.numeric(x)
58
59 ## -----------------------------------------------------------------------------
60 y = set_units(1:3, km/h)
61 x + y
62 y + x
63 x == y
64 c(y, x)
65
66 ## -----------------------------------------------------------------------------
67 x * y
68 x^3
69
70 ## -----------------------------------------------------------------------------
71 e = try(z <- x + x * y)
72 attr(e, "condition")[[1]]
73
74 ## -----------------------------------------------------------------------------
75 methods(class = "units")
76
77 ## -----------------------------------------------------------------------------
78 a = set_units(1:10, m/s)
79 b = set_units(1:10, h)
80 a * b
81 ustr1 = paste(rep("m", 101), collapse = "*")
82 ustr2 = "dm^100"
83 as_units(ustr1) / as_units(ustr2)
84
85 ## -----------------------------------------------------------------------------
86 set_units(1, m^5/s^4)
87
88 ## -----------------------------------------------------------------------------
89 as_units("m2 s-1")
90 deparse_unit(set_units(1, m^2*s^-1))
91
92 ## -----------------------------------------------------------------------------
93 library(units)
94 units_options(negative_power = TRUE)
95 # initialize variables with units:
96 mtcars$consumption = set_units(mtcars$mpg, mi/gallon)
97 # "in" is also a reserved R keyword, and so needs back-quotes:
98 mtcars$displacement = set_units(mtcars$disp, `in`^3)
99 # convert to SI:
100 mtcars$consumption = set_units(mtcars$consumption, km/l)
101 mtcars$displacement = set_units(mtcars$displacement, cm^3)
102 par(mar = par("mar") + c(0, .3, 0, 0))
103 with(mtcars, plot(1/displacement, 1/consumption))
104
105 ## ----eval=requireNamespace("ggplot2", quietly=TRUE)---------------------------
106 library(ggplot2)
107 ggplot(mtcars) + geom_point(aes(x = 1/displacement, y = 1/consumption))
108
109 ## -----------------------------------------------------------------------------
110 (dt = diff(Sys.time() + c(0, 1, 1+60, 1+60+3600))) # class difftime
111 (dt.u = as_units(dt))
112 identical(as_difftime(dt.u), dt)
113
114 ## -----------------------------------------------------------------------------
115 (t1 <- as_units(as.POSIXct("2017-08-20 17:03:00")))
116 (t2 <- as_units(as.POSIXct("2017-08-20 17:03:00"), "hours since 2017-08-20"))
117 (d1 <- as_units(as.Date("2017-08-20")))
118 as.POSIXct(t1)
119 as.Date(d1)
120