R Programming For Beginners | R Language Tutorial | R Tutorial For Beginners | Edureka

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is me from ADA Rika and welcome to the session on our programming now let's not waste any time and let's move forward and look at today's agenda and get a good understanding as to what we'll be learning in today's session now we will start off by a simple understanding of what a variable is and the various data types that are supported in our from there forward we'll be looking at the various data operators the conditional statements the loops and then we will be slightly deep diving into strings and functions now this is a completely practical session but we will be looking at some of the theoretical part as well so I hope you guys are definitely looking forward to today's session can you give me a quick confirmation in the chat box okay so I've got a confirmation from Jude as is with Charlie Terry the pure Benjamin that's great to see guys okay Benjamin is asking me what do I do in case if you have a dot okay this is something that I want to clear out at the start of the session itself at any point if you're not clear with respect to any concepts that we discussed as far as this session or there's some place that you're getting stuck it then please let me know just put across your problem in the chat box and I will be able to assist you right away so make sure at any point if there's something that you want me to repeat as well that you let in does that answer your question Benjamin okay that's great to see so let's move forward and first begin by installing our on our system now to do that you just need to go to the crown website and download ah let me just show you how to do that so you can open Google and search for download are so you can go onto the first link and you will get the direct link to download our four window space so Graham basically stands for Palm Pre until our archive network so they extensively work on development with respect to our so you can either go download it from the first link or let's say if you're using a different version that is the line X or Mac as your operating system then you can go onto the website cran dot our - project oog and then you can download the corresponding version as well now for Windows in case if you are on this page make sure you're downloading the base version and not any other version so we have four different versions here so just download the base version here ok so just click on the link go ahead and download it onto your system and the installation is quite easy you just need to click on next and once you've completed your installations please let me know so that we can move ahead ok so now that all of you have successfully downloaded and installed our on your system there is another tool that I want you to download and install that is the R studio now the purpose of installing our studio onto a system is to help you have an IDE that will make it easier for you to program and work with us so to download our studio just go back to your browser let's go to a trusted friend Google and search for a studio so just click on the first thing here again and you will be redirected to the spit so there are basically four versions of our studio I would recommend that you go ahead and download the desktop version of our studio which is free just click on download here and you get the complete list of our studio versions and you can choose the version that suits your operating system and then go ahead and download and install it so again this is a quite straightforward installation you just need to keep clicking on next so please go ahead and install our studio as well on your system so that we can move ahead while you guys are simultaneously trying out the code when I'm explaining it so please go ahead and download and install our studio ok so this is how your our studio looks like but let's keep it aside for now and we'll come back to a presentation where we will start off with some basic knowledge with respect to our before we actually deep dive into it now the first concept that we'll be talking about is what exactly is a variable now before we actually move into the slides I just want to have a simple understanding of how many of you are familiar with the term of variables or how many of you are actually aware of what exactly is a variable from your understanding of different programming languages of going variety of answers here from most of you some of you are saying it's a value that you're using some of you are telling value stored in a memory location okay there seems to be some lack of clarity here let's so let's move forward and actually understand what exactly is a wave now we would basically is nothing more than a result memory location to store your value basically what it means this every time you need a variable in your program then what happens is you want to create the variable at the times execution it identifies that this variable is of this datatype and correspondingly a memory location is allocated for each variable according so again it is a value that is being stored in a specific location is that clear okay so let's look at the image below here now basically you're taking three different kinds of variables one is actually a numerical value the second is a string and the third is actually a boolean kind of bag now don't worry about what exactly these datatypes up here it's just to help you understand how when you're using a variable correspondingly a block of memory is allocated to it based on the data type because size of the block slightly weighs but again each variable get its own memory location allocated for so now comes the most obvious question as to what exactly is a data type in our now talking about the data type data type basically helps you classify the value stored in a variable now why do we need to classify this is basically to help you perform different kinds of operations now let's say it's a numerical value then you can go ahead and perform various arithmetic operations you can perform with these logical operations relational operations but you cannot do the same when it's a character value that time there is no correct sequence or a correct way to perform arithmetic operations between two characters now this is the exact reason why we need to classify the variables based on their different data types now if you take a look at the image below here you can see although we're creating three different variables they each have a different data type that they belong now let's say X is equal to 50 now this can be considered to be an integer type but when you say Y is equal to 3.14 to eight five seven this is considered to be a numeric buy and finally when you say B is equal to true then this is again being classified as a boolean value now how many different data types are present in R and how are they different let's move forward and try to understand them as well now there are basically five different data types present in R you have vectors you have matrices arrays lists and data frames so let's move forward and discuss each one of them one by one now we will start off with understanding vectors as passed now a vector is basically a collection of different data elements that belong to an atomic that again what is an atomic type you have five basic atomic types present or as they better known the five classes of vectors the first is logical now logical datatype can either take a value of true or false and then you have integers now integer takes a whole number value whereas numerical datatypes take both whole number as well as decimal partner apart from that you have complex variable and you have a character variable as well wherein you can store a single character or a sequences of words as well so are you clear with respect to these five atomic types okay now when we talk about a vector as a whole either you can have a single element belonging to one of these data types or it could be a sequence of elements as well both of them are a vector so let me go to the ass video and show you these five data types as well as how you can work with vectors so we have our asked to do here now just to give you a simple introduction this is basically you're a scripting area so the scripting workspace is something that you can use when you need to write multiple lines of code and let's see if you want to execute a single line of code then you can use the console area as well now every time you declare and work with available it gets automatically updated into the environment area and let's say if you want to install various packages work with different packages or you're not clear with something then you can use this workspace to get a better understanding and have a clearer image now again even if you plot a graph this is the workspace where you can see it for now let's try to understand the different atomic classes of vector so to command anything in R you can use the hash symbol so first we are talking about vectors let's begin by looking at logical data types now again to create a vector you just need to specify the name of the vector so I'm going to call it VTR 1 then you can use equal to and then you see and inside quotes now inside quotes you can pass any value that you want now a logical value can always be true or it can be false now always remember true and false need to be capital always now if you want to just execute it let's select this statement and you can go to run or if you have multiple statements that you want to execute at once then you can go to click on source this will directly completely execute the whole code written in the script page now I just need to execute the single line so I'll just click on run and a new variable has been created so if you see here a new variable has been created of logical data type and it stores two values its true and false but how do you know which class it belongs to to always know to which data type a vector actually belongs to you have a simple command that is class and then you just need to pass the variables name so in this case let's say VTR 1 as you select the statement again and let this run at intuitive so here you can see the output in the console area it says the class which vector 1 or the data type 2 which vector 1 belongs to is logical so here again you can see there are two values that I am passing to vector 1 now let's say I am going to create vector 2 as well ok here I just pass a simple numerical value let me say fee of 15 a five point six seven four nine five four and then I'm gonna even pass nine nine nine nine nine nine okay so I'll just select this and run it so a new vector has been created you can see that in my environment workspace now if you want to see the value stored inside a specific variable all you need to do is specify the name of that variable now you can do that in your script workspace so if you write it directly into your console then you can see the corresponding value as well now if you look at the format shop the value for each of these having changed this is mainly because the range of numerical value that it can store a numerical value that is greater than seven digits can will always be converted to the exponential form now since a specific value in that vector that is nine nine nine nine nine is greater than the range of numerical value all the values present in that vector are being converted to the exponential value so this is something that you can always remember but let's say I'm going to reduce this value here now if I just update and run this then you can see the value present here 15 was the whole number which did not have a decimal pointed value but since 85 had a decimal pointed value everything gets shaped so always all the values are checked in the vector and only then are they stood so are you clear with respect to logical and numerical data type okay so Judas asked me how is integer type different from numerical type that's a very good question Jude now again this comes back to the same point that I had discussed earlier integers cannot store a value that has a decimal point associated with it so to store decimal point value you have the numerical data so let me show you integer as well but 2/3 is equal to C of let's say you want to store 35 now if you stressed part 35 then what happens is this gets treated as a numerical value but with this value if I just pass a capital L then it will treat it as an integer value 35 54:58 and 146 so always when I am passing L along with the numerical value it treats it to be an integer so let's say I want to print out a numerical value of vector3 so you have all the values and I will say class off vector 3 then it's telling me the value is integer now coming back to your question let me just create a new vector vector 4 2 which I'll assign C of 58.4 6 5 and I will put an L along with this as I had mentioned earlier if you pass L along with a numerical value it treats it as an integer but what happens if you pass L along with a decimal pointed well as you show you that so here you can see when the compiler is trying to execute this code you can see a warning message has been passed saying that the integer literal 58.4 6 5 L contains a decimal point and it's suggesting me to use a numerical value rather than an integer so are you clear dude ok that's great to see there's another question from Charlie Charlie's ask me what happens if I try to store different data types in a vector okay that's a very good question Charlie let me show you what happens let's say I'm going to create vector five okay equal to see here I'm going to pass true which is a logical value then I will pass an integer value let's say 35 I even pass a decimal pointed value let's say 3.14 so who can tell me what will happen if I to execute this okay let's see what happens it gets created but let's see what value is actually being stored in vector five - you can see here is considering the value true to be won and dis converting everything to decimal pointed back now I'm going to create a new vector let let me call it vector six where I even include a character string along with it so see now again any character string can be put into a single quote or a double put it treats both to be the same and it's store using a double quote in R so let's say hello then I'll pass logical value false I'll say 65l so basically this is a combination of character it has logical value and it has an integer value let's run this and it gets created still there is no error that gets generated but what happens when you try to print vector six so here if you can notice everything is being stored but they are in double quotes that means what happens is that every value that is being passed along with the character value is being considered to be a character so hello is a character false is a character and 65l is a character to get a better understanding let's use the class function where I am passing the vector five now if you remember vector five had logical value it had integer value and it had a numerical value if you execute this you can see it considers everything to be a numerical value and in case if I check with respect class of vector six then it's treating everything to be character even though there is a logical value and an integer value it is considering all of it to be character so again in case if you are creating a vector that has different datatypes then it actually converts all the variables having different datatypes to a common data type so are you clear charlie okay that's quick so this is another we have talked about the five different classes of atomic vectors and we've seen vectors as well let's move forward to the next data type that is matrix now basically your matrix in R is quite similar to your arrays in other programming language now here you have arrays as a different data type itself but you'll understand it better if you are able to relate it to those of you who are not familiar with what exactly an array is with respect to programming language an array or a matrix in this case is a two-dimensional rectangle layer basically it's similar to an actual matrix that you are using in mathematics where you storing different values in a orderly manner now the syntax for declaring a matrix is quite straightforward you specify the data type that is matrix followed by that you specify the data the number of rows number of columns now by room basically is a logical cue that you say so you actually use by row to order the input vectors in a specific manner and it div names basically I use to assign names for your rows and columns so let's go back to our studio let me clear all this and let me show you how to create a matrix now first again let's try to name our matrix I'm going to call it MTR and then empty are equal to now again as I had mentioned you need to specify the data type so it's of a matrix type now here you need to specify the data that needs to be stored inside a matrix now I can store a random value that is there but what I'm going to do is I'm going to create a new vector and this vector basically is going to have a sequences number starting from 5 and going on till 30 so there are basically 25 elements ok now when you say 5 : 30 that means I need to store a sequence of numbers starting from 5 with a constant increment of 1 going up to 30 that is 25 elements now my number of rows are 5 my number of columns are 5 now I do not need to arrange them in certain way nor do I need to specify the names of the rows and call so this is it you can just go ahead and run it so here you can see the problem this phase in a matrix of 5 cross 5 I cannot store 26 elements that means it's greater than 5 so one thing would be is you can either reduce the value here to 29 or you can update the value here as well so let me just reduce this and let me try rerunning this you can see successfully it has been executed so let me show you how the value looks like so again MTR and I just ran it so you can see below how a matrix is being stored here so five six seven eight nine ten and correspondingly the sequence of being filled in a column manner and this is quite similar to the matrix that you have seen in mathematics so any doubts on how to work with matrix on how to create one you can go ahead and try creating a matrix and if you have any problems or any doubts do let me know in the chat box okay so I've got a confirmation from Benjamin Terry's the PRF swetha charlie that's great to see that you guys are following and trying it out simultaneous understood what a matrix is it will be easier for you to understand what exactly is array in R now your array is quite similar to your matrix now it can store the data in more than two dimensions what do I mean by that is so if you look at the example here it will be easier for you to understand not only are you creating a 4 cross 4 matrix but you're also storing it in or 2 cross 2 matrix so basically you're storing 64 elements but they are again in turn being stored in four different 4 cross 4 matrix so your array in R is something that can store your data in more than two dimensions here so I hope this example was something that I had given you a simple idea on how an array is different from matrix and how it is in R okay so let's go back to our a studio and let's try it so let me go ahead and create an array called arr then you need to specify the data type that is array then comes the value that needs to be stored in the array so I'm going to create a new vector starting from one to store to nine then comes the dimension of the array now to specify the dimensions you need to use time keyword then you need to use a vector now in your vector the first two columns are going to specify the standard size of each matrix that is going to be stored in an I so I'm going to each store it in a 3 cross 3 matrix then what I'm going to do is I'm going to store it in a foo cross to a ring so I have a 3 cross 3 matrix that is again in turn going to be stored in a 4 cross 2 array okay so let me just run this and if you try to print array as enlarged this for your understanding so here you can see the values so this is my first matrix this is my second matrix this is my third matrix now all these three belong in the first row so you have the corresponding column number and we go similarly the fourth column of the first row also stores the same then you come down to the second row and you have four similar divisions so I you clear with respect to how we work with an array and how to declare and view an array any doubts with respect to array okay so Agatha has a good question here Aditya has asked me what happens in case the number of values is less than the size of five that's a very good question idea so here what I lose let me just change it to six so if you look at our first matrix it's storing all the values 1 2 9 in the column fashion now I have only a range of starting from 1 going up to 6 now with the same condition let me disagree run this and I'll try to print array so what you can see here Aditya is that if your array or matrix does not have enough values to fill the matrix or array completely then what happens is it takes the same input vector and start inserting elements from the star now I needed to store nine elements in my vector but I had only six values so I have inserted the first six values column wise then I have again started inserting the values from the start of the vector so the first three values are stored in my first matrix which is stored again in the first row and first column of the array then the corresponding next values have been stored the next matrix which stored in the second column of the first row have you gotten an idea about this Agia is a question clear a detail that's great so any other questions all right so let's move forward and talk about the next data types that are lists you lists are quite similar to your vectors itself but the only key difference between your list and your vectors is that you can store elements that belong to different data types in lists without actually changing their data types now we had seen this problem with vector where you is trying to store character value logical value and a numerical value and in turn they're getting change to a character value and in our first example if you remember we had tried to store logical value and integer value and a numerical value which in turn got converted to numerical value olara so with respect to lists you can store the elements which belong to different data types and their data originality is - now let me go ahead and show you how to do that so let me clear this as well now let me begin by creating two vectors so let's say we do 7 equal to C off say I have numerical value let's hit five point six seven eight then I have 32 95 31.6 and I have another vector V tr-8 which has character values so hey how are you buying cue okay so I have these two vectors that are need to be created so this run this and they get created now what I'm going to do is I'm going to create a list say call it my list which is going to be a data type list so again list and here what I'm going to do is I'm going to pass vector seven and vector eight if you want I can even pass vector one which has logical value vector one and then let me just execute this command and they get outdated now let's check what my list looks like now if you look at this all the numerical values are being stored together you have the character values and you have your logical values also being stored none of these values are being converted to a common format and all of them are actually retaining their original data type now in case if you want to store different data types and retain their original data types then you can go ahead and use lists so I clear with respect to lists any doubts okay now the last data type that we'll be talking about is a data frame so basically your data frames is a table or a two-dimensional array like structure that can actually store your data in an ordered manner now again let's say you're working with a table that is stored in an excel file or you are loading a table from a database then you would be directly loading it into a data frame and then working around with now let's go back to our our studio and let's first try creating a new data frame and then I'll also show you how you can work around with the data frames that are present in our so let me just clear all this I can also go ahead and clear the variables in my memory so we'll begin by first creating three vectors from stock so my video one is going to have let's say value starting from one and all the way up to five so six value is there then vector two is going to have names let's say I start off with mind I'll put in dude let me bring an item as well dear Shwetha okay let's keep it just five names here okay and along with this let me create a nother vector that's vector3 and here what I'm gonna store is let me just do a new random numerical values so let's say teen comma 25 comma 65 comma 1 45 and 74 okay so basically I have three vectors here so let me run this and you can see three vectors have been created now what I'm going to do is I'm going to create a data frame to create a data frame you can use the syntax data dot frame and then pass the values that you want to be in your data frame so I want my vector 1 vector 2 and vector 3 so just from the statement again you can see your data frame present here from my vector 1 1 2 5 has been stored correspondingly to that the first element from my vector to that is meal has been added and the corresponding first value is being taken from the third vector and it is also being added so each of the first element from three vectors have combined together to form the first row of my data frame so is this clear on how a data frame works okay so let me show you how you can use a data frame of an in-built table so this call for data frame now let's say I want air quality now I equality basically is a table that is already present in my a studio so if you just run this you can see the complete table being stored as a data frame so here again you have the ozone you have solar ratio you have the wind temperature a month so this is basically the quality of air measured over different time frames in a span of one day so every day you're measuring these four quantities and then you're putting it as part of your table so this is something that will give you a good understanding as to how to work with the data similarly you can import a table you can import an excel file and then directly store it into your data frame as well so we'll be seeing about that in our next class where we'll be working with the analytical section of that but I hope you get a clear with respect to what is a data frame and how you can work around with it can you give me a quick confirmation in the chat box ok so I've got a confirmation from our the PRF terraces here so does youth Rita that's great to see guys so now let's move forward to the next concept of today's session there are operators in are now talking about operators there are many four different operators but what exactly is an operator operators are basically construct which help you manipulate the actual value of each of the variables so using an operator you can perform various operations and change the value stored in each of the variables now again as I mentioned there are mainly for different types of operators you have automatic operator your relational operator logical operator and time and orbit so let's move forward and look at each one of them one by one now talking about arithmetic operators you have addition subtraction multiplication division these are some of the common operations now I am quite sure that all of you would be familiar with this I guess we come back here to make clear all this let's say print six plus nine point eight let us run this so basically you're getting it output similarly if you want you can change it to any other you can say division so it's giving a corresponding value and so far now apart from normal arithmetic operations of addition subtraction and division we have three new operators here the first is modulo operator now in a normal division when we perform division we get the result to be the quotient of that type but in case of a module division what happens is you get the reminder now to specify that it is a modular division you just need to use to personate symbols and it gets converted to a module addition if I run this you can see the difference earlier it had given me a division here it has given me the reminder now let me give you a better example here 22 divided by 7 gives me the value of pi' now I'm quite sure all of you are aware effect now same thing let me just change to modulo division and I will rerun this now when I rerun this you can see the output to be what although a division is being performed here but instead of the quotient like you normally get you're getting V reminder you clear with respect to what modulo division is oh that's great now apart from modulo division you have your exponent operator basically if you want to raise any variable to a power then if you use the exponent operator let me say I want to power 7 then you can use the exponent operator and you can run and get the corresponding value now finally you have your flow division now flow division is something that is quite interesting what happens in flow division is that you divide a number and if the quotient that you're getting as a result turns out to be a decimal number then it is actually rounded off to the previous whole number now to explain this to you what I do is I just modify this I will make it again 22 Flo / 7 okay now we know that when 0/7 actually gives us the value of 3.14 - 8 5 7 and 22 modular division 7 gives us the output of 1 but what happens in case of a flow division you get the output to be 3 so what happens here is the value that you actually get is 3 point 1 4 2 8 5 7 but since it is a flow division it gets rounded off to the previous whole number that is 3 so I guys clear with respect to how a flow division works - with us asked me what if the decimal point value is greater than 5 ok that's a very good question so let's try something out let's say 3 point 9 okay flow divided by 2 what is the answer for this yeah it's one point nine five so if I run this you can see the value to be 1 even if it is point nine nine nine nine nine it still gets converted to the previous whole number so does that answer your question swetha all right so apart from arithmetic operations you have relational operators now relational operators are usually used to compare the actual values of two variables and then the output that you get is actually a boolean value let's say you have two variables a and B now if you want to know which is greater of the two then you need to use the relational operator greater and you need you can check if a is greater than B if a is greater than B then it returns an output to be true if it is not it returns an output to be false similarly you can have less than now apart from your greater than and less than you also can check if a corresponding value of a variable is equal to or greater than or less than that is if you look at the operator here if let's say you want to check if a is either greater than or equal to B then you can use the greater than symbol followed by an equal to and let's say if you want to compare the value of two variables then you need to use double equal to and only then will it compare if you use a single equal to it becomes assignment and for checking if it is not equal to you can use not equal let's come back let's say we're equal to 25 and y1 equal to 60 now let me print greater than where one run this we put is false no the value of your variable where is not greater than variable var1 but let's say if I am checking if they are equal to as well so again double equal to and if you run this it says false are they not equal to each other so for that an exclamation mark followed by equal to gives you the output to be true that means the value of variable is not equal to variable 1 ok with respect to the relational operators okay so others ask me where do we normally use relational operators so that's really good question RJ you will be using relational operators and logical operators that we'll be seeing next as part of your conditional statements now what a condition of statements I request you to be a bit patient because we will be discussing that in the upcoming slides and if you are not clear at that point then we can come back and answer your question all over again is that okay Jay okay that's great so going forward from relational operators you have assignment operators now again assignment operators are of two types so till here you have seen how I have created variables and assigned them using the equal two apart from that you can always use the lesser than symbol followed by a hyphen now this also means that you are assigning a value to a variable and this can also be done as per left assignment and right effect so what this basically means is that you can either write the name of the variable to the left the assignment operator and then the value to the right or you can also write the value to the left following the assignment operator and the name of the wave so both of this is accepted and you can use an equal to or the lesser than followed by - symbol to assign the value to a variable are you guys clear okay so after the assignment operator comes your logical operator now there are basically three logical operators that we'll be discussing you have your logical and logical or and not operator what does your logical ion do is that it actually checks the value of both the variables and then it gives you a corresponding output now you get the output to be true only and only if both values are true in case one of them is false then the output is considered to be false in case of your logical act but in case of your logical or at least if one value is to be true then the corresponding output would be true now I am quite sure that most of you would be confused so what I'll do is let's go back to our studio clear all this I create two values value one which is again a vector having true and false true and false I'll also create another vector value too that will have value false true-true again and finally false what we're going to do is let's see the output for this print value 1 and value 2 so let's execute this code now so here you can see when I am trying to check the only case the output is coming to be true is when both the values are being true so each value is being checked with the corresponding value from the second vector and it is decided if both values are true only in that case is it printing out a value to be true but let's say in case of an or operator there is this one this entry so in that case only where you are dealing with two false statements is the output consider to be for all other cases are coming out to be true apart from this you also have your logical or wherein if I execute this and fee then only the first values present between both vectors are checked and the corresponding value is decided so if I am using a single and or a single or it is an element wise and or an element twice or but if I am using double or or double and then it is a logical and operation or a logical or operation so here you can see where I am using a logical and operation then it is second we first two values and since both values are not equal to true it is giving me the output of false so any doubts with respect to the logical operators oh yes we missed out on one operator that is the not operator now not operators quite straightforward let's say not of value one that all the values have been reversed where I had to as the first value in my value one it is getting replaced to false where it was false it is getting changed to true so any doubts with respect to the logical operators so this is the element wise and element wise are we have seen the logical and logical or and finally we've also seen not operator okay so I've got a confirmation from Charlie AJ the theory that's great to see guys that you're following so please make sure you're simultaneously trying out these quotes as well so that you will have a complete understanding so moving forward let's look at the conditional statement and this should answer your question as to where we're using relational operators and logical operators now talking about the conditional statements there are mainly three types of conditional statements that we'll be dealing with and an extension of a conditional statement so basically there are four different conditional statements that we will be using now the first conditional statement that we'll be looking at is the if statement now what if statement basically does is that it checks a specific condition and if the condition is found to be true then it goes on to execute a set of statements but if it is not if you ignore these complete set of statements and move for now how does this work let's go back to our our studio let me clear all this let's say we're 1 equal to 25 well 2 equal to 35 now I'm going to use the if statement if we're one plus we're too is greater than 50 so you can figure I'm using a relational operator along with the condition statement as well so coming back to your question this is where we completely use relational operators as well as you'll also be seen logical operators in the upcoming slides now in case if they are greater then I am going to say print now if you want to print a string put it in quotes value is greater than 50 and select all this and if you click on run it says value is greater than 50 but let's say I make it 100 now if I just run this segment of the code then it does not print anything that basically means that the statement written inside the if condition gets ignored I guess clear with respect to how the if statement works okay so I've got a confirmation from everyone that's good to see but let's say you have more than one condition that needs to be checked what do you do in that case in that case you can actually use an else--if statement now else' statement basically can be used for helping you to check multiple statements and check their corresponding validity let me just show you how to use the elsif statement as well now first if condition is checked then let's say else if r1 plus r2 is greater than now it's checked the first condition whether it is greater than 100 or not if it is not found to be true only then will it come to else if condition let's say 75 in this case then I just copy the statement value is greater than 75 let's say you want to add even one more condition to this then just copy this else if let's say 65 in this case so again you can see here we are putting multiple statements here and there order of checking or the order of evaluation is in a sequential manner first it will check if it is greater than 100 if it is found to be false it will go into the next condition it will check if is greater than 75 if it is false then we come down to the third condition and see if it is greater than 65 I'll select all this and let's run it so none of these three statements have been executed so it's not it's checked all these three statements and it's found to be false finally let me just write one more condition elseís and copy this completely and here I'll just change it back to 50 now we know this value is greater than 50 and I'll update it inside as well now let's execute this infi again finally it is evaluating all these preconditions before it and finally it's found a match and that's why we are getting the output so I guys clear with respect to how the elsif condition' works yeshuaa that's a very good question so Schwitters asked me is there any limitations on number of cells if I can write multiple you can write any number of conditions but rather than writing multiple conditions that you want to check what you can do is you can make use of logical operators and then valuate these expressions okay so that's great to see that all of you understood the if and the else is part but let's say if none of your condition meet then what you do and in such cases you can always go ahead and use the L state now your L statement is a conditional statement that definitely gets executed if no other condition is met now if you go back to our studio let me just remove this condition the final condition and let me just put in an else statement here else I will say print number is less than 65 okay let's try executing this so here you can see all these conditions were checked and since none of them were found to be true then you go into the else part and the statement return inside else gets executed so this is same that has been diagnosed a presented here the penis give a simple explanation here so if you see always we start the first thing that you do is check the if condition if it is found then you execute the if part it is false then you go to the else if conditions each of the else's conditions is validated and whichever condition is found to be true then the corresponding else's code gets executed if none of the else's conditions are also found to be true then you go to the else part and execute the else code and finally come out from the selection sex so I you guys clear with respect to the if the else's and the else statement now please make sure you're quite clear with this because this is a very strong fundamental concept and is one of the very important concepts that you need to be aware of with respect to any programming language not just our okay so I've got a confirmation from everyone now the fourth condition statement that we'll be talking about is this switch case statement now in case of the switch case statement basically what you're doing is you're already aware of you value that the expression is going to take now once you know that then in such cases you can go ahead and write a specific statement now how does it vary from a first statement now the major thing there is the knowledge of the value that the expression takes now you cannot use an uncertain value or a range like you have done with the if-else statements here you need to specify the actual value that the expression can take and then you can specify the corresponding state now what I'll do is let's go back to our our studio you means clear all this let's take a simple example let's say switch with respect to expression I'm just going to give you a value of 4 and then let's say in case the value is equal to 1 then print Monday so I hope you guys get what I'm trying to do for the number value that it could take I am specifying which day it is going to be equal to print Tuesday wetness tea Thursday v is equal to Friday six Saturday finally 7 is equal to Sunday okay so before I go ahead and execute it let me just still make certain small modifications respect to this I'll put the values in quotes basically it makes it easier for the evaluation to take place now I'll just execute it and you can see the value is being printed as Thursday so what has it done it has first checked the value of expression then go on to check on the various value that the case can take once it's found the match then it is gone ahead and executed the statement associated to that case and always make sure you put comma after you've completed the statement for each case because this is how it differentiates that the next cases about the star now in cases there is no match then we can use a default statement that case what I lose I just write print invalid input so now that I have specified the default value let me go ahead and change this value let's say it's going to take percentage as a special character so it does not mean any of the cases if you try and redirect this then what happens is you get the output us invalid it that is again as you remember we had done with else case if no other condition meet then the else code is written here if no other case matches then you beast the default state so any doubts with respect to the switch case statement or the e first statement any of the control statements that we have discussed till here from Jude swetha AJ Adithya that's great to see so going forward let's talk about the next statement that our loop statements talking about the purpose of loops in our it basically is quite similar to any other programming language the main objective is to help you execute a group of statements multiple number of times rather than rewriting them multiple times so this in turn goes on to reduce the code redundancy and also make the performance of the program better now talking about the different kinds of loops in our you have mainly three tens of loops the first is repeat loop the second is why and the third is for now although these three perform the same operation that they help you execute the statement multiple number of times but how they do that is slightly different with respect to each of them so let's go forward and try to understand each one of them one by one starting from the repeat loop now the repeat loop basically helps you execute a group of statements any number of times until the condition that you have specified inside it turns false and this condition is always checked after the statement is executed now talking about the flow of a repeat loop statement you always start with the execution then you go on to execute the code block and then you check the condition if the condition is found to be true then you exit from that loop and if it is false you go on to execute the loop n number of types until the condition is found to be true okay so let's go back to our studio and try this up let me clear all this so let's begin by first creating a new variable let's say variable var1 okay which is equal to 5 then what I'll do is I'll use the repeat you now here let's say I want to print every odd number starting from 5 to 21 so in that case what I'll need to do first is print the variable 1 to know the current value so we are 1 then I need to update the value of variable 1 so we're 1 I need to make sure it achieved odd numbers only so wire 1 is equal to where 1 plus 2 now I need to define the condition which will help me leave from the slope so in that case let's say if my wire 1 is greater than 21 then you need to break from here so break so I get killed this as to the logic of the repeat loop as I explained at once again so we have started by declaring a variable equal to five then we have printed the current value of the variable we have appended it and if it crosses 21 then it needs to break so let's go ahead and execute this and here you can see it's printing 5 7 9 11 13 15 17 19 and 21 so once it has reached the value of 21 it is leaving from the loop now let's say if you had to write this manually then you just need to think so just think about these steps now this loop is running for 10 times now if you had to write these steps manually you need to write these two statements at least 10 times to get the corresponding updated values and just think about it 10 times you're writing the same set of statements that increases the code redundancy and apart from that also it tends to take up more time for execution of that code so definitely the main purpose of having loops is to help you reduce your career agency and as well as increase the execution time of hair code so I guess care with repeat loop okay so I've got a confirmation from everyone now let's move forward to the next loop that is the while loop now while loop is quite similar to your repeat loop it says you need to specify the conditions and it will run till the condition is fulfilled but the major difference between your while loop and your repeat loop is that the condition which determines whether the loop has to run or not is Rutten first in case of a while loop so always before you begin execution of that code what happens is that it checks whether the condition is true or not and only then does it go forward and enter the loop condition so if you look at the flow for the while loop you start off with the execution you check the condition if at first it says the condition is found to be false then you exit from that loop but if it is found to be true you go on to execute the code block and then keep on repeating until the condition is found to be false okay so let's go back and here what I'll do is I'll write the same code let's say while then the condition so here I need to mention pad 3 has to be less than 21 so here we'll check where 1 has to be less than 21 now in our earlier statement what we had done was we were checking when the value of one increases beyond 21 and then we're breaking but here we're going to enter the loop only and only if the value of wire one is less than 21 if that is true then go down and I'll just copy this so this logic is going to remain same and that's it so I'll just update the value of r1 equal to let's say five select this and let's rock now here you can see the difference earlier what was happening was even though we had achieved the value of 21 it was getting printed and then it was breaking from that rule but in this case what is happening is that before it achieves the value of 21 it is exiting from that loop the condition was same in both places but it was just that how the loop statement differs now to give you an even better example what I'll do is I'll just update this value let's say 22 here and I'll run this now in this case you can feed 22 is getting printed that means it has entered the loop and then it is going forward and executing this logic present inside but only after it is executed the good is it checking the condition so therefore with the repeat loop it executes at-least once but same thing let's say if I modify with respect to my while loop I just run this section here you can see nothing is getting printed that means it's not even entering that you so this is how majorly your repeat and while loop refer your while loop is actually an entry control loop and your repeat is an exit control loop because your while loop defines when you enter into the loop statement and you repeat defines as to when you leave from that loop statement ie clear with respect to the while loop and how it's different from the repeat loop okay so I've got a confirmation from AJ charlie dude Rita Aditya it's great to see that you guys are following now comes the final loop that is the for you and how the for loop differs from pure repeat and while loop majorly is its purpose of usage now in a scenario where you're not sure as to how many times you need to execute the same code you can go on to use the repeat or the way you but let's say you know how many times the statement written inside the loop has to get executed in that case you can use the for loop for loop is a loop that you use when you know the number of times the statement has to be executed now if you look at the flow what happens is you start with an initialization that is you the initial-value go on to check if the condition is true or not if it is false you exit but if it is true then you go on to execute the code block once that's done you update your value and then you want to repeatedly check the condition let's go back to our our studio and try out the for loop as well now let's take a simple example let's say I want to print the first 25 numbers in that case what I can do is I can say for my X that is a variable in the range 1 to 25 that means starting from 1 till 25 just print the value of x ok so I just select this I can go ahead and run it and you can see starting from 1 all the first 25 numbers have been printed so since I know I need to print the first 25 numbers I'm going with before you so any doubts with respect to the 3 loops that we have discussed they repeat the while and the for loop any doubts with respect to them ok since all of you seem to be clear with respect to the loop statements as well let's go on to talk about the next concept that are strings now strings are basically your characters itself but here any value that you write between a single or a double quote in R is actually treated as a string so let me show you how to declare a string let me take STR 1 and I put it in chemicals let's say hey RS fun print str1 as well similarly it is copy this and McGrath er - and I will put this inside double quotes now I'll select all this and I will run this so here you can see even though they are returned in single quotes and double quotes they are not they're all treated the same now while you are working with shrinks there are certain things that you need to remember now one major thing that you need to remember while you're working with string is that you cannot insert a double quote within any string that is actually declared using double quotes that is let's say you're defining a string inside double quotes then in between that you cannot use double quotes similarly if you are working with a single quote then you cannot put a single quote between double quotes but at the same time let's say you want to put a single quote between your string then make sure it is enclosed within a double quote similarly vice versa if you want to include a double quote then make sure your string enclosed in a single quote okay remember that okay so moving forward let's to look at some of the operations that you can perform using strings now let's say you want to join two strings or better known as concatenation then you can use the paste function now let me come here and since I have str1 and str2 already defined I'm going to create an STR three which is going to use the paste function and here I'll pass Str one and str2 then let me just print str3 now if you select this and let you run this then you can see both drinks have been concatenated and added to the new string so this is basically how you concatenate two strings as part of our now apart from this if you want to find the count of an STR you have the N character function now n character function is quite interesting function let's say n care of STR 3 this will give me the total number of characters present in HTR three so three letters for hey one for comma one for space that is five six four are seven is another space after that you have to for kiss that makes it nine space is again ten following that you have fun which is 30 now the first string has 13 characters the second string will also have 13 characters and the space between both is 1 so that means I have total 27 characters now let's say you want to convert the characters present in that string to upper case and lower case so in that case what I use this I'll use new string string 4 which is equal to 4 converting two of the case you have to upper where I will pass my STR 1 and I'll create another string that is STR 5 which is going to have the lowercase so for that again you have to lower so in which I will pass my STR to finally come sprint therefore and STR five so let's execute the section so here you can see the first string that is getting printed is completely in uppercase the second one is completely in lowercase in your original string you had your head capital and our capital rest everything was small but when you see everything is converted into small or everything is converted into capital but let's say you want to separate or you want to take a certain part of a string so for that we actually have this substring option now let me create STR six which is going to use this substring method so substring where I am going to pass my STR 3 as the parameter or the string from which I want to extract now you need to specify the position from which you want to extract this substrate so what I'll do is I'll say 5 comma 16 okay and then I'll print STR 6 let's execute this and you can see here from the fifth character till the 16th character it has been extracted and stored into a different story so any doubts with respect to strings okay I've got a confirmation from Jude I'm PR a gyrator charlie that's great to see so now I'm going on let's talk about the last topic of today's session that is functions now functions are basically a block of code which can be used with respect to different purposes but again they help you to achieve code redundancy and they in turn also help you to achieve different performances now again if you look at the examples here let's say I want to add different values first I want to add three and five then I want to add six and nine then I want to add one and five so what happens is I need to write B statement zip it now when I'm writing in a loop I need to identify a certain parameter based on which the change is going to happen but what happens if they are discrete values and I need to perform the same operation in such cases you can definitely go ahead and make use of a function now here what happens is I am passing three and five in the first turn that gets computed from my function and it returns me a value of it similarly next time I'm passing six and nine which returns me the value of 50 finally I am calling one and five which again goes on to return me the value of six now with respect to the different types of function in our there are mainly two types the first is predefined function which are already present in our and we have been using them so good example of that would be your print statement your substring method that we have just seen your paste method so all these methods are already defined in our apartment that a user can also go ahead and define a function so let me show you how it is done let's go back to our studio a clear everything for every function one the most important part is for the function to have a name so what I'm going to do is I'm going to write a function that's going to compute the Fibonacci series based on a value that I specified now my function doesn't know how many times it has to compute it so I'm going to call the function so the first thing you want to start off with by naming a function so I'll call it Freebo then function and here comes your parameters so I'm going to say in a then open your parentheses and then you can go on to write the logic for that function now here let me begin by declaring two variables where one which is equal to zero I have wire two which is again equal to one then I'll print we are one I print fire too as well and then what I'm going to do is I'm going to write a for loop where it starts where my ex actually belongs in the range one took a that I am getting as input and then I am going to create a new variable where 3 which is going to be equal to where 1 plus where to once I have computed this I need to print 3 and then append the value of where one and where - so where one becomes equal to the current value of wire - and where two accepts the new value of r3 so any dot with respect to this logic of Fibonacci series okay so swetha has pointed out a very good thing here so you don't need this pass with datatype with respect to that function and this is because R actually decides what kind of data it is taking as input and functions accordingly so we've defined the logic for the Fibonacci series now all we need to do is we need to call this method so for that just specify the name of the method that is Peabo you need to pass the value let's say I want to compute the first five Fibonacci elements then I have to just select this and let's execute it so you can see the first five elements of the Fibonacci series has been caught now let's say I need to call Fibonacci series for different values so let's say Freebo or flex 8:10 then ma I'm going to call for 15 so I am calling the function three times but each time I am passing a different value here so what I have done is that I have generalized the same set of operations that I want for any number of values that I am passing as the request you just run this you can see for five it is printing the first fire element then for 10 it is going on for the first 10 and for 13 it's going on for the first 13 elements so I hope you guys have got a simple understanding as to how you can write a user-defined function in R now again it is all left to you to explore it now you can go ahead and write functions for different purposes and as to how you write that is completely left you because everyone has their own personal style so any doubts with respect to how to work with functions in R okay so I've got from everyone so let's move forward to summarize what we learned today we started off by understanding how to install our we saw the different data types the different data operators then from there we moved on to understand the different conditional statements the various loops and finally we talked about these functions in R so any dots with respect to this since you're concluding session you're free to put across your questions if not I hope you guys have a happy weekend thank you I hope you enjoyed listening to this video please be kind enough to like it and you can comment any of your doubts and queries and we will reply to them at the earliest to look out for more videos in our playlist and subscribe to our Eddie Rick a channel to learn more happy learning
Info
Channel: edureka!
Views: 964,951
Rating: 4.8619933 out of 5
Keywords: yt:cc=on, r programming, r programming for beginners, r programming tutorial, r language for beginners, r language, r language tutorial, r programming tutorial for beginners, r tutorial, r tutorial for beginners, r tutorials for beginners, r programming language, r programming for data science, r programming basics, r programming for data analysis, r programming software, learn r programming, r training for beginners, r certification, r edureka, data analytics edureka, edureka
Id: fDRa82lxzaU
Channel Id: undefined
Length: 70min 56sec (4256 seconds)
Published: Thu May 18 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.