**Vectors vs Matrices**
A vector is a sequence of data elements that is one-dimensional. In contrast, a matrix is a similar collection of data elements but this time arranged into a fixed number of rows and columns. Since you're only working with rows and columns, a matrix is called two-dimensional. Unlike vectors, matrices can contain more than one Atomic Vector type.
**The Matrix Function**
To create a matrix, you use the Matrix function. It needs a vector containing the values you want to place in the matrix and at least one dimension specification. You can choose to specify the number of rows or the number of columns. For example, if you have a vector VOR containing the values 1 to 6 and you set the n row argument to 2, R infers that there will be three columns such that the number of matrix elements matches the number of input factor elements.
**Matrix Dimension**
Alternatively, you can specify the column dimension by setting the n call argument. In this case, R infers the number of rows automatically. If you want to fill up the matrix in a row-wise fashion, you can set the by.row argument of Matrix to True. This means that the input vector will be filled into the matrix from top to bottom.
**Handling Short Vectors**
When passing the Matrix function a vector that is too short to fill up the entire matrix, R fills up the matrix column by column and repeats the vector if necessary. If you try to create a matrix with a vector whose multiple does not nicely fit in the matrix dimensions, R generates a warning message.
**cbind and rbind Functions**
Apart from the Matrix function, there are two other easy ways to create matrices: cbind and rbind functions. The cbind function takes vectors as input and sticks them together as if they were columns of a matrix. The rbind function does the same thing but takes the input as rows and makes a matrix out of it. These functions can come in handy because they're often more easy to use than the Matrix function.
**Pasting Matrices**
The cbind and rbind functions can also handle matrices, making them useful for pasting another row or column to an already existing matrix. For example, if you have a matrix M containing the elements 1 to 6, you can add another row to it containing the values Seven, 8, and Nine by running this command.
**Naming Matrices**
To name a matrix, you use the names function for vectors, but for matrices, you can assign names to both columns and rows. This is done using the rownames() and colnames() functions, respectively.
**Creating Named Matrices**
There are also one-liner ways of naming matrices while building them. You use the dimnames argument of the Matrix function, which specifies a list with two elements: a vector of row names as the first element and a vector of column names as the second element.
**Data Types in Matrices**
Matrices can only contain a single Atomic Vector type. If you try to start different types in a matrix conversion automatically takes place. This means that if you have a matrix containing numerics, for example, trying to bind it with a matrix containing characters will result in an error.
**Conclusion**
In conclusion, matrices are just an extension of vectors and can only contain a single Atomic Vector type. While the Matrix function is a convenient way to create matrices, there are also other easy ways to do so using cbind and rbind functions. Additionally, matrices can be named using rownames() and colnames() functions. However, when it comes to binding different data types in a matrix, conversion automatically takes place.
"WEBVTTKind: captionsLanguage: enhello again in the next few videos I'll be talking about the Matrix as before I'll take you through all the basics you need to know about this new data structure so what's a matrix well a matrix is kind of like the big brother of the vector where a vector is a sequence of data Elements which is one-dimensional a matrix is a similar collection of data elements but this time arranged into a fixed number of rows and columns since you're only working with rows and columns a matrix is called two-dimensional as with the vector The Matrix can contain only one Atomic Vector type this means that you can't have logicals and numerics in a matrix for example there's really not much more theory about matrices than this it's really a natural extension of the vector going from one to two dimensions of course this has its implications for manipulating and subsetting matrices but let's start with simply creating and naming them to build a matrix you use the Matrix function most importantly it needs a vector containing the values you want to place in a matrix and at least one Matrix Dimension you can choose to specify the number of rows or the number of columns have a look at this example that creates a 2x3 Matrix containing the values one to six by specifying the vector and setting the N row argument to two R sees that the input VOR has length six and that there have to be two rows it then infers that they'll probably one three columns such that the number of Matrix elements matches the number of input Factor elements you could just as well specify n call instead of n Row in this case r infers the number of rows automatically in both these examples R takes the vector containing the values 1 to six and fills it up column by column if you prefer prer to fill up the Matrix in a row-wise fashion such that the one two and three are in the first row you can set the by row argument of Matrix to True Can you spot the difference remember how are did recycling when you were subsetting vectors using logical vectors the same thing happens when you pass the Matrix function to a vector that is too short to fill up the entire Matrix suppose you pass a vector containing the values 1 to three to the Matrix function and explicitly say you want a matrix with two rows and three columns R fills up the Matrix column by column and simply repeats the vector if you try to fill up the Matrix with a vector whose multiple does not nicely fit in The Matrix for example when you want to put the Four Element Vector in a six element Matrix R generates a Waring message actually apart from the Matrix function there's yet another easy way to create matrices that is more intuitive in some cases you can paste vectors together using the cbind and the rbind functions have a look at these calls cbind short for column bind takes the vectors you pass it and sticks them together as if they were Columns of a matrix the rbind function short for row bind does the same thing but takes the input as rows and makes a matrix out of them these functions can come in pretty handy because they're often more easy to use than the Matrix function the bind functions I just introduced can also handle matrices actually so you can easily use them to paste another row or another column to an already existing Matrix suppose you have a matrix M containing the elements 1 to six if you want to add another row to it containing the values Seven 8 and nine you could simply run this command you can do a similar thing with cbind next up is naming the Matrix in the case of vectors you simply use the names function but in the case of matrices you could assign names to both columns and rows that's why R came up with the row names and column names functions their use is pretty straightforward ret taking the Matrix M from before we can set the row names just the same way as we named factors but this time with the row names function printing M show that it works setting the column names with a vector of length three gives us a fully named Matrix just as with vectors there are also oneliner ways of naming matrixes while you're building them you use the dim names argument of the Matrix function for this check this out you need to specify a list which has a vector of row names as the first element and a vector of column names as the second element Don't Panic if you've never seen this list function before you'll learn all about that later on as I explained in the beginning of this video matrices are just an extension of vectors this means that they can only contain a single Atomic Vector type if you try to start different types in a matrix conversion automatically takes place have a look at these two matrices one containing numerics the other one containing characters let's now try to bind these two matrices together in a columnwise fashion using cbind did you see what happened the numeric Matrix elements were cerss to characters to end up with a matrix that is only comprised of characters to have a multi-dimensional data structure that can contain different elements you'll have to use lists or more specifically data frames up to you now to become a Jedi in the world of matrices enjoy it you shallhello again in the next few videos I'll be talking about the Matrix as before I'll take you through all the basics you need to know about this new data structure so what's a matrix well a matrix is kind of like the big brother of the vector where a vector is a sequence of data Elements which is one-dimensional a matrix is a similar collection of data elements but this time arranged into a fixed number of rows and columns since you're only working with rows and columns a matrix is called two-dimensional as with the vector The Matrix can contain only one Atomic Vector type this means that you can't have logicals and numerics in a matrix for example there's really not much more theory about matrices than this it's really a natural extension of the vector going from one to two dimensions of course this has its implications for manipulating and subsetting matrices but let's start with simply creating and naming them to build a matrix you use the Matrix function most importantly it needs a vector containing the values you want to place in a matrix and at least one Matrix Dimension you can choose to specify the number of rows or the number of columns have a look at this example that creates a 2x3 Matrix containing the values one to six by specifying the vector and setting the N row argument to two R sees that the input VOR has length six and that there have to be two rows it then infers that they'll probably one three columns such that the number of Matrix elements matches the number of input Factor elements you could just as well specify n call instead of n Row in this case r infers the number of rows automatically in both these examples R takes the vector containing the values 1 to six and fills it up column by column if you prefer prer to fill up the Matrix in a row-wise fashion such that the one two and three are in the first row you can set the by row argument of Matrix to True Can you spot the difference remember how are did recycling when you were subsetting vectors using logical vectors the same thing happens when you pass the Matrix function to a vector that is too short to fill up the entire Matrix suppose you pass a vector containing the values 1 to three to the Matrix function and explicitly say you want a matrix with two rows and three columns R fills up the Matrix column by column and simply repeats the vector if you try to fill up the Matrix with a vector whose multiple does not nicely fit in The Matrix for example when you want to put the Four Element Vector in a six element Matrix R generates a Waring message actually apart from the Matrix function there's yet another easy way to create matrices that is more intuitive in some cases you can paste vectors together using the cbind and the rbind functions have a look at these calls cbind short for column bind takes the vectors you pass it and sticks them together as if they were Columns of a matrix the rbind function short for row bind does the same thing but takes the input as rows and makes a matrix out of them these functions can come in pretty handy because they're often more easy to use than the Matrix function the bind functions I just introduced can also handle matrices actually so you can easily use them to paste another row or another column to an already existing Matrix suppose you have a matrix M containing the elements 1 to six if you want to add another row to it containing the values Seven 8 and nine you could simply run this command you can do a similar thing with cbind next up is naming the Matrix in the case of vectors you simply use the names function but in the case of matrices you could assign names to both columns and rows that's why R came up with the row names and column names functions their use is pretty straightforward ret taking the Matrix M from before we can set the row names just the same way as we named factors but this time with the row names function printing M show that it works setting the column names with a vector of length three gives us a fully named Matrix just as with vectors there are also oneliner ways of naming matrixes while you're building them you use the dim names argument of the Matrix function for this check this out you need to specify a list which has a vector of row names as the first element and a vector of column names as the second element Don't Panic if you've never seen this list function before you'll learn all about that later on as I explained in the beginning of this video matrices are just an extension of vectors this means that they can only contain a single Atomic Vector type if you try to start different types in a matrix conversion automatically takes place have a look at these two matrices one containing numerics the other one containing characters let's now try to bind these two matrices together in a columnwise fashion using cbind did you see what happened the numeric Matrix elements were cerss to characters to end up with a matrix that is only comprised of characters to have a multi-dimensional data structure that can contain different elements you'll have to use lists or more specifically data frames up to you now to become a Jedi in the world of matrices enjoy it you shall\n"