[MUSIC PLAYING]
SPEAKER: Well, hello, one and all and welcome to our short on matrices
and arrays, two structures to store data all the same
type in multiple dimensions.
Now, why would you do that?
Well, I have here an aerial view of San Francisco and the Bay area
more generally.
And an interesting thing about this area is
that temperatures vary between the mountains and the city and the Bay.
We could use matrices and arrays to model how those temperatures actually
vary in given areas.
Now, setting aside matrices and arrays for now,
let's just think through how we could kind of pictorialize temperatures
across this map here.
Well, one thing I suppose we could do is break it up into a grid
a bit like this one here.
Here we have a three-by-three grid with nine cells in total
and three rows and three columns.
Now, it turns out, I've said before, temperatures
can vary between the city and the mountains and the Bay,
so we could slot in temperatures to each of these cells representing
the average temperature in the particular area
that we're talking about.
So for instance, in the top left here, in San Francisco,
we could add in this temperature.
Maybe it'll be 60 degrees Fahrenheit and keep going down this column.
Maybe just south of that it's about 55 degrees on average,
Fahrenheit again, and just south of that, in the mountains now,
it's even colder, maybe 50 degrees Fahrenheit here.
And we can keep filling things in now column by column.
So maybe this column here has these temperatures,
and this column here has these temperatures here.
So each area has its own average temperature, depending, in this case,
on where it's located in the time of day, as well.
So what we've created here is really a matrix,
a two-dimensional grid of values all of the same type, all numeric in this case.
It's a 2D grid where we have, in this case, three rows and three columns,
a three-by-three matrix, in this case.
So let's try to build this same idea now in R, making use of a function
that we'll call matrix.
So we'll come over here to RStudio, and let
me make here a file called weather.R, just like this.
And I'll see, in my File Explorer here, I now have this file weather.R.
We'll save temps.R data for a bit later here.
Let me open up weather.R, and the first thing I want to do,
is think through what data I should store in this matrix.
Well, to do that, I'll actually first need to make a vector.
A vector of the pieces of data I want to store in this matrix.
So let's go back to our visualization here and see what data we have.
Well, it turns out, when we make this matrix,
we're going to fill it in, like we did before, column by column.
So I want a vector that has all of these values
that I could fill in to this matrix column by column.
So I would start perhaps, with 60 degrees Fahrenheit, top left here.
Then maybe 55 degrees, kind of middle left, as well,
then 50 degrees bottom left.
Then I would go to 60 top middle, 60 again, in the middle of the matrix here,
and 55 down in the middle of the bottom row.
So let's keep going here and make up our own vector to store in this matrix.
We'll come back now to RStudio, and why don't we name this vector something
like temps, just like this.
And to make a vector of a certain type or to actually add in some data here,
I can use this function called c to basically add in individual values
to this vector.
So the first value, as we said before, is going to be the one in the top left,
60 degrees Fahrenheit.
I'll make sure that I have 60 here.
The next one, down below in the column now, was 55, followed by 50.
And that was our first column of data.
Let's move to the next column.
I believe that one began with 60 here, then we had 60 again, and then 55.
Let's check that 60, 60, and 55.
OK, pretty good.
Let's do the next column now.
I'll say that one was 65, 60, and 60.
Let's check that again.
I see 65, 60, and 60.
So it seems like our vector is appropriate now
for building this matrix.
Let's come back now here, and we want to make sure we actually
store this vector temps.
And if I type now temps in my console, I should, hopefully,
see all these numbers that are part of my vector.
Now, to make this matrix, I actually use a function called matrix.
And I would use it a bit like this.
I could use matrix by typing in the matrix function, just like this.
And it turns out that matrix takes a few arguments.
One is this vector called temps, the very first one.
The data we want to actually store inside of this matrix.
So I'll type temps here, saying that I will
use this vector I defined earlier to fill in this matrix I'm about to create.
Now the next two arguments, they are defined by the number of rows
and the number of columns we actually want to have in this matrix.
So again, we had a three-by-three matrix,
meaning we had three rows and three columns.
And then when we specify this in R is as follows,
I can say n row, for number of rows, and set it equal to some value, like 3,
in this case.
I could also do ncol, another argument here, and set that equal to 3, as well.
So now I've said, take this data from my temps vector
and turn it into a matrix that has three rows and three columns.
So if I'm at my console, a little bit bigger down here,
and I were to run this function matrix here, what should I see?
Well, a matrix down below looking very similar to the grid
we saw in our slides, albeit without the background here.
I see the first column of 60, 55, and 50 next column 60, 60, 55,
and then 65, 60, and 60 again.
So why don't we store this now in its own object.
I'll call this one weather.
And I'll assign it now to this name.
So if I type weather, in the future, I should now see this matrix of values,
a three-by-three grid of these values all of the same type.
Now, matrices can be of any dimension, actually, really
of any number of rows or columns.
They have to be two dimensional, that is.
Now, I can make this four columns or four rows,
whatever it is, as long as my vector has enough data to fill
in all of those rows and columns.
So if we have this matrix, what can we do with it?
One thing we can do is try to access certain values.
So if I tried here to maybe store this as a matrix, as I've already done,
I could then ask the question, what does this first row of values look like?
I could access that first row of values by using a syntax, actually,
just like we saw down below.
If I look down below here, I'll see that the first row seems to have this syntax
in front of it, [1,_].
Hmm, so if I actually go to my program here and type weather,
the name of our, in this case, matrix, and then I type [1,_] space
for style's sake, I'll hit Enter here, command Enter,
and I'll actually see the very first row of values in our matrix.
Let's check that again.
Here, I'll go back, and I see 60, 60, and 65 for the first row of this matrix,
so pretty good.
I could even try to find values by column, too.
So I could use weather, and then I could leave the first part blank,
just do comma space, and then type in 1, and that would give me, in this case,
the first column of values.
So to be clear, what we're doing here is this.
When I did weather [1,_], I was asking for this first row of values,
and when I did the second thing I did here, weather [, 1],
I'm asking for the first column of values in this matrix.
And I could use 1, 2, or 3 the indexes for this matrix
here, to get any row or any column I would like.
Now, I can also combine these.
So let's say I were to type this, weather [1,1],
I'm curious what you think that would give me if we look back at our matrix
here.
Might give us, in this case, the value in the first row
and in the first column, which would be this top left one 60 degrees.
So let's try that.
If I type this and run this line of code here on line 11,
I should see I get back 60 degrees from this matrix of values I've created.
So pretty handy here.
And there are other options for making matrices, too.
So if I were to type, ?matrix to actually open up the documentation
for matrices, I would see exactly how I could use this matrix function and some
other parameters inside of it.
So let me go over here and show you what that could look like.
If I scroll down, and look at the usage now for matrix,
I'll see that, again, the first argument was--
the one we gave it --in this case, it has a name, "data,"
but we gave the value of temps for our vector now.
We set these parameters here, nrow and ncol, which are by default 1.
And we could also change this parameter here called byrow.
So by default, we filled in our matrix by column first, first the first column,
then the next column, and the third column,
but we could invert that, and instead, fill in our data by row instead.
And there's one more here down here, too,
one called dimnames, which let us name the dimensions of our matrix.
The one going top to bottom, and the one going
left to right, in this case, kind of our x- or y-axis, if you will,
inside of our matrix.
So this is how we can make matrices in R and access their values.
Now, there is a more advanced data structure that
might actually come in handy for us.
One called an array.
So let's look at why we might use an array
and think through, conceptually, what it could represent for us in this case.
So I'll come back now to our slides, and let's think.
So this represents temperatures at a certain moment in time,
maybe in the afternoon time, but as evening approaches,
these temperatures might actually change.
They might go from this to this, getting a little bit colder
as the night goes on.
And maybe around midnight or so, once it is definitively night time,
it might be even colder.
Maybe it changes to be a temperature landscape more like this here.
So temperatures can change over time.
Each location can have its own temperature change over time.
Now, to represent this, you might notice we're using multiple matrices.
If I go back to our very first one, this was one matrix, this was another matrix,
and this was a third matrix.
So how could we combine these into one data structure?
Well, we can do so by thinking in three dimensions.
So here we have a cube, a good visual metaphor for what we're about to do.
You can imagine, again, our first matrix, which was two dimensional,
had three rows and three columns.
But you could also visualize time on its own axis
here, this third dimension that stretches back, in this case,
for instance.
You could imagine that these temperatures change
over time in this third dimension.
So for instance, if we look at, let's say,
the top-right cell in each of our matrices,
notice how it is first 65 degrees Fahrenheit, then as evening comes,
it stays about 65 degrees, but then as night time comes,
it changes to 55 degrees.
So time, in this case, is a third dimension stretching
backwards from our point of view now.
So you can have these structures that are multi-dimensional,
not just 2D but 3D or 4D, 5D, however many dimensions you want,
you can make in terms of an array.
So let's go ahead and try this out using this same data.
We'll come back now to weather.R. And let me clear what I have right now
and instead, load in this new vector I created and stored inside of this file
called temps.RData, on the right-hand side here.
So if I were to run this, load("temps.RData"),
saying take whatever data is stored inside this file,
temps.RData and load it into my environment, well,
I should get now a new value for this vector temps.
If I hit command Enter here, you'll see a lot more values in this vector.
And this is because we have enough data to fill in,
not just that first matrix, not just that second,
but also that third matrix, too.
Enough values to fill in three whole three-by-three matrices stretching back
in time, if you will.
So how can we turn this vector into an array?
Well, we can use, in this case, the array function.
So I will do just that here.
It looks very similar to our matrix function in terms of usage.
I could type array, just like this, and I could give in, as the first argument
here, this temps vector.
So I'll say temps is the vector we'll use to fill in this array of values.
But then array differs slightly from matrix,
and it takes another argument, another parameter one called dim, dim.
And dim stands for dimensions.
Now, the dimension argument actually takes a vector as input itself.
So here I'll define a new vector using our c function.
And I could say, well, what kind of dimensions do I want?
We saw before, when we had a 2D matrix, it was a three-by-three matrix,
but now if I look back at our cube, you might notice that, well,
there are three matrices here.
It's kind of like we have a three by three by three structure three rows,
three columns, but each of those stretches back three different times.
And you can visualize it a bit more like this notice how we have,
again three rows, three columns, but three of those rows
and columns stretching back in time.
So let's look at this now.
I could actually represent this same cube by making a vector like this,
3,3,3.
And this specifies I want three rows, three columns, and each of those
is stretched back three times in this other dimension we have.
So if I were to run this line of code here on line five,
you could then see a much more complex output down below.
But we can break it down into smaller pieces.
Notice here, that this should look a little bit familiar.
This is the matrix we had at the beginning of our temperatures at one
point in time.
But then, we also have the next matrix down below here,
and the next matrix down below here representing
how the temperatures have changed over time, as well.
So how could we access this data in this three-dimensional structure?
Well, we could actually still make use of this comma syntax.
We see a hint here.
Notice how I have comma comma 1, this seems to give me this matrix.
Comma comma 2, well, this gives me this matrix, our second one here,
and comma comma 3, gives me our third matrix here.
So I actually could try this.
If I go into our file, I could store this now
as the new array for this weather object.
I'll hit command Enter to store that.
And now, if I were to type, on line 10, weather[,, 1],
what do you think I should see?
Probably the first matrix in this 3D structure.
So I'll hit Enter now, and I'll actually get back that very first matrix
from before.
But then how did these temperatures change over time?
Well, I could look at the next one.
I could say, weather[,, 2], and now I see my second matrix, weather[,, 3],
and now I'll see our third matrix, again,
visualizing how these temperatures have changed over time.
So similar to matrix, array does have its own documentation too.
So if I type ?array, to open this documentation,
I can see ways to create an array.
I'll open this up for us here.
And let's look at the usage now for array.
So we saw our very first argument to array
was a parameter that we called data.
And we gave it the value temps, this longer version
of temps to fill in not just one matrix but three.
We then specified the dimensions in terms of a vector.
In this case, we gave it a vector of length 3, where
we asked for three rows, three columns, and three of those structures
to stretch back in time.
And finally, we have a very similar parameter,
dimnames if we wanted to name each of those dimensions,
like, x, y, or z or maybe like longitude, latitude, and time
we could do that here if we wanted to.
So this is a brief foray into how we have
tried to create our own structures using matrices and arrays to represent
temperatures how they can change over time.
I'm excited to see what you all create with these structures.
See you next time.