11) Create a Matrix of defined size with a Vector.

In Blog entries beforehand showed how to create a matrix using an intuitive method to bind vectors into columns and rows, comparing this to an Excel spreadsheet.  It is possible to create a matrix with a given specification then fill that specification with a single vector which over-spills the dimensions. 

The matrix() function is intended to take a single vector as an argument coupled with the dimensions (i.e. the number of rows and columns).  The single vector fills up this matrix by moving through each entry, downwards, in each column repeating the vector, should that vector not be long enough to fill up the matrix.

Start by creating a vector of six values by typing:

LongVector <- c(1,2,3,4,5,6)

1.png

Run the line of script to console:

2.png

Bearing in mind that the matrix will fill up column wise, make a matrix that is only three rows deep, while being four columns wide (i.e. nrow=3,ncol=4):

matrix(LongVector,nrow = 3,ncol = 4)
3.png

Run the line of script to console:

4.png

To view the matrix and specifically how the LongVector overlaid this matrix type:

OverspillMatrix
5.png

Run the line of script to console:

6.png

It can be seen that in moving column wise, when the vector runs out, it starts again until the matrix has been filled as per the dimensions.