noc21_ma36 - Lecture 03

IIT Kanpur July 201
20 followers
1 Video View·Dec 2, 2023
Hello, welcome to the course essential of data science with R software to where we are
going to talk about sampling theory and regression analysis topic and in this lecture we will
continue with the topics of introduction to R software.
So you may recall that in the last lecture I had brief description that how are you going
to start with R.
My objective was very simple suppose if there is a student who do not know about R much,
but he has a brief idea he does not know that much details of the R software possibly he
or she will get courage to start to learn these topics right ok.
Now in this lecture I will try to take some some more commands and I will try to show
you that how this R can be used as a calculator.
One thing I would like to mention here that every software has its own peculiarities the
way we write any command any function they may differ.
Similarly R also has some differences the commands are written in a different way than
in other softwares I am not saying that they are extremely different, but there are differences.
So my objective is that I want to make sure that you know what you are doing in order
to fulfill an objective how you have to write the command that you must know that is my
simple objective.
So in this lecture I am essentially going to explain how a simple calculations like
addition, multiplication, subtraction, division, board mass etc can be used in R.
So let us start with the course so in so in general you know that when we start our R
program then you get here a symbol like greater than this is here the prompt sign.
And when we try to assign a value to a variable the assignment operators there are two operators
so one assignment operator is you have to write the sign less than and hyphen.
So this looks like a left arrow at the dash and second option is this that instead of
writing this left arrow sign you can write simply the equality sign right.
For example if I take a variable x and if I want to assign the value 20 to it I have
two options that I can write down here x less than hyphen 20 or I can write down here x
equal to 20 both this commands will assign the value 20 to x right.
So now a very obvious question crops up to our mind that why there are two commands.
So I am the one who has seen the growth of this R software when I was pretty young means
now I am no more that young.
So when I started my career in 90s and when this S plus was my first encounter and then
came R in S plus they used to use the command left arrow is it of equality sign.
That time means I had learned different type of a language like Scoble, Fortran, basic
etc.
They all were using the equality sign for assigning the as an assignment operator but
S plus was the first language which used the symbol less than and dash.
So initially it was extremely not convenient to me at least that why they are using but
anyway that was the only software which I had to use so I practice it and then since
S plus was a very good software it has a capability to program and to use the built in commands
but it was an expensive software.
So a group of people all over the world they gather together and they started developing
the R software on similar lines I would say.
So that is why since they were trying to replicate the S plus software so they also
use the same symbol less than and hyphen.
So for a very long time the assignment operator in R software was less than and dash symbol
and since I grown up with that symbol so that has come into my habit to write this symbol
as an assignment operator although I try my best to use the equality sign but I am sorry
because of my habit sometime I use this thing.
So I will try my best to use this equality sign as well as means as far as possible but
if I use the sign less than and hyphen you please do not get confused that is not your
problem means I have to change myself that is my responsibility but yeah but you have
to help me out so that I can take care of my responsibilities gradually with your help
I can say right so means anyway.
So let us try to move forward and I will try to use both the symbols and you please excuse
me if you get confused between the two but in my opinion that will not make any difference
right okay.
And that now what I will do that whatever I am showing you here I will try to show you
on the R console also so that you get more confident yes whatever I have written here
that is also working.
So I will be switching between my slides and the R console so for example I have shown
you here that x equal to 20 is the operator and for we need we are using two operators
equality sign as well as less than dash sign so I will try to show you on the R console.
So let us try to come back on the R console and I can show you here for example means
if I show you here so you can see here I am writing here x equal to 20 and then if I try
to press it here x it will give me the value 20 and means if I try to write down here the
same simple x less than hyphen say here 30 so you can see here now the value of x is
changed to 30.
So now I have shown you that this both the symbol equality sign as well as the left arrow
sign both are going to right okay now let me come back to our slides okay.
So now suppose if I say here x equal let me assign it here x equal to 20 to a variable
x so I can use this symbol x equal to 20.
Now just like any other language in R also you can assign a variable to a new variable
what is this mean suppose if I define a new variable 3 into x so now this is my new variable
and I am assigning it to here new variable here y so I can do it here y is equal to
3 into x yeah I will show you later on that the multiplication sign multiplication symbol
in R is given by this star sign which is available on your keyboard.
And suppose I move further I have here 2 variables one was here x then I define a new variable
here x based on here this x and this new variable was here y and suppose I want to define here
new variable which is based on x and y.
So suppose I define here x – y and I call it here as a z so I try to define here a new
variable here z which is equal to here x – y and I try to execute it on the R console I
can show you here that I am using here x star 3 this will give me the value here x equal
to 60 because x is here 20 so y will become 3 into 20 that is 60 and if I try to make
it here z is equal to x – y so this 20 – 60 that will become here – 40.
So I will try to show you it on the R console also so I can show you here suppose I want
to clear my screen so as I said I will press here control and L so control and x L and
x equal to here 30 I try to take it here x equal to 30 well I have taken here x equal
to 20.
So let me take it here x equal to 20 now x equal to here 20 y is equal to 3 into x that
is here y so you can see here 3 into 20 here is 60 now I try to define here another variable
here z equal to x – y and you can see here the z value here is coming out to be – 40.
So now you can see here this is what I was trying to convey that in R just like any other
software you can store a scalar or a variable in a variable right okay.
And then we have a command here which is denoted by here c this commands here c will combine
the numbers that is used for combining different types of numbers.
For example if you have a set of numbers say here 1, 2, 3, 4 and if you want that R should
read all the numbers then you have to write it here c and then inside the parenthesis
you have to write these numbers separated by this comma.
Well in case if you try to write down only here 1, 2, 3, 4 then you will see that there
will be an error and in most of the cases the R will read only the first value.
For example I can denote it by here y equal to c 1, 2, 3, 4 and if I try to define here
z equal to 1, 2, 3, 4 this will have different types of this numbers.
So let me try to show you what do I mean by this thing.
So if I come here R console you can see here now I clear my window and I try to write down
here x is equal to suppose here c 1, 2, 3, 4 they have to be separated by commas.
So you can see here now it will read here c equal to 1, 2, 3, 4.
But now suppose if I say here if I give here y is equal to 1, 2, 3, 4.
Now you can see here this is giving me an error and if I try to give here the value
of here y it is not giving me the value of here 1, 2, 3, 4 but it is giving me the value
of here y which I had used earlier and if you try to recall in one of the earlier lectures
I had requested you in the last lecture actually that whenever you start a new programming
language you need to remove all this variables otherwise that will create a big confusion
right.
So anyway right similarly while doing the programming if you want to write a command
as a comment that means you do not want that the command to be executed then we use the
symbol here hash yeah nowadays hash is a very popular symbol hash tag and these are new
terminologies which have come.
So means I am sure that all the young generation knows about hash right.
So this is the symbol here hash so if you try to write down any command in which the
first letter is hash then anything whatever is written after hash will not be executed
and this will be treated as a comment only.
For example if I try to do it I will try to show you on the R console if I try to write
down here x equal to 20 then it will give me x equal to 20 but if I write down here
hash x equal to 30 it will again give me the same value it will not consider x to be 30
right.
So I will try to show you it on the R console so now if you try to show you here that this
is here x is equal to 20 so I can show you here that x is coming out to be 20 and now
suppose if I take x equal to 30 then now if you see here value of x this will come
out to be 30 but if I write down here hash x is equal to 40 you can see here still the
value of x is same 30 when you take it took x to be 20 then you took x to be 30 then 20
was replaced by 30.
But when you took this hash x equal to 40 then means again this was not a replacement
the value of x equal to 40 was not really assigned to x but the earlier value 30 was
considered right okay.
So now next very important thing that in R this capital and a small letter they makes
a huge difference they are just taken as 2 different values for example if you write
down here capital X and say small x these are 2 different things right yeah for x it
might not be so clearly visible but if I try to take it here say here a variable name here
is capital B and say small b they will be clearly visible and I will show you that these
are not the same thing for example if I try to take here capital X equal to 20 then and
for a small x equal to 20 and if I try to show you here the value of here X capital
X then it will show that there is an error it is not there.
But if I try to take here capital X to be 10 then it will give me capital X to be 10
and small x to be 20 which is coming from here I will try to show you it on the R console
which will make you more clear about these things right.
So if you try to see here now if you try to see here if I try to take here X equal
to 20 and say small x equal to 30 you may not see the difference on the screen so but
anyway since I have taken so I will try to show you capital X is 20 and small x is 30
but I will try to take it here a variable here capital B.
So B is going to be suppose here 50 so you can see here capital B is 50 but if I try
to take here small b equal to here 20 no first I try to show you what is the value
of a small b you can see here b is not found although you have given the value of capital
B. So now let me take it b is equal to here say here 80 small b equal to 80 so now you
can see here small b is 80 whereas capital B is again 50.
So small b and capital B these are the 2 different variables which are assigned to these things
right okay. So yeah this I already have shown you so I will just give you a brief thing
that this command C1234 is used to combine the numbers and one thing which I the reason
why I am trying to repeat it here I want to make you careful here that this command C1234
combines the number into a vector. Now this becomes sometime very confusing and that is
why I have written it 2 times here the same sentence so that you means I will be putting
my pen of different colors on the second line. So first line will always be clear to you
once you say vector, vector is a very confusing thing there is a data vector, data vector
means simple vector just try to combine all the things. There is a vector from the matrix
theory and beside those thing there is a things which are there in vector calculus also. So
these are different things but they are all called as vector what I would like to point
out here that when we are trying to combine the data here in C1234 we will also call it
as here vector but this is essentially a data vector. The operations of matrix theory
will not be operated on such a vector and if you want to have a vector like a vector
in the matrix theory then there is a different command matrix then we will that we will try
to discuss later on. So that is my idea here and I already have shown you this outcome
that if you try to give here 1234 or inside the parenthesis also 1234 it will give you
error it will not work unless and until you write Y equal to 1234 with the C command.
So that is my idea of this slide. Now I come on another aspect that how you can use this
R as a calculator. How you can operate the different types of commands like as addition,
subtraction, division, multiplication. So in R if you want to use the addition sign
then the symbol is very simple the usual plus sign. So if you try to give it here 6 plus 4
then this is here 10 means if I want to add 6 and 4 I have to simply write 6 plus 4 and if I
and similarly for multiplication the symbol here is star. So if I want to multiply 6 and 4 I have
to write down say 6 star 4 and yeah I just want to make it clear that there is no issue of the
blank sign between the 2 number. For example if I want to write down here 6 into 4 I can
write down here 6 into 4 just continuous without any blank space between these 3 values or I can
write down here 6 blank space star some blank space and here 4. They will be giving us the
same number and this I would like to clarify that this is true for all the mathematical operators.
Either this is equal to sign addition, multiplication, we will do subtraction,
division whatever you want this blank sign is not really going to make a difference.
So let me and try to first show you these things on the R console then I will move forward. So
if you want to add here 2 numbers suppose if I want to write here 6 plus 4 6 and 4 I want to
add you can see here this is 10. Now you can see here in this 6 plus 4 I have not given the space
between the 2. So I now I try to write down here once again 6 with space here 4 this is here 10.
And now if I try to add here more space even then you will see here the answer is 10. So
blank space has no value this is what I want to convey and similarly if you want to multiply here
2 thing 6 into 4 so you have to write 6 star 4 which is here 24 and even if you want to give
here blank space between the 2 unequal blank space whatever you want. So you can see here
24 even if I try to make it here something like this 6 and 4 means unequal number of blank space
even then it is 24. So be assured this is this blank space not going to contribute anything
to us yeah unless until I tell you right. Similarly if you want to subtract anything
suppose I want to subtract 6 and 4 then I have to use the symbol here the classical
traditional symbol minus. So means I have a so if I want to add here 6 minus 4 so this will
give me an answer 2 and if I want to use division then the symbol here is this backslash. So for
example if I want to divide 6 by 4 so I have to write 6 backslash 4 it will give me the answer
1.4. One thing I would like to point out which is a very common mistake because I will be
providing you the slides and in many cases and also in assignments also what will happen that
in many cases you will try to copy and paste the commands from my slides or from my notes or from
the assignments. These syntax these slides and these assignments have been prepared in software
like MS word PowerPoint etc. This MS word has a very peculiar property that once you write 6 minus
4 then as soon as you type 6 then minus and then blank space and then 4 you will observe this minus
sign is converted into another symbol which becomes larger. You can experiment it that if
you write 6 blank sign blank space minus blank space 4 as soon as you type 4 this hyphen which
is the minus sign becomes bigger and it becomes something else. So by looking at the eyes by
looking with your eyes you may not be able to identify whether this is minus sign or something
else. But when you try to copy the entire command and paste it on the R console that may create some
trouble and it will give you there is an error in the command or some error message. In those
situations you have to be very very careful that when you are trying to use the minus sign try to
type the minus sign inside the R console itself. This will not happen in case if you are trying to
copy and paste the command from the R studios R script window that will not happen. So this is
a point where sometime I see this that is to get confused and they waste their time. So just be
careful right okay. So now I come to my slides back. Now in case if you want to use the power
operator. Power operator means suppose I want to write down here 2 raise to power of a cube how
to write this thing. So for that I have here 2 options. First option is I can use here a command
hat and second option is I can use the command here 2 star double star. So I can write down 2
cube here like as a 2 hat 3 or 2 star star 3 and they will give us the output here say 8 right.
You can see here this is here the screenshot. Let me try to show you it here on the R console. So
first let me show you here the difference part. Suppose if I want to subtract here 6-4. So this
is 6-4 and here 2 and similarly if you want to here 40-2 this will be here 28 and suppose if
you want to divide this will be here 6 divided by suppose if you want to divide 6 by 4. So this
will be 6 backslash 4 like this and answer will be 1.5 and similarly if you want to divide here
40 divided by 2 the answer will be here 20 and similarly if you want to write down here 2 cube.
So you will have here 2 options either you write 2 hat 3 this will be here 8 or you write 2 star
star cube which will be your here 8. Similarly if you want to write down here 3 raise to power
of here 10 this is 59049 and if you want to write 3 raise to power of 10 at 3 star star 10 this will
be something like the same answer 59049. So you can see here that these are the different ways
by which you can write down these things. And similarly if you want to write down here
the square root if you want to find out the square root so you know that square root of say
here 2 this can be written as here 2 raise to the power of here 1 by 2. So now I simply have
to learn how to write down here this fraction 1 by 2. So this is so there are 2 option either you
write down here as say 1 by 2 which I will discuss later on that how to express this thing
or you can write down here 2 raise to power of here 0.5. So I have here 2 option if I want to
say obtain here square root of 2 first option is 2 raise to the power of here 0.5 or second
option is 2 star star 0.5 or third option is this that I can write down here 2 raise to the power
of here inside the bracket you know that much mathematics I can write down here 1 by 2 right.
So I will try to show you that all these things are going to work but the moral of the story is
that that if you want to use the power of a number you want to find out the power of a number. So
that is valid for integer as well as for fraction and you have to use it in the exactly same way as
you have done it earlier. And similarly if you want to use here you want to find out here 1 upon
a square root of 2 1 upon a square root of 2 is same as 2 raise to power of here minus 1 by 2
which is same as here 2 raise to power of here minus 0.5 right. So you can write down here here
as a 2 raise to the power of here minus 0.5 or you can write down here 2 power of here 2 double
star say here minus 0.5 or 2 hat minus 1 by 2 and so on whatever you want you want you can use it.
And in case if you have this multiple operators like as you want to use say this here plus sign
minus sign is multiplication sign division sign everything in one command then the rule
is very simple we are the R also follows the rule of bode mass that is the bracket of division,
multiplication, addition and subtraction that is the same what you have learnt right from your
childhood right. So let me try to show you all these things over here it means I will try to
show you here this means power operators and this bode mass rule okay. So let me come to here this
thing so power operator I already have shown you here so suppose I want to find out to 1 upon a
square root of 2 so this I can write down here 2 hat say here minus 0.5 so you can see here this
is the same thing right. And if you want to write down here 2 plus 3 minus 5 into 7 into 6 divided
by 7 and so on so in these cases the bode mass rule will be followed and you will get an answer
right okay. So now in this lecture I have shown you how this R can be used as a simple calculator
and you can obtain such results right. These are not very difficult things and these are the thing
which are usually the same as what happens in other software another languages but you need
to practice it you must know that if you try to write down a complicated expression where you
have addition, multiplication, subtraction what is really happening that is very important for
you. Because whenever you are trying to do the programming one of the very important step is
that before we execute the program on a real data we always try to take a very small data set and
we try to make all the calculations manually with our own hands. And we want to make sure that the
program is doing the same thing what we are asking the program to do it. So in order to understand
those things you need to understand these smaller things very clearly. So I will say try to take
some simple expression yourself try to write down on a piece of paper try to solve them with your
mathematical knowledge and try to type them on the R console and verify whether you are getting
the same outcome or not. This will boost your confidence so that when you are writing a big
program then you will be more confident. So you practice and I will see you in the
next lecture with some more commands on R till then goodbye.
