library(qwraps2)
packageVersion("qwraps2")
## [1] '0.5.2'
The base R package datasets provides the mtcars data set. The information in mtcars is the fuel consumption and automobile characteristics of 32 automobiles as reported in the March, April, June and July 1974 issues of Motor Trend magazine (Hocking 1976).
That dataset is modified and extended to provide support for examples within the qwraps2 package documentation. This vignette documents the construction of mtcars2.
Starting with the original mtcars:
<- mtcars
mtcars2 str(mtcars2)
## 'data.frame': 32 obs. of 11 variables:
## $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
## $ disp: num 160 160 108 258 360 ...
## $ hp : num 110 110 93 110 175 105 245 62 95 123 ...
## $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
## $ wt : num 2.62 2.88 2.32 3.21 3.44 ...
## $ qsec: num 16.5 17 18.6 19.4 17 ...
## $ vs : num 0 0 1 1 0 1 0 1 1 1 ...
## $ am : num 1 1 1 0 0 0 0 0 0 0 ...
## $ gear: num 4 4 4 3 3 3 3 4 4 4 ...
## $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
The cyl column provides the number of cylinders for in the engine of the automobiles. We will use two additional versions of this information, one as character column and one as a factor. Please note that the order of the factor levels is intentionally set to be non-sequential. This will help to illustrate the ordering or results when using a factor or a character vector as a grouping variable.
$cyl_character <- paste(mtcars2$cyl, "cylinders")
mtcars2$cyl_factor <- factor(mtcars2$cyl,
mtcars2levels = c(6, 4, 8),
labels = paste( c(6, 4, 8), "cylinders"))
Create other factor variables.
$gear_factor <-
mtcars2factor(mtcars2$gear, levels = c(3, 4, 5), labels = paste(c(3, 4, 5), "forward gears"))
Engine configuration: the vs
column is an integer vector for indicating V-shaped or straight. The constructed column engine is a factor the same information as a labeled factor.
$engine <-
mtcars2factor(mtcars2$vs, levels = c(0, 1), labels = c("V-shaped", "straight"))
Transmission: the am
column is an integer vector indicating if the transmission is automatic or manual. We construct a transmission
column to provide the same information as a factor.
$transmission <-
mtcars2factor(mtcars2$am, levels = c(0, 1), labels = c("Automatic", "Manual"))
The rownames of the mtcars2 data set provide the make and model of the automobiles. Here we will create columns for make and model and then omit the rownames.
$make <- sub("^(\\w+)\\s(.+)", "\\1", rownames(mtcars2))
mtcars2$model <- sub("^(\\w+)\\s(.+)", "\\2", rownames(mtcars2))
mtcars2rownames(mtcars2) <- NULL
To have some dates to use in examples we are going to add an mostly arbitrary date column to mtcars2. Given that the data came from the March through July issues of Motor Trend in 1974, we will create a test_date
column starting in January 1974 forward with one to three tests per week through May 1974. This assumes the data is in chronological order of the data.
set.seed(42)
$test_date <-
mtcars2as.POSIXct("1974-01-03", tz = "GMT") +
cumsum(sample(c(2, 3, 4, 7) * 3600 * 24, size = nrow(mtcars2), replace = TRUE))
Lastly we will order the columns of mtcars2 so similar columns are next to each other.
<-
mtcars2 c("make", "model", "mpg", "disp", "hp", "drat", "wt", "qsec",
mtcars2[, "cyl", "cyl_character", "cyl_factor",
"vs", "engine",
"am", "transmission",
"gear", "gear_factor",
"carb",
"test_date")]
mtcars2 is a data frame with 32 observations with 19 variables. Some of the variables tell us the same information, but in different formats.
str(mtcars2)
## 'data.frame': 32 obs. of 19 variables:
## $ make : chr "Mazda" "Mazda" "Datsun" "Hornet" ...
## $ model : chr "RX4" "RX4 Wag" "710" "4 Drive" ...
## $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## $ disp : num 160 160 108 258 360 ...
## $ hp : num 110 110 93 110 175 105 245 62 95 123 ...
## $ drat : num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
## $ wt : num 2.62 2.88 2.32 3.21 3.44 ...
## $ qsec : num 16.5 17 18.6 19.4 17 ...
## $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
## $ cyl_character: chr "6 cylinders" "6 cylinders" "4 cylinders" "6 cylinders" ...
## $ cyl_factor : Factor w/ 3 levels "6 cylinders",..: 1 1 2 1 3 1 3 2 2 1 ...
## $ vs : num 0 0 1 1 0 1 0 1 1 1 ...
## $ engine : Factor w/ 2 levels "V-shaped","straight": 1 1 2 2 1 2 1 2 2 2 ...
## $ am : num 1 1 1 0 0 0 0 0 0 0 ...
## $ transmission : Factor w/ 2 levels "Automatic","Manual": 2 2 2 1 1 1 1 1 1 1 ...
## $ gear : num 4 4 4 3 3 3 3 4 4 4 ...
## $ gear_factor : Factor w/ 3 levels "3 forward gears",..: 2 2 2 1 1 1 1 2 2 2 ...
## $ carb : num 4 4 1 1 2 1 4 2 2 4 ...
## $ test_date : POSIXct, format: "1974-01-05" "1974-01-07" ...
Element | Name | Description |
---|---|---|
[, 1] | make | Vehicle Manufacturer |
[, 2] | model | Vehicle model |
[, 3] | mpg | Miles/(US) gallon |
[, 4] | disp | Displacement (cu.in.) |
[, 5] | hp | Gross horsepower |
[, 6] | drat | Rear axle ratio |
[, 7] | wt | Weight (1000 lbs) |
[, 8] | qsec | 1/4 mile time |
[, 9] | cyl | Number of cylinders |
[, 10] | cyl_character | Number of cylinders as a character string |
[, 11] | cyl_factor | Number of cylinders as a factor |
[, 12] | vs | Engine (0 = V-shaped, 1 = straight) |
[, 13] | engine | same info as vs, but as a factor |
[, 14] | am | Transmission (0 = automatic, 1 = manual) |
[, 15] | transmission | same info as am as a factor |
[, 16] | gear | Number of forward gears |
[, 17] | gear_factor | Number of forward gears as a factor |
[, 18] | carb | Number of carburetors |
[, 19] | test_date | arbitrary date - created to approximate when the vehicle would have been assessed. |
Peak expiratory flow rate (pefr) data is used for examples within the qwraps2 package. The data has been transcribed from (Bland and Altman 1986).
The sample comprised colleagues and family of J.M.B. chosen to give a wide range of PEFR but in no way representative of any defined population. Two measurements were made with a Wright peak flow meter and two with a mini Wright meter, in random order. All measurements were taken by J.M.B., using the same two instruments. (These data were collected to demonstrate the statistical method and provide no evidence on the comparability of these two instruments.) We did not repeat suspect readings and took a single reading as our measurement of PEFR. Only the first measurement by each method is used to illustrate the comparison of methods, the second measurements being used in the study of repeatability.
The units of measure for the pefr are liters per minute (L/min).
# copied text from the manuscript
<-
pefr_table read.delim(
header = FALSE,
text = "
1 494 490 512 525
2 395 397 430 415
3 516 512 520 508
4 434 401 428 444
5 476 470 500 500
6 557 611 600 625
7 413 415 364 460
8 442 431 380 390
9 650 638 658 642
10 433 429 445 432
11 417 420 432 420
12 656 633 626 605
13 267 275 260 227
14 478 492 477 467
15 178 165 259 268
16 423 372 350 370
17 427 421 451 443")
Build the data set
<-
pefr expand.grid(subject = 1:17,
measurement = 1:2,
meter = c("Wright peak flow meter", "Mini Wright peak flow meter"),
KEEP.OUT.ATTRS = FALSE,
stringsAsFactors = FALSE)
$pefr <- do.call(c, pefr_table[, 2:5])
pefr
head(pefr)
## subject measurement meter pefr
## 1 1 1 Wright peak flow meter 494
## 2 2 1 Wright peak flow meter 395
## 3 3 1 Wright peak flow meter 516
## 4 4 1 Wright peak flow meter 434
## 5 5 1 Wright peak flow meter 476
## 6 6 1 Wright peak flow meter 557
See vignette("qwraps2-graphics", package = "qwraps2")
for examples using this data set.
sessionInfo()
## R version 4.0.4 (2021-02-15)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Big Sur 10.16
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] qwraps2_0.5.2
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.6 digest_0.6.27 R6_2.5.0 jsonlite_1.7.2
## [5] magrittr_2.0.1 evaluate_0.14 rlang_0.4.10 stringi_1.5.3
## [9] jquerylib_0.1.3 bslib_0.2.4 rmarkdown_2.7 tools_4.0.4
## [13] stringr_1.4.0 xfun_0.21 yaml_2.2.1 compiler_4.0.4
## [17] htmltools_0.5.1.1 knitr_1.31 sass_0.3.1