knitr::opts_chunk$set(echo = TRUE, results = 'markup')
Although there exists a function called vector()
in R, vectors are always defined in R using the function c()
which stands for "concatenation".
Let us assume we want to assign the following vector $a$
$$a = \left[\begin{array}{c}
10 \
7 \
43
\end{array}\right]$$
to the variable named a
in R, then this can be done with the following statement
a <- c(10,7,43)
A single vector element can be accessed using the variable name followed by the element index in brackets. Hence, if we want to know the first element of vector a
, we have to write
a[1]
Vector elements can be used in arithmetic operations such as summation, subtraction and multiplication as shown below
a[1] + a[3] a[2] * a[3] a[3] - a[1]
The function sum()
can be used to compute the sum of all vector elements. The function mean()
computes the mean of all vector elements.
sum(a) mean(a)
Arithmetic operations can also be performed not only on elements of vectors but also on complete vectors. Hence, we can add the vector a
to itself or we can multiply it by a factor of 3.5 which is shown in the following code-chunk
a + a 3.5 * a
Given are the following two vectors $v$ and $w$.
$$v = \left[\begin{array}{c} 3 \ -5 \ 1 \ 9 \ \end{array}\right]$$
$$w = \left[\begin{array}{c} 1 \ 9 \ -12 \ 27 \ \end{array}\right]$$
Compute
v <- c(3, -5, 1, 9) w <- c(1, 9, -12, 27)
Now we do the computations.
The sum $v+w$ is
v+w
The difference $v-w$ is
v-w
and the dot-product is
crossprod(v,w)
or
v %*% w
Please note: Although the R-function is called crossprod()
what is computed is the dot product between the two vectors. The function name crossprod()
is used because in Statistics the product ($X^TX$) of a transposed matrix ($X^T$) and itself ($X$) is called a matrix crossproduct. This has nothing to do with the crossproduct $v \times w$ between two vectors $v$ and $w$.
Matrices in R are defined using the function matrix()
. The function matrix()
takes as first arguments all the elements of the matrix as a vector and as further arguments the number of rows and the number of columns. The following statment generates a matrix with $4$ rows and $3$ columns containing all integer numbers from $1$ to $12$.
mat_by_col <- matrix(1:12, nrow = 4, ncol = 3) mat_by_col
As can be seen, the matrix elements are ordered by columns. Often, we want to define a matrix where elements are filled by rows. This can by done using the option byrow=TRUE
mat_by_row <- matrix(1:12, nrow = 4, ncol = 3, byrow = TRUE) mat_by_row
Matrix elements can be accessed similarly to what was shown for vectors. But to access a single element, we need two indices, one for rows and one for columns. Hence the matrix element in the second row and third column can be accessed by
mat_by_row[2,3]
Arithmetic computations with matrices can be done with the well-known operators as long as the matrices are compatible. For summation and subtraction matrices must have the same number of rows and columns. For matrix-multiplication, the number of columns of the first matrix must be equal to the number of rows of the second matrix.
In R the arithmetic operators +
, -
and *
all perform element-wise operations. The matrix multiplication can either be done using the operator %*%
or the function crossprod()
. It has to be noted that the statement
crossprod(A, B)
computes the matrix-product $A^T \cdot B$ where $A^T$ stands for the transpose of matrix $A$. Hence the matrix product $A \cdot B$ would have to be computed as
crossprod(t(A), B)
Given the matrices X and Y
X <- matrix(1:15, nrow = 5, ncol = 3) Y <- matrix(16:30, nrow = 5, ncol = 3)
Compute
X + Y Y - X X * Y crossprod(X, Y) crossprod(X) crossprod(Y)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.