The five minutes you spend each week will provide you with a building block you can use in the next two hours at work. Review language basics, discover methods to improve existing R code, explore new and interesting features, and learn about useful development tools and libraries that will make your time programming with R that much more productive.
All series code samples can be downloaded at https://github.com/mnr/five-minutes-of-R.
Skill Level Intermediate
- [Narrator] When you're programming in r, there are several data structures that you'll want to be aware of. Vectors, lists, matrices, arrays, data frame, and factors. Let's talk about matrices. Now matrices are vectors with attributes of a dimension and optionally, dimension names attached to the vector. Let's create some data to work with. First of all create a vector. And into that vector, I'll place some strings.
All right, there's a very simple vector. And we can ignore the warning here. This is six elements, and it contains characters. Let's create a matrix based on that. It's done like this. A matrix, that's the name of the object that we'll place this into. And we're going to place into it a matrix. We're going to use the vector that we just created. And we're going to give it a number of rows.
So n row equals two. And a number of columns, so n col equals three. Now if I hit return, you'll see that I now have a matrix. And if you look at the structure over here, I have two objects, one column two, and one column three, that define the structure of that matrix. Let's take a look at what that looks like here. And you can see that I have two rows and three columns.
And that comes from when I defined this matrix. I said, "n rows equals two, and n columns equals three." So I have two rows and three columns. Now I can index into that matrix if I go i am a matrix and I use square brackets. And let's go with the second row and third column. And what you see is toves, which is if I go down two rows and over three columns, I get toves.
I can also see what the dimensions of that object is. So I hit an d i m I am a matrix and I'll come back with two rows and three columns. Now we can define these matrixes in different ways. And here's the first way, which is by row. I am a matrix. And into it we're going to do something a little more complex. We're using the matrix command again. And we're going to use the vector that we just used previously.
We're going to put the number of rows equals two and the number of columns equals three. And I'm going to type in by row equals false. And now what I can see, is I have, again, two rows and three columns. But you'll notice how the words read. So if I start at the top, I have to go down. Twas brillig, and then I switch columns to the second column.
And the, and the third column is slidey toves. Now let's change that. And if I repeat the previous matrix definition, and I'm going to put in true. And I'll type in I am a matrix again. And again, all I'm doing is hitting the up key to step through the history. Now what you'll see, is before, where it was up and down, this is left to right. So if I start at the first row and the first column, it's twas.
And then I have to go to the first row second column for the second word. So it's twas brillig and, which is the row, the slidey toves. So notice the difference between defining by row true and defining by row false. We can also take a look at dimension names. Let's create a vector here. And into that we'll store, let's do this, 10 letters.
And then we'll store 10 capital letters. So there's a bunch of letters, and lots of letters. Now let's create a letter matrix. And this is a matrix, so we'll use the matrix definition. And we're going to use lots of letters. It's a vector that I just created. The number of columns in this matrix is going to be two.
And the dimension names, now this is a little bit complex, we'll step through it. Dimension names has to be a list when you pass it to the matrix command. And it has to be a list of two items. First of all, the names of rows, and then the names of columns. I don't want to put any names of rows in. So I'm just going to put in an empty value, an empty vector. But I am going to name the columns. Let's call it lower case, and then upper case.
And when I hit return, now what I have, let's take a look at that. And you'll see that I have two columns with 10 rows. The first column is named lower case. And the second column is named upper case. Now we can change how that gets defined. Let's go ahead and I'm going to use the up arrow to step through the history. And I'm going to change how that list was created. So I'm going to go through and delete all of the elements of the list definition.
Again, this is dimension of list. And I need to make sure to match my parenthesis. So in dimension names, I'm going to first of all name the rows. And what we'll do here is I'll type in c, which stands for concatenate. And then lower case, followed by upper case. And then I'll type in a comma. And then I don't want to name the columns, so I'll just type in an empty concatenate.
And now if I hit return on that, you'll notice that I have a problem here. Because it says the number of columns that I've tried to put into this doesn't match the dimensions of the names. So I can change that if I go back here to letter matrix. And I'll type in the number of rows, instead of the number of columns, let's do that. You can see here, I accidentally called out for two columns. Let's call out for two rows. That's what I wanted. And that gave me an extra thing. So now if I hit return, now let's take a look at letter matrix.
And you'll notice that what I previously had was two columns of 10 rows. I now have two rows with 10 columns. With a matrix, I can also transpose elements. So let's go ahead and show how that works. Matrix, I'm creating a matrix called matrix transposed. And into that, we are going to transpose just simply t I am a matrix.
Now let's go ahead and hit that. And then we'll take a look and see what that actually looks like. Matrix transposed, and you'll remember that I am a matrix was the words from Jabberwocky. It was previously by row, and you'll notice that now it's by column. Now there's an interesting thing you can watch out for here, which is matrix transposed. And if I try to index into that, let's go with the second row and the third column. And if you're following along, you'll notice that I'm going to get an error, because I don't have a third column.
So instead what I have to do here is matrix transposed. Bracket, third row, second column, and that works because I do have three rows and two columns. So that's matrixes, and again a matrix is a vector or a list with two dimensions. And they have the attribute of dimension and optionally dimension names attached to that vector.