Lecture 12

IIT Kanpur July 201
20 followers
1 Video View·Dec 1, 2023
[Music]
[Music]
hello friends welcome to the course
foundations of r software
and you may recall that in the last
couple of lectures so we have learnt the
different aspects of calculations and
computation in the r software
we have learnt about how to do the
calculation with the scalars with data
vectors and with built in functions now
continuing on the same line another
ah
very important part of calculation is
matrix you know what is the matrix
and the rules for matrix operations they
are different than the usual
mathematical operations so the next
question before us is how to handle the
matrix in the r software and then how to
do
various types of calculations and
computation related to the matrix well
this matrix is very important because
when you are going to use the r software
for various applications in statistics
and other areas many times the input
data has to be there
in the format of matrix the output data
is also many times in the format of
metrics so how to understand it how to
read it and how you can extract a
particular part of the matrix it can be
an element it can be a row vector it can
be a column vector or it can be a sub
matrix then after that there are many
operations like as transpose inverse for
finding out eigen values
etcetera which are very popular when you
are trying to use
the statistics and other sciences where
you want to do the calculations
so now in this lecture and in the next
couple of lectures my target is that i
would like to discuss about the
matrices
so in this lecture i will try to give
you a
basic idea that how you can
define the matrix inside the r software
and then in the forthcoming lectures i
will try to take some common popular
operations which are related to matrices
so let us begin our lecture and try to
understand first that how we can handle
the matrix in the r software
ok so the matrix theory you know that ah
matrix
theory plays a very important role
in doing modeling and other type of
things
and
in r also matrices are
important
objects in any calculation the first
question comes here what is the matrix i
believe that you all have a
reasonable background in the matrix
theory but just for the sake of a
quick revision means i can tell you a
matrix is a rectangular array
in which the values are arranged in rows
and columns for example if i say that
there is a matrix which has got p rows n
n columns
right so this looks like this
i mean there are rows here one two up
two here three rows and then there are
here columns one two etcetera
and columns and the data is like
arranged like 1 2 3 4 5 6 and so on that
you know
now
the values inside this matrix they are
the elements of this matrix
and an element in the ith row and the g
column is denoted by say x i j like this
x and the subscript i j
ah well that is the notation when we try
to do this matrix algebra on the
blackboard in the book
but when we try to write it down in the
say program version means how to write
it in the software then
a popular
notation is like this one x
and inside the
square brackets you try to write here i
n j or i and j they are going to be some
values for example i can be any values
the value between 1 to n and j can be
any value between 1 to p
now you can see here
that for the first time i am now showing
you what is the application of this
square bracket and you can recall that
at some point of time i had shown you
that when we are trying to do the
mathematical calculation then we have
this type of
simple brackets then curly brackets and
then say square brackets and these
brackets have got
some important role in the r software
so now this is first place where i can
show you that this square bracket has a
different role
than the mathematical operations so now
in case if you try to understand the
matrix in a broader way then the element
of a matrix can be an object for example
a string also
but in mathematics we are mostly
interested in the
matrices which have got some numerical
values
and whose values are some real numbers
right so the first question comes here
how can you create a matrix in the r
software so we know
that whenever we want to create a matrix
there are
three ingredients which are needed
we want to know the total number of rows
then total number of columns in the
matrix and then we want to know what is
the data that has to be arranged in this
number of rows and columns
and after that
many question comes whether you want to
arrange the data row wise column wise
etcetera so you will try to seek answers
for all those questions
so the first thing i would like to
discuss here that how to create the
matrix
so in the r software
matrix is the command to create a matrix
which is m a t r i x
and all are given in the lower case
alphabets
and after that
within the parenthesis
you have to
indicate
the
basic information that how the matrix
has to be created
you want to inform the total number of
required rows columns and the data
so in order to inform the metric that
how many rows are going to be there we
have a command here n row
n r o w
and
in case if you write n row equal to for
example force so this will indicate that
there are four rows in the matrix
similarly in order to know the number of
columns in the matrix we have
the command here n c o l
and call which means number of columns
so if you write here n call is equal to
two
this will indicate that there are going
to be two columns in the matrix
now
when you have four rows and two columns
then you need to have four into two is
equal to eight
data values which are to be arranged
inside the
matrix in rows and columns
and you know
any particular element in the matrix
has an address
and that address is
indicated by the
row and column for example i can say the
element in the
second row and third column so in order
to give the
input data inside the
matrix
we have
our command here d a t a data and now
after this
ah you have to write
data is equal to
but after that you have couple of
options
that you can give the data in the form
of a sequence or individual values or in
continuation etcetera but anyway at this
moment we have not done many things in
the r software so i would try to
keep it here very simple
and i try to take here 8 values 1 2 3 4
5 6 7 8 and i use the
format of data vector so these values
are going to be
arranged in 4 rows and 2 columns
so in case if i try to assign
this command in a variable here x
and if you try to execute on the r
console then x will look like this so
you can see here this is
indicating the first row let's say r1
then second
row i say r2 third row say r3 and fourth
row sar4 and these are the columns so
this is suppose here column one and this
is here column 2.
so now you can see here there are 8
positions here
where
the data has to be
placed so now
all these values which are given in the
data vector they will be placed inside
this matrix in these
eight values
so you can see here
how these values are going to be placed
the first value if you try to see the
data is in this order one two eight so
the first value comes here then the
second value then the third value
then
the fourth value and after this the
control goes to the
first row in the second column so first
four values are inserted in the first
column in four rows
now after this the
fifth value is going to be entered
then sixth value is going to be entered
then seventh value is going to be
entered and then eighth value is going
to be entered so now you can see here
that this data is entered like this
so the data is entered here how column
wise that first the data is filled up in
the column and then in the rows yes that
can be interchanged also so how to get
it done these are the questions that we
are going to understand in the
forthcoming slides so this you can see
here this is the screenshot if i just
write here like this and then you get
here this type of matrix this type of
output okay so now if you try to see
in this matrix command
i have explain you what is the role of n
row what is the role of n call and what
is the role of data and these data
values are written column wise
so that is what you have to understand
now the next question comes here how do
you identify or how do you access a
particular value in the matrix so one
thing i can
share with you that is coming from the
matrix theory actually
that when i try to write down here x
say here 1 2 so this means this is the
value in the first row and second column
so this one two is indicating like here
the position of row and position of
column so for example in this matrix
which is given here if you try to see in
case if i see here x 1 2 so 1 means the
first row like i say like this
and 2 is the second column so like this
so this value at the intersection of
these two lines this is
the value of x 1 2 which is equal to
here 5.
so now
the same thing i want to
do in the r software so for that the
question is what type of command are we
going to use so in case if you want to
access a single element
for of a matrix say x
because you have to give the matrix a
name
so here i have given the matrix name as
x
then inside this square brackets you
have to first write the
position of the row
and then you have to write the position
of the column from there
you want to access that element
for example in case if i want to
access the element in the third row and
second column
then i have to write down here x third
row
that is r equal to three
and
column is the second so c is equal to
two so now if you try to see here if i
try to write down here x three two so
what is this value this is here my row
number three and this is my here column
number two so this value at the
intersection is seven
so now as soon as you write here x three
two and the three two they are separated
by comma and then they are written
within the square brackets so this value
will come out here 7 and this 7 is
actually here this 7
and if you try to execute it on the r
console also there won't be any problem
ok right so before i move further let me
try to give this example
it on to you on the r console so let me
try to define my here matrix here like
this you can see
x is here matrix n row equal to 4 and
call equal 2 etcetera and if you try to
enter here then you try to see the value
of here x this comes out to be like this
so you can see here now the values are
entered 1 2 3 4 column wise and then
5 6 seven eight right and in case if you
want to
access any particular
element from here you can write the name
of the matrix x and then suppose if i
say here four comma two so four comma
two will come here
eight because this is the fourth row and
the second column and this value here is
eight and similarly if you try to take
here say x two comma four
then what will happen do you what you
expect this is your here second row and
fourth column but where is the fourth
column i have only had
two column column number one column
number two so let us see what r does
it will give you error that the
subscripts
out of bounds that means you are trying
to give the
value of the column
which is not in the range of the
the columns which are present in the
matrix so rather if you try to write
down here something like
x two two
what does this mean
this is the second row
and second column so the value here is
six so if you try to enter here you will
get here the value six and in case if
you try to use here some other bracket
like x two
comma two you can see here this will not
work because
it cannot understand what this
parenthesis is trying to indicate so you
have to use only the
the square bracket
that is going to make the r understand
that this is a
command for the matrix so now we come
back to our slides and try to see some
more operations ok so now you have seen
that when you are trying to define a
matrix in this particular way
ah
as you have done
this data is going to enter
column wise that you can see here
the data is entered here column y like 1
2 3 4 and then 5 6 7 8 like this so now
my objective is that suppose i want to
enter this data row wise so how should i
control it
so when you are trying to enter the data
column wise one to it it goes like this
one two three four and then it goes to
second column five six seven eight
but when you want to
write down the data in
row wise it means that one
after this it will be two will be
assigned in the second column and then
it comes to three
and then it will go to 4 then it comes
to 5 in the first column and 6 in the
second column then 7 comes in the first
column and 8 comes in the second column
so now how to control it so for that we
have here
an option
which is by row b y r o w
this by row this is a logical variable
so this can take two possible values one
is here true
and another here is false
so now in case if you
write by row is equal to false and try
to think for a while what does this mean
you are trying to say by row is false
that means the data has not to be
arranged by rows when the data is not to
be arranged in rows then what is the
next option the data has to arrange in
the columns only so in this case if you
try to see here
the
outcome comes out comes out to be like
this data is arranged in the column but
what is this mean if you try to compare
this command which is not using by row
and the second command which is using
here the by row when you are trying to
use by row
then or you are not using the by row it
is giving you the same outcome this
means when you are not using the option
of by row then matrix command is
assuming by row equal to false as
default that either you give it or not
it will always assume that by row is
equal to false so now the question comes
if you want to arrange your data row
wise then what you have to do that is
now very obvious simply try to use by
row is equal to true
and in case if you try to use the same
command but now i am using it in a
different variable name say here y so
matrix command with n row equal to 4 and
call equal to 2 and data is the same but
by row is equal to now true now you can
see the outcome of y
so you can see here one is coming here
then it goes to two in the second column
then the next value comes with the first
column it is here three and then it goes
to the fourth column which is here four
and then it comes with the first column
as five then go to the next column as
six then it comes to the first column
seven and then goes to the next column
as eight so you can see here this
operation is going like this is row wise
assignment of the data so now you can
see this is in your control that how you
want to assign the data in the matrix
and if you try to see here the
screenshot that when you are trying to
see here there is no option by row is
equal to true or false you are not
writing anything then the data is
this here like this column wise and when
you are writing here by row is equal to
true then the data here is row wise like
this so let me try to show you these
operations on the r console also so that
you get here more confident so you can
see here this is your here matrix x
where you have not
used any command over here and you can
see here this is your here x
now in the same command let me try to
reduce the phone so that you can see
everything on the same scale
suppose i make it here 60
so now you can see here
that i try to add here
by row
is equal to
false now you can see here the value of
here x
this is here like this let me reduce the
phone size to be like this so that you
can see clearly yes now it is clear so
you can see here when you are trying to
use here by row equal to false then the
outcome here
and here that is the same but now in
case if you
try to use here this outcome and you try
to make it here true
and you try to assign it in a new matrix
say y then you can see here y
now you can see here when you have biro
equal to false then the data is like 1 2
3 4 and when you are trying to use here
by or equal to 2 then the data like 1 2
3 4 and so on so that is the difference
between the two right ok so now let me
come back to our slides and try to see
some more operations right
so now
let us try to consider the same matrix
and with then we try to learn some more
operations on this matrix x which has
four rows and two columns with the data
one two eight so this is your column
wise arrangement and we try to learn
here something here more now whenever
you are trying to use a matrix suppose
some matrix is given to you and you want
to know the information about this
matrix so one information in which you
are always interested in the dimension
of the matrix that how many number of
rows are there how many number of
columns are there and so on so in order
to know the dimension of the matrix we
have a command here d i m dim and within
the parenthesis if you write down the
name of the matrix it will give you the
information like is like this 4 comma 2
4 is the number of rows and 2 is the
number of columns and in case if you
want to know only
the number of rows then the command here
is n row and parenthesis inside the
parenthesis you write the name of the
matrix so this will be your here n row
and say inside paranthesis x and the
answer will come out to be here four why
because you can see here in this matrix
x you have here one two three four rows
and how many columns are there one and
here this two so now in order to know
the columns of a matrix you simply have
to use the command here and call ncol
and inside the parenthesis you have to
write the variable in which the matrix
is saved and it will
explain you the number of columns
present in the matrix so it is here 2.
so you can see here these are not very
big operations and similarly if you want
to know the mode of the matrix do you
remember mode we had done in the earlier
lecture where we had numeric character
its etcetera and remember this mode was
not the mean median mode is a
statistical mode so this is going to
inform us that how the values in this
matrix x are stored
so this is you can see here numeric and
similarly we have here one more
command here attributes so this also
provides all the attributes of an object
actually so in this case this is a
matrix so if you try to write down here
attributes x inside the parenthesis then
it will give you this type of
information which is trying to tell you
the dimension of this x which is here 4
and 2 which is four rows and two column
and the spelling of this attribute is a
double t r i b u t e s in all in lower
alphabets lowercase alpha bits small
alphabets so this is how you can obtain
this type of information now
in this matrix there are many other
options which are available that how you
can handle this matrix so i will not go
into all the details but i will request
use that you try to see into the help
metrics so try to type help and within
double quotes type here matrix it will
give you all the description metrics as
matrix is metrics etc and after that it
will give you the usage then
the details about different
arguments dim names and then after that
it will give you this type of detail i
have simply taken it from the help which
is available in the r right so i will
not detail it here because now you know
all the things i already have explained
you the concept of say s i and s for
example if you use here this command s
dot matrix so this will simply show you
whether the variable here is in the
format of matrix or not and in case if
you want to convert any number into a
matrix then you have to use as dot
matrix right so all these things are
there i would request you that you
please try to look into
this help menu and try to read it here
and i will try to show you these things
on the r console about these two
operation
dimension and row and call and
more attributes so let me try to show
you these things on the r console so you
can see here now let me try to see here
y is our this matrix and
if i want to find out the dimension of
this here y this is here 4 2. in case if
i want to find out the n row in the
matrix y you can see here this is here 4
and in case if i want to find out their
n call of this y this is here like this
2 and if i try to find out here the mode
of y this will be here numeric and if i
try to find out the attributes of here y
this will be like 4 2. so you can see
here getting the information about the
matrix
is not difficult at all ok so now we
come to an end to this
lecture this was a very simple
short lecture
short in terms of the
the content of the lecture my idea was
that ok this is the first lecture where
you are trying to understand the concept
of matrix for the first time
so
i should stop here so that you get some
time to do
all these operations yourself in the r
console
it is not difficult the only thing is
this you have to
settle down these concepts
in your mind
that how this end row end call data by
row etcetera they are going to work once
you are familiar with these objects
after that i promise you handling with
the matrices is very simple and it will
be
very easy for you to do such operation
so you try to have a quick revision try
to practice it and i will see you in the
next lecture with more
details on matrix operation till then
good bye
[Music]
do
[Music]
[Music]
you
