Arrays in R represent three or more dimensions of data. Learn how to manipulate and use this data structure.
- [Instructor] When you're programming an R there are several data structures that you need to be aware of. Vectors and lists and matrices and arrays and data frames and factors. Let's talk about arrays. Now in R an array is a vector or a list with two or more dimensions. And a matrix is actually a two dimensional array. Array is like a stacked matrix. So let's build some data that we can use to demonstrate that. First of all, let's build up a vector.
And there's a vector with a lot of values, string values in it. Zoom that out a little bit. Now let's create an array out of that. And to create an array, I'll go A-R-R-A-Y with parenthesis. And I'll bring up the previous vector that we created. And I'm going to give it some dimensions.
And in this case, I'm going to concatenate three values, two rows, three columns, and two levels. And we can ignore the warnings. So now I have an array, and you can see that there are three dimensions. Let's play around with that. Let's take a look at it first. And see what that actually looks like. So you can see that I have two tables. Actually looks like two matrices. The first one, twas brillig and the slithy toves has two rows and three columns.
And then there's a second level. And again it has two rows and three columns. It's the second half. We divided the original vector into two levels. I can index into this array and pull out values. So I am an array. And let's do something a little unexpected. I'm going to go with the second row, third column. A third level. Now I got an error back because there aren't three levels.
What I should have done instead is use the up arrow key to go back in the history and fix that error. So now I do have two levels. So now what I've done is gone into the second row, the third column of the second level. So if you look down into the second level the second row and go across three columns, the word is the. So you can index into array using those subscripts. We can substitute. So let's do this.
I am an array. And let's go two comma three comma two which is the second row, third column, second level. And into that space, I want to put a new value. And I hit array and if I type in I dot am an array, you'll see that in fact that new value, if you look down in the second level, second row, third column it says a new value.
One more thing I can show or emit values inside array. So let's go I am an array and we will index in, I'm going to go with the second row. And if I simply type in another comma, I will select all columns of the second level. And I get a selection of the second level. So it's the second row, second level gyre gimble and a new value.
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.