R tutorial - Learn How to Create and Name Matrices in R

preview_player
Показать описание
Understand how to create and name your matrices in R.

So, what is 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 are 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 the 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 the following example, that creates a 2-by-3 matrix containing the values 1 to 6, by specifying the vector and setting the nrow argument to 2:

R sees that the input vector has length 6 and that there have to be two rows. It then infers that you'll probably want 3 columns, such that the number of matrix elements matches the number of input vector elements. You could just as well specify ncol instead of nrow; in this case, R infers the number of _rows_ automatically.

In both these examples, R takes the vector containing the values 1 to 6, and fills it up, column by column. If you prefer to fill up the matrix in a row-wise fashion, such that the 1, 2 and 3 are in the first row, you can set the `byrow` argument of matrix to `TRUE`

Can you spot the difference?
Remember how R did recycling when you were subsetting vectors using logical vectors? The same thing happens when you pass the matrix function a vector that is too short to fill up the entire matrix. Suppose you pass a vector containing the values 1 to 3 to the matrix function, and explicitly say you want a matrix with 2 rows and 3 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 a 4-element vector in a 6-element matrix, R generates a warning 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 `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 6:

If you want to add another row to it, containing the values 7, 8, 9, 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 used 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 rownames() and colnames() functions. Their use is pretty straightforward. Retaking the matrix `m` from before,

we can set the row names just the same way as we named vectors, but this time with the rownames function.

Printing m shows that it worked:

Setting the column names with a vector of length 3 gives us a fully named matrix

Just as with vectors, there are also one-liner ways of naming matrices while you're building it. You use the dimnames argument of the matrix function for this. Check this out.
Рекомендации по теме
Комментарии
Автор

My man! Thanks for these organized, concise, STRAIGHT-TO-THE-POINT, ZERO BOOLSHHT tutorials. You guys understand the importance of clarity and time.

AJ-iunw
Автор

Omg! Now I understand it very well...thx heaps for very well organized information and clear explanations.
Before I found your videos, I just want to run away from R class😖..without any of programming and statistics background.
Am so lucky I found these videos before my exam..oh dear lord..thx Quarantine

khinmyatthu
Автор

May the Force be with you))) Thank you for great and up to the point lessons))) it is very helpful)))

bellaghazaryan
Автор

Thanks so much for these really helpful videos!

SamirNeg
Автор

Ay thanks a lot!! That was so helpful, thank you very much

baneledludlu
Автор

tysm sir i was jst mad for dis video thax a lot

sheetalsharma
Автор

Actualy matrixes can have more than 2 dimensions. Not sure if this is correct in R.

raphaelsouza
Автор

I have the different variables data in a single column, how can I convert these variables in different columns?

farrukhmahmood
Автор

one minor slip up, I think at 4:47 the dimnames should have the matrix m as argument. dimnames(m)=list(c("row1", "row2"), c("col1", "col2", "col3"))

DataSoong
Автор

How to create a matrix from inputs by user?

vasimraja
Автор

can I make the rownames bold, or make the line separating rownames from actual values bold?I need to distinct the names from the actual values?

katrinkostova
Автор

when ever we are declaring matrix and when we need to set byrow = True then in that case True always be in double quotes. Please correct this at 2:10 seconds of this video time.
Correct syntax is:
matrix(1:6, nrow = 2, byrow = "True")

kawaljeetsingh
Автор

I'm trying to understand what it means for a matrix to be able to contain only one atomic type. Because during d tutorial, you used the "rbind" and "cbind" function to merge both character and numeric data set in one metrix. So how do explain this. Because that is a single metrix with two different data types

vivianimade