Skip to content

Commit

Permalink
Typesetting adjustments 1
Browse files Browse the repository at this point in the history
  • Loading branch information
aphalo committed Jan 6, 2024
1 parent ca4b180 commit 1e3a20d
Show file tree
Hide file tree
Showing 33 changed files with 432,905 additions and 432,629 deletions.
46 changes: 26 additions & 20 deletions R.as.calculator.Rnw
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ remove(VCT1, VCT2, VCT3) # cleanup
\end{playground}

\begin{explainbox}\label{box:integer:float}
In\index{numeric, integer and double values} \Rlang, all numbers belong to mode \Rclass{numeric} (we will discuss the concepts of \emph{mode} and \emph{class} in section \ref{sec:rlang:mode} on page \pageref{sec:rlang:mode}). We can query if the mode of an object is \Rclass{numeric} with function \Rfunction{is.numeric()}. The returned values are either TRUE or FALSE. These are logical values that will be discussed in section \ref{sec:calc:boolean} on page \pageref{sec:calc:boolean}.
In\index{numeric, integer and double values} \Rlang, all numbers belong to mode \Rclass{numeric} (we will discuss the concepts of \emph{mode} and \emph{class} in section \ref{sec:rlang:mode} on page \pageref{sec:rlang:mode}). We can query if the mode of an object is \Rclass{numeric} with function \Rfunction{is.numeric()}. The returned values are either \code{TRUE} or \code{FALSE}. These are logical values that will be discussed in section \ref{sec:calc:boolean} on page \pageref{sec:calc:boolean}.

<<classes-01>>=
mode(1)
Expand All @@ -156,10 +156,12 @@ Real numbers are a mathematical abstraction, and do not have an exact equivalent

<<classes-03>>=
is.numeric(1)
@

<<classes-03a>>=
is.integer(1)
is.double(1)
@

\end{explainbox}

\index{vectors!introduction|(}\label{par:calc:vectors:diag}
Expand Down Expand Up @@ -1972,29 +1974,26 @@ vct1[selector]
vct2[selector]
@

Numerical indexes can be obtained from a logical vector by means of function \code{which()}.
Positional indexes can be obtained from a \code{logical} vector by means of function \code{which()} as it returns a \code{numeric} vector with the positions of the \code{TRUE} values in the \code{logical} vector.

<<vectors-PG6a, eval=eval_playground>>=
indexes <- which(vct1 > "c")
indexes
vct1[indexes]
vct2[indexes]
@

Make sure to understand the examples above. These constructs are very widely used in \Rlang because they allow for concise code that is easy to understand once one is familiar with the indexing rules. However, if one does not command these rules, many of these terse statements become unintelligible.
Make sure to understand the examples above. These constructs are very widely used in \Rlang because they allow for concise code that is easy to understand once one is familiar with the indexing rules.
\end{playground}

\begin{explainbox}\label{par:calc:vector:map}
\index{vectors!named elements}
In all earlier examples we have used integer valued indices for extraction of elements. In the vectors used as examples above the elements were anonymous or nameless. In \Rlang the elements can be assigned names, and these names used in place of numeric indices to extract the named elements. There is one situation where this is very useful: the mapping of values between two representations.

Let's assume we have a long vector encoding treatments using single letter codes and we want to replace these codes with clearer names.
Above, \code{integer} or \code{logical} vectors were used as indices for extraction of anonymous elements, or members, from \code{character} vectors. In \Rlang, elements can be assigned names, and these names used in place of \code{numeric} indices to access them. One situation where this is very useful is the mapping of values between two representations. Let's assume we have a long vector encoding treatments using single letter codes and that we want to replace these codes with self-explanatory names.

<<vectors-named-01>>=
treat <- c("H", "C", "H", "W", "C", "H", "H", "W", "W")
@

We can create a named vector to \emph{map} the single letter codes into some other codes, in this case full words that are easier to understand. Above we used function \Rfunction{c()} to concatenate several \code{character} strings, without assigning any names to them, thus they can extracted from the vector using numeric values for indexing by position. Below, we assign a name to each string. Using operator \Roperator{=} we assign the name on the left-hand side (\emph{lhs}) to the member of the vector on the right-hand-side (\emph{rhs}).
We can create a named vector to \emph{map} the single letter codes onto full words. Above we used function \Rfunction{c()} to concatenate several \code{character} strings, without assigning any names to them, thus they have to be extracted from the vector using \code{numeric} values, indexing by position. Below, we assign a name to each string. Using operator \Roperator{=} we assign the name on the left-hand side (\emph{lhs}) to the member of the vector on the right-hand-side (\emph{rhs}).

<<vectors-named-02>>=
treat.map <- c(H = "hot", C = "cold", W = "warm")
Expand Down Expand Up @@ -2029,7 +2028,7 @@ row 1/.style={nodes={draw=none, fill=none, minimum size=5mm}}}]
\end{footnotesize}
\end{center}

As \code{treat.map} is a named vector, we can use the element names as indices for element extraction.
As \code{treat.map} is a named vector, we can use the element names, in addition to \code{numeric} values, as indices for element extraction.

<<vectors-named-03>>=
treat.map["H"]
Expand All @@ -2042,7 +2041,8 @@ treat.new <- treat.map[treat]
treat.new
@

where \code{treat.new} is a named vector, from which we will frequently want to remove the names.
\noindent
where \code{treat.new} is a named vector, from which we will frequently want to remove the members' names.

<<vectors-named-05>>=
treat.new <- unname(treat.new)
Expand All @@ -2052,7 +2052,7 @@ treat.new
It is more common to use named members with lists than with vectors, but in \Rlang, in both cases it is possible to use both numeric positional indices and names.
\end{explainbox}

Indexing can be used on either side of an assignment expression. In the chunk below, we use the extraction operator on the left-hand side of the assignments to replace values only at selected positions in the vector. This may look rather esoteric at first sight, but it is just a simple extension of the logic of indexing described above. It works, because the low precedence of the \Roperator{<-} operator results in both the left-hand side and the right-hand side being fully evaluated before the assignment takes place. To make the changes to the vectors easier to follow, we use identical vectors with different names for each of these examples.
Indexing can be used on either side of an assignment expression. In the code chunk below, we use the extraction operator on the left-hand side of the assignments to replace values only at selected positions in the vector. This may look rather esoteric at first sight, but it is just a simple extension of the logic of indexing described above. It works, because the low precedence of the \Roperator{<-} operator results in both the left-hand side and the right-hand side being fully evaluated before the assignment takes place. To make the changes to the vectors easier to compare, identical vectors are used in each of the examples below.

<<vectors-7>>=
vct2 <- 1:10
Expand All @@ -2077,7 +2077,7 @@ vct2 <- 1 # no recycling
vct2
@

We can also use subscripting on both sides of the assignment operator, for example, to swap two elements.
Indexing can be used simultaneously on both sides of the assignment operator, for example, to swap two elements.

<<vectors-8>>=
vct3 <- letters[1:10]
Expand Down Expand Up @@ -2163,6 +2163,8 @@ rm(list = setdiff(ls(pattern="*"), to.keep))
\section{Matrices and Multidimensional Arrays}\label{sec:matrix:array}
\index{matrices|(}\index{arrays|(}\qRclass{matrix}\qRclass{array}

Matrices have two dimensions, rows and columns, and like vectors all their members share the same mode, and are atomic, i.e., they are homogeneous (Figure \ref{fig:matrix:margins}). Most commonly, matrices are used to store \code{numeric}, \code{integer} or \code{logical} values. The number of rows and columns can differ, so matrices can be either square or rectangular in shape, but never ragged.

\begin{figure}
\centering
\begin{footnotesize}
Expand Down Expand Up @@ -2218,8 +2220,6 @@ rm(list = setdiff(ls(pattern="*"), to.keep))
\caption[Diagram of an \Rlang matrix.]{Diagram of an \Rlang matrix showing indexing of members.}\label{fig:matrix:margins}
\end{figure}

Matrices have two dimensions, rows and columns, and like vectors all their members share the same mode, and are atomic, i.e., they are homogeneous (Figure \ref{fig:matrix:margins}). Most commonly, matrices are used to store \code{numeric}, \code{integer} or \code{logical} values. The number of rows and columns can differ, so matrices can be either square or rectangular in shape, but never ragged.

In \Rlang, the first index always denotes rows and the second index always denotes columns. The diagram below depicts a matrix, $A$, with $m$ rows and $n$ columns and size equal to $m \times n$ ``cells'', with individual values denoted by $a_{i,j}$. Here we use a simpler representation than that used for vectors on page \pageref{par:calc:vectors:diag} above, but the same concepts apply.

\begin{warningbox}
Expand All @@ -2242,17 +2242,17 @@ where $A$ represents the whole matrix, $m \times n$ its dimensions, and $a_{i,j}

Vectors have a single dimension, and, as described on page \pageref{par:calc:vectors:diag} above, we can query this dimension, their length, with function \Rfunction{length()}. Matrices have two dimensions, which can be queried individually with \Rfunction{ncol()} and \Rfunction{nrow()}, and jointly with \Rfunction{dim()}. As expected \Rfunction{is.matrix()} can be used to query the class.

We can create a matrix using the \Rfunction{matrix()} or \Rfunction{as.matrix()} constructors. The first argument of \Rfunction{matrix()} must be a vector. Function \Rfunction{as.matrix()} is a conversion constructor, with specialisations accepting as argument objects belonging to a few other classes.
We can create a matrix using the \Rfunction{matrix()} or \Rfunction{as.matrix()} constructors. The first argument of \Rfunction{matrix()} must be a vector. Function \Rfunction{as.matrix()} is a conversion constructor, with specialisations accepting as argument objects belonging to a few other classes. The shape of the \code{matrix} is controlled by passing an argument to either \code{ncol} or \code{nrow}.

<<matrix-01>>=
matrix(1:15, ncol = 3)
matrix(1:15, nrow = 3)
@

When a matrix is printed in \Rlang the row and column indexes are indicated on the left and top margins, in the same way as they would be used to extract whole rows and columns.
When a \code{matrix} is printed at the \Rlang console, the row and column indexes are indicated on the left and top margins, in the same way as they would be used to extract whole rows and columns.

\begin{explainbox}
Matrices are most useful for storage of numeric values as matrix algebra plays an important role in statistical computations. This notwithstanding, it is possible to create matrices (and arrays) from atomic vectors of other classes such as \Rclass{logical} or \Rclass{character}. The only difference is the scarcity of meaningful operations other than retrieval of members using two indices.
Matrices are most useful for the storage of numeric values as matrix algebra plays an important role in statistical computations. This notwithstanding, it is possible to create matrices (and arrays) from atomic vectors of other classes such as \Rclass{logical} or \Rclass{character}. The only difference is the scarcity of meaningful operations other than retrieval of members using two indices.

<<matrix-character-01>>=
matrix(letters[1:15], nrow = 3)
Expand Down Expand Up @@ -2330,7 +2330,9 @@ colnames(mat1)
rownames(mat1)
colnames(mat1) <- c("a", "b", "c", "d")
mat1
mat1[ , c("b", "a")]
rownames(mat1) <- c("A", "B", "C", "D", "E")
mat1
mat1[c("E", "A", "D"), c("b", "a")]
colnames(mat1) <- NULL
mat1
@
Expand Down Expand Up @@ -2589,6 +2591,9 @@ When the pattern of levels is regular, it is possible to use function \Rfunction

<<factors-bx-01>>=
gl(n = 2, k = 5, labels = c("A", "B"))
@

<<factors-bx-01a>>=
gl(n = 2, k = 1, length = 10, labels = c("A", "B"))
@
\end{explainbox}
Expand Down Expand Up @@ -2703,7 +2708,8 @@ fct4 <- factor(c("treated", "treated", "control", "control", "control", "treated
levels(fct4)
fct4 <- factor(fct4, levels = rev(levels(fct4)))
levels(fct4)
@
@%
\pagebreak

Sort in decreasing order, i.e., opposite to default.

Expand Down
Loading

0 comments on commit 1e3a20d

Please sign in to comment.