all right now that we know how to create
and name a matrix let's learn how to
subset and select single and multiple
values from it remember what we said
about matrices they are natural
extensions to vectors
well subsetting matrices works in much
the same way indexing and slicing
happens with vectors we just need to
take into account that matrices have two
dimensions instead of one ok let's get
to it I will create a matrix very very
quickly
you
this matrix gives us the box office
grosses of Oh 8 Harry Potter movies
on US soil and worldwide I have
intentionally left the column and row
names blank we will have no difficulty
adding them later
okay take a look at the matrix we just
made it has eight rows into columns and
our index system accordingly rows are
indexed with the number of the row
followed by a comma in square brackets
whereas columns are indexed by a comma
followed by the column number again in
square brackets
so why is that well this is the way our
notation system is all set up extracting
a value happens with square brackets and
the values you won't extract it in those
square brackets if you're working with
chloride dimensional objects think of
ours extraction schema as looking like
this basically the element that's always
there is the comma
okay let's try and call a single value
from the matrix 897 it is located on the
sixth row and in the second column
and just like indexing vectors we can
type HP mat open square bracket roll
number comma column number close the
square brackets and we get the value we
wanted 897 great easy
what if I wanted to call an entire row
however with all the values on that row
let's say I want to get the US and
worldwide box office figures for the
movie on the sixth row
if I just type this
our returns 290 a single value
but why does that happen well here is
why when we give our this specific
instruction it looks for the sixth value
not the row not the column so R goes
through all the values in the matrix one
by one column by column left to right
and it counts 290 is the sixth value so
this is what is returned
if we really want to get the entire six
row
I should have typed the comma after the
row number and use the blank space to
indicate that I want every value on that
row
open bracket six comma blank space
closed brackets this returns 290 and 897
all the values in the sixth row
the exact same logic applies to
extracting an entire column but this
time the comma precedes the column index
so HP mat square bracket blank space
comma to close the square bracket and
voila all eight values for the second
column
notice that our returns the result as a
vector instead of a matrix this is
because we're only asking for a
one-dimensional object and a vector
suffices to store such information which
brings me to the next type of subsetting
selecting multiple elements this and
more will be the topic of the next
lesson for more videos like this one
please subscribe