Matrices are just slightly more complicated than vectors.
To create a matrix, we usually start by creating a vector.
Here I'll create a vector that has elements 1 through 12
using the integer colon integer notation.
Next, I'll use the matrix function to create the matrix.
Whenever creating a matrix, it is helpful to provide the
number of rows and the number of columns to ensure that
you're creating the precise matrix that you expect.
You can do this using the end row and end col arguments.
If you happen to forget to use the argument names, R will try
to guess what you meant.
It usually does this based on the order of the arguments.
For the matrix function, if we specify the number of rows
before the number of columns, we get the
same type of result.
You can also experiment with leaving
off one of the arguments.
Notice that if I only include the number of columns, I need
to specify the end col argument name if I want to get
the same matrix that I got before.
The matrix function is very handy in that.
While you might specify the complete set of data for the
matrix right up front, you don't always have to do this
if parts of the matrix will repeat.
Here's a matrix with all zeroes.
We only need to specify a single zero
for the entire matrix.
We can also specify a partial vector that can be used to
fill in the entire matrix.
Let's create a three-by-four matrix with values 11, 12, and
13 filled in in the entire matrix.
In this example, the vector, which had length 3, was
recycled until the three-by-four
matrix was filled up.
Notice that the vector runs down the columns rather than
across the rows.
If we had wanted to make it run across the rows, we can
add a fourth argument to the matrix function--
byrow equals TRUE.
We also sometimes want to learn more about a matrix.
We might start using the dim function to learn the
dimensions of the matrix.
Here the output of dim is a vector of length 2.
The first value is the number of rows in the matrix and the
second is the number of columns.
We can also apply the length, head, and tail
functions to a matrix.
The length will return the number of elements in the
entire matrix.
And the head and tail functions will provide up to
six rows from the top and bottom of the matrix.
If we want, we can specify a second argument in the head or
tail function and take it a different number of rows back.
One last matrix topic to consider is how to select one
or more elements of the matrix.
Let's return to the matrix that we had at the beginning
of the video.
Here it's just values 1 through 12 in the matrix that
has three rows and four columns.
Like with vectors, we can subset a matrix, just like one
or more parts of the matrix.
But since a matrix is two dimensional, we need to
specify two dimensions in the brackets.
For example, if we use 2 comma 4, we will get the element in
the second row and the fourth column.
If we leave the column entry blank, we'll
get all of the columns.
And if we leave the rows blank, we get all of the rows.
Just like with vectors, you can use vectors of length
greater than 1 inside the subsetting to return
more than one value.
Using vectors with length greater than 1 to get both
rows and columns may produce a slightly surprising result--
another matrix.
Let's consider the matrix where we specify the rows 1
and 2 and the columns 3 and 4.
Doing so gives us back a submatrix specified by these
dimensions.
Finally, before moving on, one final word on matrix
subsetting.
If you forget the comma, R will still return a value.
Each matrix is stored in R as a vector with extra
dimensions.
So if we request the third element of the matrix, it
counts down the first column and returns the third element.
If we specify the fifth element, R runs out in the
first column, and so it goes to the second column as it
keeps counting.
I wanted to highlight this, not because subsetting this
way is a good idea, but just to make clear that it can be
accidentally done.
And so it's something you need to watch out for.
Using the row, column subset annotation is
the preferred method.
In the next video, we'll take a look back at the first six
videos and also how to access help files for functions
inside of R.