Lecture 7

IIT Kanpur July 201
20 followers
2 Video Views·Dec 1, 2023
[Music]
[Music]
hello friends welcome to the core
foundations of r software
and you can recall that in the last
lecture we started a small discussion on
some basic elementary operations in the
r software and in this lecture we are
going to continue on the same lines
so we are going to demonstrate here and
we are going to understand here some
more basic fundamental operations
to understand the functioning of the r
software so let us begin our lecture and
we try to learn some more aspects okay
so
now the first
basic fundamental command which i am
going to tell you is about this greater
than sign although i already have told
you it many many times and you know it
but in order to complete this lecture i
need to inform you formally also and i
need to tell you some more stories also
behind this thing so
this is the prom sign that you know that
when you are coming in the r console you
can see
this symbol so
this is the place after which you try to
write your commands now after this
when we
try to consider the problem that how to
assign a value to a variable
then the
assignment operator
in the case of
r software
there are two assignment operators
one is like this
left arrow and dash this and this
and
second operator is the usual equality
sign like this one
so there are two
assignment operators in the r software
one is less than and dash and another is
equal to sign
actually
when r is started
then in the beginning
there was only one assignment operator
which was like this
less than n dash and this was the same
operator which was also available in the
s plus software
so that is why possibly
in the r
the same
operator continued but recently couple
of years back
when this r software was updated
then along with this the equality sign
was also incorporated as an assignment
operators well in case if you try to ask
any hardcore programmer then possibly
they can explain you that what will be
the difference between the use of these
two symbols but
for users like us practically it will
not make any difference whether we are
going to use
less than an
symbol or equality symbol
so now onwards most of the time i will
try my best to use equality operator
but in many many resources and books you
will also see
this operator less than dash so please
don't get confused both are the same
thing and i have explained you the
reason also
so you need not to get confused so for
example in case if i want to assign the
value 20 to the x i have to write x
less than
hyphen 20. so this is going to assign
the
value 20 to the x and similarly on the
other hand
in case if you write x equal to 20 this
is also going to do the same thing and
this will also assign the value 22x so
you can see here in the r console i can
show you that if you try to write down
here x less than hyphen 20 this also
gives you 20 and when you write x equal
to 20 this also gives you 20. and once
you have assigned a value to a variable
then it is also possible to assign a
variable to another variable for example
in case if i choose
x equal to 20 and then i define a new
variable x into 2 because the star sign
is the
multiplication sign
this 2 into x is going to be assigned to
a new variable y
and after that the value of
x plus y which is also a variable this
is going to be assigned to a new
variable here z
so now with the these three statements i
am able to show you that numerical value
can be assigned to a variable as well as
a variable can also be assigned to
another variable so if you try to see
here if i try to write down here y is
equal to x into 2 and then the y value
become 40 and if i try to write here z
is equal to x plus y then x plus y is 20
plus 40 which is here 60. so i try to
show you it on the r console also so
that you feel more confidence so if i
try to say here suppose if i write here
x equal to 20
you can see here now the value here is
assigned to be here like this x equal to
20 and you already have done it many
many times but now i am telling you
formally on the other hand in case if
you try to suppose write x less than
hyphen say forty
so you can see here what happens now the
value of x is forty
but in case if you try to write down
here once again x
less than hyphen twenty you can see here
the value of x will change here to x
equal to 20 right so now in case if i
try to define here x equal to 20 then
suppose i define here another variable
here y as see here 2 into x
so now you can see here the value of y
becomes here 40.
so the value of x is coming from the
first variable
and then the value of y is obtained and
similarly if you try to obtain here z is
equal to suppose x plus y you can see
here the value of z comes out to be 20
plus 40 which is 60. so now you can see
here that
the values as well as variables can be
assigned to any variable right
now when you are trying to assign a
value to a variable then there are two
types of values one which are numerical
and
other are some characters
so
the numbers i already have discussed
that the numbers are assigned to
any variable in the usual way as we said
x less than hyphen twenty or x equal to
twenty but when you are trying to assign
the
or assign a character or a some word or
characters
to a variable then they have to be
assigned within the
double course or within the single
quotes for example suppose i
want to define a word apple a double p l
e
and i want to assign it to a variable
here x so i will write here
double quotes
and then within double course i will
write the value of the variable for
example it is here apple so this will
assign the value apple to the variable x
and similarly here also you can use this
operator x less than hyphen and suppose
if i say that here you want to to define
here apple 1 you can write down here x
less than dash within double quotes you
write apple one so this will
assign the apple one
to the variable x and beside those thing
if you want to assign the value here
with only the single quote also that is
also possible instead of using the
double quote you can also use here the
single quotes and either you try to use
the equality operator or less than and
dash operator they are going to assign
the
word apple to the variable x right so
here you can see here here i have
defined here like this x equal to 20 and
here you can see here within double
cores i am defined the variable and with
single quote also i am trying to assign
the variable and in both the cases there
is no issue both are acceptable but when
you try to look into the outcome outcome
will always have
double quotes that is what you have to
keep in mind so by looking at the
outcome you cannot judge whether the
value was assigned through a single code
or a double quote
now i try to show it on the r console so
that you get more confident
so suppose if i take here a variable
here x is equal to apple
you can see here now this x becomes here
apple and instead of this thing in case
if i instead of this one you can see
here now i am using here
single quote
so you can compare
now i see here apple so you can see here
apple
and instead of this if i try to define
here y
less than hyphen or dash
say
apple one
you can see here this is here again
apple one or
i can also use here the equality symbol
like this then you can see here
this is again apple one
right so this is the basic structure
what we try to use in the r software now
we try to
understand another aspect in the
r
programming
whenever you are trying to write down a
program you are trying to handle some
values now as you have seen these values
can be numerical values or these values
can be characters
so now the question is that whenever you
are trying to entertain any value within
the program using some command you would
like to know
whether this value is a numerical value
or this is a character so how to get it
done these type of situations are very
common when you are trying to write down
a program
for example when you write a program you
might be getting the data file from
somewhere else and you have no idea you
have absolutely no idea whether the data
is in the form of numerical
characteristics or some
data frame or some list etcetera so how
to know you cannot look inside the file
to know whether all the values numerical
or some values are character or some
values are numerical you need a way by
which you can find it out so in order to
know that if a value is a number we have
a command here is dot numeric and within
the parenthesis you have to write down
the variable
and
in case if you want to know
if a value is a character
then the command is
is dot character
i s dot c h a r a c t e r
and for s dot numeric this is i s dot
and u m e r i c and in both the cases
everything is in the lower case the
outcome of these two command
is dot
numeric or s dot character
is going to be in terms of true or false
although i will
take up this issue after some lectures
but
let me tell you here that this true and
false when they are written in the
capital letters capital t capital r
capital u capital e or false in all
capital letter or instead of true you
can also write capital t and in place of
false you can write only here capital f
these are the reserved words
reserved words means these words are
reserves to indicate only the true and
false which are the logical variable so
we will talk about the logical variable
after sometime but at this moment i can
explain you that you cannot use these
two words true and false all in capital
letters for any other thing and true
means true and false means false one
thing what you have to keep in mind that
capital t capital r capital u capital e
and
capital t
and all other things in a small letter
small t r capital small letter u e
etcetera any other thing they are
different so only the capital letters
are the reserved word so
the answer to these two commands is
going to be in terms of a logical
variable
as
either true or false and then you have
to understand what it is trying to say
for example if i try to take here x
equal to 20
now you know that this is a number
so this is a numeric value
so now if you try to use here the
command is dot numeric
and
inside the parenthesis you write this
value x
the answer is going to be here true
so that means yes
the x is a
numeric
and now in case if you try to use this
command
is dot character
and within the parenthesis you write x
so x here is 20. so do you think that x
equal to 20 is a numeric or a character
this is a numeric and it is not a
character
so when you try to use this command s
dot character inside parenthesis x then
it will give you the answer false
that means x is
not a character well one thing you have
to keep in mind beside this number so
that you have to be careful when you are
trying to interpret it when you are
trying to say s dot numeric is true that
means it is numeric and when you are
trying to say is dot character is false
so whatever is false that is false but
then what is true that we do not know
that can be character or that can be
something else that can be numbered that
can be something else and if you try to
execute these commands on the r console
you will get here the same thing what i
shown you here
now i try to take
one more example now i try to take here
a character
and i write apple within the double
quotes so now you can see here this is a
character
now i use the command here is dot
character
y
answer is true yes
apple what you have given here in the
variable y
this is a character
and when you try to use here the command
is dot numeric within the parenthesis y
then this is indicating
it is false
obviously y is apple which is a
character so when you are trying to
check whether this is numeric it is
telling you
no its not numeric and your statement is
false but now if you ask me what is this
whether it is matrix data frame list
etcetera that i do not know so you have
to interpret the result in this
particular way and if you try to do it
on the r console also you can see here
you will get the same outcome and then
the question comes over here
in case if you have got a number which
is either
numeric or character can you interchange
its role why not mean suppose i am
getting a value here
for example x equal to 20
is this a number for that i use command
here is dot numeric it is true yes this
is a number but somehow i want to use
this value as a character
what is the difference between a number
and a character that in case if you have
two numbers 20
plus 30 both are numbers so their sum is
going to be 50.
but if something is character
apple
plus say banana
or
apple
plus 50 you cannot find the sum
but somehow in some
value in some experiment
20 is going to indicate some category
so i want to use this value
as a character
but the default
value inside the r software or any
mathematical software that is always
taken as mathematical so the question is
how can you convert a numeric value into
a character so for that
in case if you want to convert a value
as a number
the command here is as dot numeric and
within parenthesis you give the variable
name
and in case if you want to convert a
value to a character
the command here is as dot
character and within parenthesis you
have to give the variable name and both
this command as dot numeric and as dot
character they are going to be written
in the lowercase alphabets
so now in case if you try to take the
same example
suppose i am trying to take here x equal
to 20 which is a numeric but now i
operate the command here as dot
character within parenthesis x and
whatever is the outcome i try to store
it here as a y
then now after if you check
whether this y is numeric so for that
you use the command here s dot numeric y
and you will get here false although x
was numeric but now y is no more numeric
now you would like to check
what is this y is this character
then the answer comes here yes
why because you have operated the
command s dot character to change a
number into a character and so
y has now become character and whose
value here is like this
you can now see here
your x was 20
but y is now given as 20 within the
double course you can see here
so that means this is now the character
you can see here this is the screenshot
of the same operation
right
now
in case if you try to do the opposite
suppose i have
got here a
character
and i want to convert it into a number
what happens
so suppose i take here the variable here
apple which is a character and i store
it here as a y
now i try to operate the command here is
dot numeric y that means i want to know
whether this y is numeric or not
the answer comes out to be here false
means obviously y is apple it is
character so it cannot be numeric so
that is why the answer is coming out to
be false but now i would like to check
is dot character y that means whether y
is a character answer comes out to be
here true yes y is a character now
i try to convert this y into number so i
try to use the command here as dot
numeric and within parenthesis here y
and then i try to store this value
inside this new variable z
so now it is
trying to give us a warning message
whenever you are trying to work in the r
software you will sometime get some
messages these messages are of primarily
two types one is here warning
and second category is error
so what is the difference between
warning messages and error messages
warning is just like a warning warning
means when you really want someone you
are not exactly going to do the same
thing
right
and when you are getting a
error message that means this is a
mistake this is an error and the program
cannot execute after this so when you
are getting the warning message that
means the program will continue but it
is warning you that something is
happening please try to look please be
careful please be watchful but when
there is error message then the program
will simply stop there it will not move
forward because there is a mistake there
is an error unless and until you correct
it the program will not run so it is now
giving you here and is introduced by
corsair question means forcefully
and n a
is a sort of something like
missing value not available which we are
going to discuss once again i will say
in the
further lectures in more detail
so now if you try to see here
that is dot numeric z
it is telling you yes now that is
numeric
but in case if you try to find out the
value of the z now tell me one thing you
are trying to convert apple into a
number what do you expect
is it possible no that is why when thing
was possible but when you are trying to
do it forcefully then z is trying to
replace this word by another number
which is n a and in case if you try to
see here is it a character answer is
false
because any is not actually a character
it looks like a character n a at this
moment but after i give you the details
of
two aspects of two reserved works n a
and
null then you will understand that just
like true and fall these are also the
reserved words so this is how we are
going to work in the r software when we
are trying to convert the numeric into
character or vice versa and you can see
here this is the same screenshot which i
have explained you here but then i would
like to show you it on the r
console also so let us try to
to understand these things
so let me try to
copy this command so that i can save
some time on the r console so let me try
to remove these things by pressing ctrl
l now you know so if i try to take here
x equal to hundred
and if i try to do here
s dot numeric
x
answer is
true and if i try to say here is a
character
it is false
but in case if i try to take here
y as say here apple
and now in case if i try to see here
what is y is it a numeric
answer is false
but when i try to say here is it a
character
then it is coming as yes this is
character so now
because you already have seen that x
equal to 100
is your
number and y
equal to apple is your character
so i would like to convert them number
into character and character into number
so
now you can see here the command here is
in case if you try to
convert a number
into a character
then the command here is as dot
character so i try to use here as dot
character x so you can see here this is
now within double code it is a hundred
so within double quotes as you are
trying to say
this becomes a character so by looking
at this
this hundred and this hundred you can
very easily identify that which of them
is the numeric and which of them is a
character
and similarly
in case if you want to
change this
character into a number then what you
have to do your y is here is apple
and you want to convert into a numeric
so you can see here this is giving you
the value here is coming out to be here
n a but it is giving you a warning
message that n is are introduced by
quotient when forcefully means you are
forcing r to do something which is not
acceptable but
it is trying to do
something to follow your orders and it
is trying to replace the apple by the
value n a right so this is how the
things work when we are trying to work
with these characters and numbers in the
r software now after this one more
aspect whenever you are trying to do any
programming then as a good programmer
you always like to give some information
which can be used either by yourself or
by somebody else for example suppose i
am cutting at i t kanpur and i am
writing a program in which i have
assumed three variables x is height y is
weight and that is h now
when i am trying to send this program to
somebody else outside iit kanpur how do
i communicate and if i try to
communicate in a different file that
will be very confusing so what i will do
that within the program i would like to
mention
that x is this y is this and that is
this so how to get it done so in r
software
in case if you write this hash
symbol
in the first place on a line then after
that whatever you are going to write
that will become only a comment
and no mathematical operations will be
done over that
for example in case if i am trying to
write down here hash
mu is the mean so you can see here
in case if you try to enter here
nothing will happen no warning error
etcetera
that is accepted as soon as you write
here hash all the characters after this
up to the end of the line they are
simply ignored and r will skip this line
r will come to know that i do not have
to operate on this line and similarly in
case if you try to write down here hash
x equal to twenty or x hash
less than dash 20 then it is going to be
considered only as a comment for example
if you try to write like here this then
it is going to take it here as a
numerical value
a numerical value of 20 is assigned to x
but when you are trying trying to write
down here this hash then it is not going
to be like this
right
and
one very important thing
r is case sensitive the small letters
and capital letters they are different
for example in case if you try to write
down here x
equal to 20
in capital letters and small letters
they are going to be different for
example i can show you here on this
screen
output and then i will try to show you
in the r console also when you are right
trying to write down here x this is say
lowercase alphabet lowercase x
you are trying to write down here lower
case
x is equal to 20 and you press here x it
will come out to be here 20
but when you try to write down here
capital x and enter it will say error
object x not found because you have
defined it to be small x but you are
trying to be here trying to find out the
value of your capital x but in case if
you try to assign here the value capital
x equal to 30 and then if you try to see
the value of capital x this will come
out to be 30
and the value of small x will come out
to be 20. so i will try to show you this
thing so you have to be very careful
when you are trying to work in the r
console that lower case alphabets and
uppercase alphabet they are going to be
different and that was the reason that
many times whenever i am trying to
explain you about the functions name and
commands whenever there is a small
letter
or capital letter or lowercase or
uppercase i am informing you very
clearly that this is what you have to do
so before we move forward let me try to
show you these two commands on the r
console and then we will move forward
with some more commands
so now you can see here suppose if i
write here
mu is the mean you can see here it will
not understand it will say some error
some unexpected symbol in mu is
something like this but
in case if i introduce here the symbol
hash
then if i try to do it
it will say that it will just accept it
that there is no issue now
in case if i try to say here 20 plus
forty
this will come out to be a sixty but in
case if i try to put here a hash symbol
before this
twenty plus forty let us see what
happens
nothing is happening there is no value
so that is the different that when you
are trying to put a hash at the first
place
then r is not really going to execute
whatever is written after this
right
now suppose if i say here
capital k is equal to 5
and i
try to write here the value of here k
like this
and i try to write down here hash k
there is no value there is no outcome
even if i try to write down a small k
here it is there is no outcome right so
that is the thing what i want to show
you here now after this i try to show
you what is the difference between
lowercase and uppercase alphabets so let
me try to take it here suppose if i try
to take here capital l
this is equal to
30
right
and then i try to press here what is
small l
well i am not taking a small x and
capital x because you can see that it
may be difficult for you to identify
very clearly the difference between
small and capital x so that is what i
have taken here l
right but
in case if i say here l is equal to 30
or say 20 you can see here now there is
here l
and if you want to press it here capital
l it will give you this thing right and
similarly in case if you try to take
here another variable here
m is equal to
say 30
and if you try to find out the value of
a 2 into small m it is saying no is not
found because you have defined the value
here there is a capital m and you are
giving here as a a small m so but if you
try to change it to capital m it will
give you 60. that is what you have to
keep in mind when you are trying to do
the programming these are very common
mistakes and but
sometimes they take very long time to
find it out similarly there is another
common mistake when people try to write
0 or o once you write
0 and you want to write o or if you want
to write
o you write 0 it is very difficult to
find and particularly when you are
trying to write down a mathematical
expression where you have written o in
place of 0 then
it takes a considerable amount of time
to find out the mistake ok now i come to
another aspect this is about combining
the values in a data vector now you see
i am using here the new terminology data
vector
do not get confused with the word vector
we have vectors in physics vectors in
vector calculus etcetera etcetera but
this is here our data vector data vector
means it is trying to combine
more than one values for example in case
if you
want to combine five values one two
three four five and you want to give it
as an input in the r software
then the command here is that you write
here
small c
and then within the parenthesis you try
to write down all the values so this
will combine
all the values one two three four five
and in case if you try to follow
something else i can show you with this
screen shot here what will happen
suppose if i want to combine one two
three four five in our data vector y and
if i simply give here y equal to one two
three four five you can see here the
outcome here is error similarly if i try
to give it here y is equal to one two
three four five within the parenthesis
even then
there is some error and
in case if you write y is equal to a
small c means lower case c and within
parentheses one two three four five then
you try to see this y here is like this
so remember one thing very important for
the r programming whenever you want to
give more than one values in the r
software try to use this command
lowercase c and try to define the data
vector using this command c this is very
important but before that i try to show
it to you on the r console also so that
you are clear about it you can see here
if i try to define here y is equal to
suppose 1 2 3 4 5
you can see here it is not giving me any
value and even if i try to make it here
paranthesis it is giving me error but
when i try to write down here small
letter c lowercase c
then it is assigning the values
right so this is the way and whenever
you are trying to do the mathematical
operation and other types of operation
unless and until you give the values in
this format using the
c command there will be trouble things
may not really work so you have to be
very careful when you are trying to do
it
ok
now i would like to give you one more
command this is about mode
mode mode this command mode explains the
type
or storage mode
of an object
for example
in case if you try to say here x equal
to 6 so this is a number so the mode of
x is numeric
so in order to find out the mode of a
variable you have to use the command
mode m o d e and within the parenthesis
you have to write the name of the
variable now here i would like to
caution
to give a caution to all of you that
when i am trying to use the word mode
sometime people get confused that ok it
is a mode like mean median mode that we
study in statistics because
in order to find out the mean the
command is mean in order to find out the
median the command is median but in
order to find out the mode the command
is not the mode it is something else so
be watchful in your childhood you have
learned the chapters like
modes of transportations
you say that ok there are different ways
for the transportation you go by ship go
by air go by train etc
so this mode is like that mode it is not
the statistical mode where you try to
find out the value corresponding to the
maximum frequency be careful similarly
in case if you try to
take a character like as here i try to
take a variable y in which i assign the
character as apple then the mode of this
y
mode of this apple will be character
so this is another operation mode which
helps you when you are trying to do a
programming and when you are trying to
get the data from some external sources
because you will be getting a file which
will have say thousands and millions of
values and it is not possible to go
through with each and every number in
the file to know the mode of the value
and based on that you have to do the
mathematical operations
and this is here the screenshot of this
thing i will try to show you it on the r
console also you can see here if i try
to see here x equal to here 5
then the
mode of here x here is numeric but in
case if i try to take here y here as the
apple
then
mode of y here is
character but in r
we have some more modes that we will try
to use and learn whenever we need them
they are logical integer double complex
raw character list expression name
symbol and functions
so we are not going to do it here but
then whenever you need such numbers you
can always find it out now there is
another command for this mode that is
the storage mode
this command returns the storage modes
office argument that means whatever is
written inside this parenthesis whatever
is the mode of storage of this value
that is informed through this command
actually this command is usually used
when we are trying to call the functions
which are written in
other languages like a c or fortran and
this actually ensures that the r objects
have the data type as expected by the
routine being called by those
programming languages
right
so we are not really going to use
it here much in this course but it is
very important for you to know for
example if the mode is logical then it
is storage mode is also logical when the
mode is numeric then the storage mode is
integer
or double
when the mode is complex then the
storage mode is also complex when the
mode is character then the storage mode
is also correct when the mode is raw
then the storage mode is also raw so
this is for information that you must
know actually for example if you want to
know the storage mode for example if i
try to say here x equal to 6 which is a
numeric then if you try to use the
command here is storage dot mode and
within parenthesis you write this x then
it will give you the answer double
similarly if you try to take a logical
variable which i just explained you as
small x equal to true which is a logical
variable so the storage mode of this x
is logical and in case if you try to
take care a character like as a x equal
to here apple then the storage mode is
is here character and the same thing you
can see this on the screenshot of this
software also so before i go further let
me try to
show you these things on the r console
also so that you are more convinced
right so if i try to take here x equal
to here 78 for the this is a number so
the storage mode of x is
double but in case if i try to take here
y is equal to here apple
right then the storage mode
of this y here is character and so on so
this is is needed when you are trying to
write down the programs many times for
the mathematical operations you need to
do it and based on that you try to make
your operations okay now
whenever you are trying to do
some calculations for that you write a
programming sometime
you get the answer
as infinity
and many times this creates a problem
and while you are working with a longer
program then
sometime in between at some place you
are getting the some value infinity and
it is not giving you the output at that
moment we always would like to know that
somehow
if i can find out the outcome of this
command is coming out to be finite or
infinite because it may happen that you
are trying to divide some number by some
number but somehow during the
computation that number has become so
small that it is almost zero so when the
number divided by zero that will give
you infinity so how to know all these
things so in r software
in case if you want to know whether a
number is finite or infinite
we have a command here
is dot finite or
is dot infinite
for example you know that when you are
trying to divide
3 by 0
the answer is infinity that you can see
here in the r if you try to divide 3
divided by 0 the answer will come out to
be i n f
so which indicates that infinity
right and you know that if you try to
add 5 in infinity the answer will again
be infinity or if you try to subtract
divide multiply anything it will remain
any infinity so if you want to know that
there is a variable x is equal to 5 plus
infinity
5 plus inf then i just want to know what
will the outcome so i am asking
s dot finite this x means obviously we
have given here infinity though x is not
finite so it will give you the answer
here as say false and then if i ask
is dot infinite
x
it will give you the answer true yes it
is in finite
right so and you can see here this is
the screenshot and if i try to show you
these things on the r console and if i
try to define here the variable here as
say x is equal to suppose 6 plus
infinity
right so you can see here
x is infinity and if you want to find
out
is
x infinite true
and if you want to know is
x finite answer is false so this is how
you can use such commands while doing
the programming and that will help you
right ok so now we come to an end to
this lecture and now you can see here in
this lecture we have understood
a variety of things
and these are small commands which are
trying to give you some information
about the intermediate steps also so
my request will be that you please try
to go through these commands try to
execute them and try to understand what
are they trying to do that is more
important
and i am not saying that these are the
only commands which are
possible in the r software there are
many more commands
but i expect that if you know these many
commands after that if you want to know
more commands and if you consult any
book
or any source
goods source it should not be a
difficult thing for you to learn at
least you will understand what is really
happening
once you know what is this s dot
character or s dot character after that
if you come to know that there is a
command like is dot matrix or s dot
matrix you will automatically know that
ok it is trying to understand the
character
or the characteristics of this variable
where whether this is a matrix or not
so that was my objective to choose and
to give you some representative commands
so that
your further learning in the r software
become easier so you try to practice it
and i will see you in the next lecture
till then good bye
[Music]
[Music]
[Music]
you
