-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions_check.Rmd
85 lines (68 loc) · 2.47 KB
/
functions_check.Rmd
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
---
title: "Check Functions"
author: "Hauke Sonnenberg"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Check Functions}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
### Function `assertRowsAndColumns()`
If you want to do calculations with a set of matrices it is important that all
matrices have the same dimensions and that rows and columns of the same name are
found at the same row and column indices, respectively. The function
`assertRowsAndColumns()` takes a matrix as input and returns a matrix as output.
The function guarantees that the returned matrix contains rows and columns of
requested names in requested orders. It does so by
* creating rows/columns that are requested but not contained in the input
matrix,
* removing rows/columns that are contained in the input matrix but not
requested,
* moving rows/columns that are contained in the input matrix but not in the
requested order.
Imagine three different matrices, each of with is created with `createMatrix()`,
another function from this package:
```{r}
(m_1 <- kwb.utils::createMatrix(c("a", "c"), c("x", "y"), value = 1))
(m_2 <- kwb.utils::createMatrix(c("a", "b"), c("x", "z"), value = 2))
(m_3 <- kwb.utils::createMatrix(c("b", "c"), c("y", "z"), value = 3))
```
Each matrix has two out of three possible rows `a`, `b` and `c` and has two out
of three possible columns `x`, `y` and `z`. You can now unify the shape of all
matrices by means of `assertRowsAndColumns()`, specifying the rows and columns
to be required:
```{r}
row_names <- c("a", "b", "c")
col_names <- c("x", "y", "z")
(m_1 <- kwb.utils::assertRowsAndColumns(m_1, row_names, col_names))
(m_2 <- kwb.utils::assertRowsAndColumns(m_2, row_names, col_names))
(m_3 <- kwb.utils::assertRowsAndColumns(m_3, row_names, col_names))
```
Missing rows or columns are inserted and filled with the value that is given
in the argument `fill_value` (defaulting to `0`, as can be seen above). Now,
the matrices can e.g. be summed up:
```{r}
m_1 + m_2 + m_3
```
Or, they can be put into a three dimensional array:
```{r}
array_3d <- array(
c(m_1, m_2, m_3),
dimnames = list(row_names, col_names, 1:3),
dim = c(3, 3, 3)
)
array_3d
```
Check Functions
### Function checkForMissingColumns()
### Function msgAvailableFields()
### Function stopIfNotMatrix()
### Function warnIfEmpty()
### Function warningDeprecated()