R Tutorial | Learn R Programming | Intellipaat
Hey guys welcome to the session by in telepath now you guys must have come across the term our programming quite a few times and numerous questions will have popped into your head so is it just another programming language how is it different from other programming languages and is it really hard to learn our programming so today we've come up with just a right we do so that you can start your journey in the field of our programming so let's have a quick glance at the agenda we'll start off by understanding what exactly is our programming and then we'll work with variables in our after that we'll look at the different types of operators and then we'll learn about objects in our following which will implement flow control statements so what is R well R is a language which was developed by statisticians first statisticians so if you want to perform any sort of statistical analysis then R should be a guru language now R is also a great visualization tool so R provides packages such as ggplot2 and plotly to create stunning visualizations and R is an open source cross-platform compatible software and it just plug and play so all you have to do is install our ones and then you can start having fun with it and since R is an open source software you can actually modify the code and add your own innovations to it and being cross-platform compatible you can run the same R code on multiple operating systems and the best thing about RS it is a Turing complete language notice it can perform any task which a Turing machine can now that is amazing isn't it so this is what makes our such a powerful language so you can do multiple operations without such as statistical analysis implement machine learning algorithms and also create stunning visualizations all with the super language called as R so now that we know what exactly is our let's go ahead and install our and our studio in our systems so we can install our from the site over here so we can go to Krannert our project oog right so have different versions of R so if you are using a Linux system then you can download offline X if you're using a Mac system then you can download off a Mac similarly if you're using Windows system you can download off a window and since I'm using a Windows system I'll click on this link right so you see this link oh do install are for the first time click over here now this is the latest version of our our three point five point one so you have to click on it and then the download would start now since I've already downloaded our in my system I'll go ahead and cancel this right now after you install our you would also require an IDE so that you can perform your tasks quite easily so one such IDE is our studio so our studio is a free open source software which provides a smooth interface so that you can perform all of your operations quite easily and you can download our studio from this site over here our studio calm so let's go to our studio calm and download our studio right so we have entered the site I'll click over here download our studio and we see the free version over here our studio desktop open-source license again I'll click on download and you see different versions of our so again if you're using a Windows system this is the right link for you if you're using Mac system you can download a studio for Mac similarly if you're using a boon to our up fedora system you can download these versions and again since I'm using a Windows system I'll click on this link right the download has again started and since again I already have our studio installed on my system I will cancel this right so we have successfully installed our and our studio in our systems now let's go ahead and understand what exactly is a variable so simply put a variable is a temporary storage piece so let's understand the concept of variable with this example over here so let's say we have this cart and this cart would be a temporary storage piece now.
I also have an apple with me and I decide the store this Apple inside the scart on a temporary basis now after some time I decide to remove that Apple and replace it with a banana now again after some time I decide to replace that banana with a mango so this is the basic premise behind a variable it provides a temporary storage piece where we can keep on changing the values right so now that we've understood what exactly is a variable let's head on to our studio and implement some variables right guys so this is how our studio looks like so we have four panes in total this is the script window this is the console window this is the environment pin and up this is the tab where we can install new packages and also build some plots right so what I'll do is I will create a new variable and name that variable to be fruit and I will assign it a value of mango now I will select this and paste it inside the console window now let me print fruit so I get the result mango over here and since fruit is actually a variable I can change the value which is present in so again I will type in fruit and this time I will assign a value of Apple so I will paste it in the console window now let me print fruit so see that the initial value of mango has been replaced with Apple similarly I'll create the same variable and I'll change the value so this time the value which I'm assigning this would be a banana let me be sit over here now let me type in fruit let me print it so we get a banana so this is how a variable works guys so this is a temporary storage piece where the values can be kept changing again and again so what I've basically done over here is I have taken a variable fruit initially I had assigned a value mango after which I had replaced that value with an apple and again I had to replace the same value in the same object with banana right so now that we've understood what exactly is a variable so each variable is basically associated with a data type so let's start off with the first data type a numeric so a variable can store numeric values such as 5,000 and minus 33 so after numeric data types we have the character data types so variable can also have character values such as these so character value could be either a single character surd or it could be the word Sparta or it could be two words such as hello world right now after the character data type we also have the logical data types so a variable can store logical values such as true or false now you guys need to keep in mind that in our by convention this needs to be in capital right so this keyword true is capital and this keyword false is also capital and then we have the final data type which has complex so a complex data type basically comprised of two parts first the real part and then we have the imaginary part over here so 2 plus 5 I 30 minus 30 i-n minus 100 point 4 7 I all of these are complex data types so similarly a variable can also have complex data types alright so now that you've understood what exactly are data types let's go to our studio and implement some variables with different data types so I'll start off by creating a variable which would have a numeric value so I will name this as my obj and first I will assign it a numeric value which is 123 now I can use the class function to have a look at the class of the object so let me type in class and inside this class function I will pass in the object which I've just created which would be my Oh beachie right so the class of this object is numeric now what I'll do is do the same object I will pass in a character value so let's say I will pass in the word spot R to this let me hit enter now let me have a glance at the class of this object class of my obj so now we see that the class of this object is character right so initially the class of this variable was numeric but then when I had assigned a character value to the same variable the class changed to character now I'll go ahead and assign a logical value to this so let's say I assign a true value to this now again let me have a glance at the class so class of my obj so this time this is of logical type right now finally let me assign a complex type to the so my obg and using this assignment operator let's say I would give this a value of 30 plus 100 I right now let me use the class function and have a look at the class of this object so that would be my Oh beachie right so the class of this object is complex so what we basically understand from this is as we change the type of the value given to the object the class of the object also changed so we started off by assigning a numeric value to the object then when we saw the class of that object was numeric then we had the change the value and we had assign the same object or character type and that's class changed to character right so this basically means that if we change the value given to the variable the class of that variable would also change right so these were data types in art now let's have a look at the different operators which are available in art so these operators basically help us to perform some sort of mathematical or logical operations on top of a variables so basically we have these four operators in general so we can have assignment operators arithmetic operators relational operators and logical operators.
So let's start off with the assignment operators so as you can see what is stated over here assignment operators are basically used to assign a value to an object and these are the three different assignment operators available in our so you can just use the equal to operator to assign a value or we have this another symbol over here so here we have a less than operator after which we give a - so this is another type of assignment operator here again we have the eyghon followed by a greater than symbol this is another type of assignment operator and if you have a look at this we have some examples over here so what we are doing is we are assigning the value of 10 to this variable X using this assignment operator similarly here we are assigning this value 20 to this variable Y using this assignment operator and similarly we are assigning this value of 30 to this variable Z using this assignment operator so again let's head to our studio and up we will be implementing some assignment operators and assigning some values to some objects so let's see I create the same variable X so I will put in the variable name which is X after which I will give in the assignment operator which is equal to and I will given the value now so let's say the value which I want to give the system right now similarly I will assign a value to the variable Y so I will give in the name of the variable which is y and then I will use another variation of the assignment operator so first I will give in the less than operator and then I will give in a - and the value which I give us let's say 20 so I'm basically assigning the value 20 to this variable Y and then I will do I will give the value 30 and I am assigning this value 30 to the variable Z so this is basically inverse assign so first I given the value and after giving in the value I would assign it to the variable right now let me print XY and z values so x y and z so we have successfully assigned the value of 10 to variable X we have assigned 20 to variable Y and have also assign 30 to variable Z right so that is how we could work with assignment operators in R now let's head on to arithmetic operators so these automatic operators are used to perform basic mathematical operations and these are the different arithmetic operators which we have in R so this plus symbol over here helps us to add two variables then we have this minus symbol which helps us to subtract two variables then we have this asterisk symbol which helps us to multiply two variables and finally we have this /r H stands for division to the help of this we can divide two variables or two operands right now we are back to our studio and let's perform all of these arithmetic operations so I'll start with addition first so I just seen that we had created three new variables XY and Z so we will be performing all of those arithmetic operations on these three variables XY and Z so while I'll start off by adding these two variables so X which is the first operand then I will given the operator which is plus after which I will given the second operand which is y and we get the result which is 30 so the initial value of X was 10 and the initial value of Y was 20 when we add 10 plus 20 we get the value to be 30 right now similarly let me perform another addition operation so this time let me add y plus Z so the initial value of Y was 20 and the initial value of Z was 30 and when I add these two I get a value of 50 so these were some addition operations now let me perform some subtraction operations so I want to subtract Y from said so I will given the first operand which is Z and I will give in the operator which is minus and then I will give in the second operand which is y so I am subtracting Y from Z and this is what we get so Z is 30 and Y is 20 and when I subtract 20 from 30 I get 10 now let me subtract X from Z and let's see what do we get so this time what I am doing us so the set value is 30 and x value is 10 and 30 minus 10 gives us a value of 20 right so these were some subtraction operations after which well-loved start with the multiplication operations so let me multiply X with y and let me see what do we get so x value is 20 so x value is 10 and Y value is 20 and when you multiply 10 with 20 we got a value of 200 right now similarly let me multiply Y with Z so Y asterisk Z let me hit enter so we get a value of 600 because Y value was 20 and Z value was 30 and 20 cross 30 gives us a value of 600 so these were some multiplication operations now let's head on to division operations right so let me divide Z with Y and let's see what do we get so Z value was 30 y is value is 20 and 30 by 20 gives us 1.5 now let me do Y with respect to X so this would be 20 divided by 10 and this would give me a value of 2 right so these were some arithmetic operators now let's look at relational operators so these relational operators are used to compare two operands so these are all of the different relational operators which you have so this is the less than operator and this helps us to find out if the first variable is less than the second variable then we have the less than equal to operator so this helps us to find out if the first operands value is less than the second operand value then we have the greater than operator and this helps us to find out the first operand value is greater than the second operand value and then we have the greater than or equal to operator and this helps us to find out that the first operands value is greater than or equal to the second operand value and then we have the is equal to operator so here we just given up to equal to and this helps us to find out if the values of both the operands are equal and thus finally we have the not equal to operator so first we give in the explanation mark and then be given the equal to symbol so this helps us to find out if the first variable is not equal to the second variable again let's head back to our studio and let's implement some relational operations so let me check if the value of x is less than the value of y so I get a true value because 10 is less than 20 now let me check if X is less than or equal to Y now this again is true because 10 is less than or equal to Y now I will go ahead and check if X is greater than Y so this time I actually get a false so this is because 10 is not greater than 20 right now similarly let me check if X is greater than or equal to Y this again is false because 10 is neither equal to nor greater than Y now let me go ahead and check if the value of x is actually equal to the value of y so this time again I get a false value because 10 is obviously not equal to 20 now let me check if X does not equal to Y so this would obviously be true because 10 is obviously not equal to 20 so these were some of the relational operators and then we have the logical operators so the logical operators are basically used to make a decision on the basis of a condition and the two most majorly used logical operators are the and operator and the or operator so this is the and operator so let's have a look at this table to understand how does a and operator work so we have two conditions over here and if both of those conditions are evaluated to true only then the end operator would give a true result so let's take this case over here so here the first condition is evaluated to false and the second condition is also evaluated to false and that is why the result which we get is false let's take the second case so over here the first condition is evaluated to false the second condition is evaluated to true but since both of the conditions are not evaluated to true the final result is false again let's have a look at this third case here the first condition is evaluated to true but the second condition is false and that is why the final result is false so let's take this final case so over here this is the only case where both of the conditions have been evaluated to prove that as the first condition is also true and the second condition is also true and that is why the final result which we get is true then we have the or operator so the or operator gives us a true result when either of the conditions is true so let's take the first case here the first condition is evaluated to false the second condition is also evaluated to false and since both of the conditions are evaluated to false the final result is false then we have the second case over here here the first condition is false and the second condition is evaluated to true and since the second condition is evaluated to true the final result is true then we have this third case here the first condition is evaluated to true but the second condition is evaluated to false but since at least one of the conditions the final result is true and this obviously we get a true result because both of the conditions are resulting true now let's go to our studio and implement these logical operators right so let me get the first condition so the first condition would be X is less than Y then I will give in the and operator and I will put in the second condition so the second one condition would be X is less than or equal to Y now let's examine this command properly so X is less than Y this means that 10 is less than 20 which is true now let's examine the second condition so over here 10 is less than or equal to 20 this again is true so true and true would give us a true result right now let me change the conditions so let me give the condition first condition to be X is greater than Y I will give in the and operator now let me put in the second condition so the second condition would be let's say X is not equal to Y so here X is greater than Y would be yielded to false because 20 is not greater because 10 is not greater than 20 then let's examine the second condition this would be evaluated to true so 10 is obviously not equal to 20 but the final result is false because one of the conditions is evaluating to false so this was the and operator now let's work with the or operator so again let me give in the first condition so the first condition would be X is not equal to Y now let me use the or operator and let me give in the second condition so the second condition is let's say X is greater than Y so guys what do you think would be the result of you so we get a true value this is because the first condition yields a true value so Tan is obviously not equal to 20 but even though the second condition is yielding false since we have at least one condition which is yielding true the final result is also true now let me do a variation over here now let me give in to false conditions and let me see what will happen the first condition would be X is greater than Y then I'll given the or operator and then I'll give in the second condition so let's say the second condition is X is equal to Y so this time we get a false value this is because first condition is false the second condition is also false and when we use our operator with to false conditions the final result is obviously false right so these were some of the operators in are now let's go ahead and understand objects so basically every data element in our is an object as simple as that guys I'm retreating it every single data element is basically an object in our and these are the six most basic used objects in our so we have a vector metrics factor list array in a data frame so let's start off with the most basic object which is a vector so vector is actually a linear object which contains elements of same type so elements of same type or in other words elements which are homogeneous and this is the syntax to implement a vector in R you will be using the c function or the combine function and you will be passing in all of the elements right so over here i am using the c function to pass in three elements which are the numbers one two and three and similarly over here.
I am creating another vector where I'm passing in two elements which are true and false so let's go to our studio and create some vectors right so I will create a vector and I'll name that vector to be my vector now using the assignment operator let me assign some values to this so I'll be giving only the elements of same type so let's say I will give an elements of only numeric type this time one two and three so this vector comprises of three elements and all of those three elements are of numeric type so let me have a glance at the class of this so class of my vector so we see that this is of numeric now what I'll do is to the same my vector object I will assign elements of different type so this time I will be passing in elements of character type so let's say the first element is character e the second element is character B and the third element is character C let me hit enter now let me have a look at the class of this so class of my vector so see that this time it is a character now again to the same object which is my vector let me assign logical values so the logical values would be true false and true now let me take the class so that would be class of my vector and this is logical so see that it the class of the vector is basically determined by the elements which are passed to it and all of the elements are basically homogeneous now what I'll do is I'll actually pass in elements of two different types to the same vector and let's see what would be the result so the name of the vector is my vector I will use in the C function or the combined function and this time what I'll do is I will pass in two numeric elements first and after passing in two numeric elements I will pass in two logical elements true/false so this is a vector which comprises elements of different types so the first type is numeric and the second type is logical so guys what do you think would be the result so do you think there would be an error or we'd be getting some other result so let's see what we'll actually get so let me print in my vector over here so this is quite a strange result isn't it so we had actually passed in one to true and false but the result which you are getting is one to one and zero this is because the logical values have been coerced to numerical values so this means that a vector can have only homogeneous values so the value of one and two are the same so one is a numeric value it remains as 1 2 is a numeric value it remains as two but this ProLogic evalu over here this has been coerced to a numeric value of 1 similarly we have this false logical value over here and this has been coerced to a numeric value of 0 right so this time I will pass in character values and logical values to the same vector let me name that vector to be my vector and let me use the C function to assign some values right so let's say the first two elements are character so the first element as the character a and the next element as the character B after this I will given two logical values again there would be true and false now guys what do you think would be the result for this so let me just check so that would be my vector right so this time the logical values have been coerced to character values so a and B were initially character and they remain as character but these two values which were actually logical their values have been coerced to character so just to verify it let me show you the class of this character so class of my vector this is a character so even though I had passed in two character elements and two logical elements the class of both the logical elements has been changed to character and the class of the entire vector is also character right now finally I will pass in all of the elements of the three different types so I will pass in two numeric values two character values and two logical values so I will pass in two numeric values which are one in two after this I will pass into character values which are E and P and then I will finally pass into logical values which are true and false now let me hit enter my vector so this time all of the values have been coerced to character so this means that character data type has the highest precedence the numeric values has the next highest precedence and the boolean values or the logical values would have the lowest precedence right so again just to verify let me show you the class so that would be class of my vector so this is a character so even though we had passed in elements of three different types the class has been changed to character so uh let me print in my vector again over here so these are the six different elements which are present in this vector so now I'll go ahead and show you guys how to extract individual elements from this character so let's say I'd want to extract this fourth element from this vector so let's go ahead and do that so for that I would have to give in the name of the vector first which would be my vector after that I will give in two square braces and inside that square braces I will give in the index number so the index number is four over here so the numbering starts from one guys right so this is index number one two three four five six and since I want this element over here the index number would be four right so this is how we can extract the element B from this vector similarly if I wanted to extract this sixth element over here all I have to do is given the name of the vector which would be my vector I will give in two square braces and inside that I will give in the index number which would be six right so we have extracted this false value now let's say we wanted to extract up only the alternate values or the alternate indexes from this vector that is the first third and the fifth values from this entire vector so for that I would have to given the name of the vector then I will give in two square braces and using the C function or the combine function I'll be selecting the first the third and the fifth index right so from this entire vector I have extracted the first element the third element and the fifth element now similarly if I wanted to extract up all of the elements starting from index number two to index number five this is how we can do it so I will type in my vector over here I will give in square braces I will put in the first index number which is two and then I will put in a colon and then I will give in the last index number which is five so like this we can extract all of the elements starting from one particular index to the last index so let me hit enter so we have extracted all the elements starting from index number two to index number five so that is how we can work with vectors so next we have less so plus is actually similar to a vector but the only differences a list can contain heterogeneous elements that is even though if you pass in elements of different types to the same list they will be not coerced to the data type of higher precedence so let's take this example over here so this is how you can create a list so first you will use in the list function and then you will given the individual elements now we see that one element is of numeric type and the other element is of character type now even though this is of numeric this will definitely not be coerced to character type so this is a speciality of a list so you can pass in heterogeneous elements where the heterogeneous elements will not lose their core identity right so let me create the first list and name that list to be my list 1 and I'll use the assignment operator again I will use the list function right now using the list function what I'll do is I will pass in three different elements so the first element would be a numeric value 1 the next element would be your character value E and the third element would be a logical value true let me hit enter now let me print out the list so let me show you guys how does it list look like so my list 1 so guys this is how a list looks like so see that there are 3 different components over here plus the first component second component and the third component right so this first component contains the numeric value 1 the second component contains this character element a and this third component contains this logical element proof now let me show you guys the class of each of these individual elements so let me show you how to extract individual elements first so my list 1 now let's say I would want to extract the second component so if I want to extract the second component from the list I will give in square braces now inside the square braces I will give in another set of square braces now inside this I will give in the component number and since I want up the second component over here I will put in 2 so this is how we can extract the second component from this entire list now let me have a glance at the class of this so class off I'll give in the name of the list which is my list one I'll put in two braces inside the braces I'll given another set of braces and let me give in the component number which is two so you see that this is of type character now similarly let me check the class off for this component over here so the component would be three so the class is logical now let me check the class of this first component over here which is one so numeric so see that even though we are assigning elements of different type these elements are holding on to their true classes or their true datatypes right so the numeric values these are the numeric value the character value stays as a character value and the logical value stays as a logical value right so now I'll go ahead and create another list so I will name this list to be my list - and this time what I'll do is I will use the list function and I'll pass in three vectors inside this list so I will pass in a numeric vector inside this so I'll use the C function and the numeric vector would comprise of these three elements one two and three after this this will look comprised of a character vector and the character vector would comprised of a B and C so let me change this to small B over here then I will pass in the third vector or the third component so the third component word comprised of logical values and the logical values are true and true let me hit enter now let me print out this list so let's see what do we get so it's my list too so this is the list guys so this is the first component this is the second component and this is the third component right so the first component contains a vector of three numeric values the second component contains a character actor of three different individual characters then we have the third component which contains a vector of three logical values so now let's understand how can we extract individual elements from this sort of list so let's say I wanted to extract this element from this entire list so this element is present at the second location of the second component right so first I would have to given the name of the list which would be my list two after this I will specify the component so two square braces inside that I will given another set of square braces so now let me specify the component number which is two after this I will give in the element position so in the second component or from the second component I would want the element present at index number two so I will give in to over here and we have successfully extracted this value now similarly let's say if I wanted to extract this true value from this third component so this is element number 1 at component 3 so I would have to given the name of the list which is my list - so I'd have to specify the component number so the component number is 3 then I'd have to specify the position where the element is present so it is presented force position so from the third component I am extracting the first element and we get the true value right so this is how we can work with lists now let's go ahead to the third object which is a matrix so matrix is not actually a linear object it is a two-dimensional object so when I say two-dimensional object it would have both the rows and the columns and this matrix would contain only homogeneous elements so elements of similar-type and this is how you can create a matrix so we can use the matrix function for this matrix function first takes in the data where all the elements come from after this we specify the number of rows present in the matrix so this is the result for this command over here so when we specify the number of rows to be two we get the number of rows to be two over here right so this is how a matrix looks like so we have rows and we have columns and all of these elements are coming from this data over here so let's head to our studio and implement a matrix so let me use the matrix function I'll type in matrix over here then let me pass in the data so the data which should be present in the matrix should be coming from this vector and this vector would be comprising of the elements 1 2 8 so let me tender and let's see what do we get so this is the matrix which you get so we have eight rows and 1 columns so if we do not specify the number of rows in the matrix then it automatically assigns each element to a new row so since we have eight elements over here we have eight rows in one single column so let me go ahead and use the end row parameter to set some predefined number of rows so let's say I'd want two rows in the entire matrix so the end revalue would be two alright so this is a matrix guys so we have two rows and four columns now what you see over here is these elements are arranged with respect to column and not with respect to row Dallas so this is one two three four five six and seven eight these are arranged with respect to the column so if I wanted these to be arranged with respect to the row I have the by row attribute and I will set this by row attribute to be true so these are elements which were actually aligned with respect to the column now have been aligned with respect to the row so one two three four are in one row and five six seven eight are in another row so I will store this in a new object and name that object to be mat one right now let me print my phone over here so this is a matrix now let's understand how can we extract individual elements from this matrix so let's say I wanted to extract this element over here for this element is present at row number two and column number three so again I would have to given the name of the matrix which is matte one then I will give in the square braces over here inside the square braces I will first specify the row number so the row number s2 then I will put in a comma after that I will specify the column number so the column number is three let me enter right so this is how we have extracted the element or the value seven from this entire matrix right now let's say I wanted dub the element this five seven and eight from this matrix so let's do that so again I would have to give in the name of the matrix which is mat one after that I will give in to square braces and from the second row so the row number is two which I'd given and I would require the element which is present at column number one three and four so I will use the combine operator and I will specify the column values which are one three and four right so this is how we can extract these individual elements and similarly let's say if I wanted to extract this entire row from this matrix so for that I would have to type in the name of the matrix which is mat one I will use the square braces so I will give in the row number which is one and since I'd want all of the columns from this row I'll not assign anything over here so I'll just leave it to be blank right and voila we have extracted all of the elements which are part of the four stroke right so that is how we can work for the matrix next we have an array so arrays are basically just an extension to a matrix so matrix was a two-dimensional object so arrays since they are an extension to matrix they have more than two dimensions and elements which are stored in an array they are all homogeneous so this is the syntax to create an array you use the array function and then you pass in the data values after that you set the dimensions so this what you see over here 3 3 2 4 this basically tells you that we will be getting two matrices which would have three rows and three columns each right so if this seems too confusing to you let's go to our and let's actually create an array by ourselves so to create the array let us first create the vectors which would be passed to annouce the data elements inside the array so let me create the first vector I'll name that first vector to be v1 and the values in this vector would be all of the numbers starting from 1 to 8 now I will create another vector and name that vector to be v2 and the elements which would be there in this vector are the numbers starting from 9 to 16 let me hit enter again right so let's have a glance v1 is all the numbers from 1 to 8 and we - where's all the numbers from 9 to 16 now I can go ahead and create the array so I will use the array function now let me give in the data so the data is coming from those two vectors over here.
I will use the C operator to combine those two vectors first vector as v1 second vector is we do so we have successfully given the data now let me set the dimensions right so let's say I would want to matrices where I have each so where I want each matrix to have 2 rows and 4 columns so I will use C so I will given the first value which would be for the row so I would want two rows I would want four columns and I would want two such matrices so this is what we get so this dem value of 2 4 2 basically states that I have 2 Tricia's which are off to cross full type so each of these matrices has two rows and four columns so let me store this in a new object and in the object to be a RR one let me print arr1 now white so this is our resultant array now let's understand how can we extract individual elements from an array so again I would have to give in the name of the array which is arr1 then I will give in the square braces and inside the square braces let's say up I'd want to extract this element over here so this element is present at second row second column of the second Matrix right so these would be the values so I will give in the row number which is 2 I would given the column number which is also 2 then I will given the matrix number which is also 2 right so we can extract this element like this now let's say I would want to extract this element value 8 from this entire array so this is present at row number 2 column number 4 and matrix number 1 so let me give in the name of the array which is arr1 so I will give in the row number which is 2 then I will give in the column number which is 4 then I will give in the matrix number which is 1 right so we have extracted this element value 8 from this matrix so this is how we can work with erase and then we have something known as a factor so factors are basically objects which are used to categorize the data so let's take this example over here so we have a character vector over here but in this character vector we have the same element value repeating twice so we have male female female male so we have males repeating twice and female repeating twice so this basically means that we can convert this into a categorical variable that is we can convert this into a categorical variable which would have two categories the first category would be male and the second category would be female so this is where the factor objects come in so what we'll do is we will take this character object we will put this character object as a parameter the AZ dot factor function and then we can convert this into a categorical variable or a factor variable so let's go to a studio and above it some factors so now what I'll do is I'll of create a vector once so that vector would comprise of three character elements XY and Z and I will repeat those same values five times and do that I would require the IEP function which stands for repeat so inside this repeat function I am creating a new vector which would comprise of three character elements the first character element is X the next character element is y and the third character element is Z so we have three character elements now what I'll do is I will be repeating these three elements five times so let me hit enter right so what you see is X Y Z is being repeated five times so this is once twice thrice four times and five times so now I will store this in a new object and I'll name that object to be my data let me check the clasp of my data plus of my data right so this is a character now since we already know that this character vector has three different categories right so let me print in my data again right so this character vector has three categories which are being repeated again and again and these three categories are X Y and Zed so now if I wanted to convert this character vector into a categorical variable all I need to do is use the AZ dot factor function inside this AZ dot factor function I will pass in the my data variable and I will store it back to my data now let me have a glance at this class of my data so this time we see that this is a factor right now let me print my data let's see what is the difference right see that these are all of the elements and the elements have been segregated right so we have three levels X Y & Z okay so this was how we can create factor objects in our and then finally we have a data frame so data frame is basically a two dimensional structure where we can have heterogeneous elements so here each column can comprise of only homogeneous elements while a row can either contain homogeneous or heterogeneous elements so when this is the command to create a data frame in R so we will use the data dot frame function and inside this data dot frame function we can create different columns so here what we are doing with this command s we are creating two new columns so I am naming the first column as name and the values which would go in the first column are Sam and Bob the name of the second column is age and the values which would go into the second column are 32 and 48 right and this is the resultant data frame which we get so we have two columns the first column is name the second column is age and the values under name are Sam and Bob and the values in the age of 32 and 48 right so now let's go to our n the create a data frame by ourselves so now what I'll do is I want to create a data frame which would be comprising of two columns the first column would comprise of names of roots and the second column would comprise off the cost of those fruits so I will use in the data dot frame function and I will name the first column to be fruit name now let me assign values to this first column by using the C function so let's say I want three fruits over here so the first fruit would be mango the second fruit would be apple and the third fruit would be banana so now we have created the first column after this we will go ahead then create the second column so the second column would be fruit cost so let's assign some cost to those three fruits so the first cost value would be let's see 105 the second cost value would be 57 and the third cost value would be 80 so now I will up store this in a new object and in that object to be fruit let me have a glance at fruit now so we can use the view function to have a glance at a data frame so view of fruit right so we have successfully created this data frame which would comprise or these two columns so the first column is the name of the fruit and the second column is the cost of the fruit the first column comprises these three values mango apple and banana and the second column comprises these three values 105 57 India tea right so now we learn how to extract individual columns from this data frame so let's say I wanted to extract this fruit cost column from this data frame so for that I would have to given the name of the data frame first which is fruit after that I will use in the dollar symbol and using that dollar symbol I will select that column which I want to extract and since I want to extract this fruit cost column I will select this so what I'm basically doing is from the fruit data frame using the dollar symbol I am extracting the fruit cost column so let me hit enter right so we have successfully extracted this column from this data frame now let's also understand how can we extract individual values from this data frame so let's say I wanted to extract this value from this data frame so this value is present at third row second column so if I wanted to extract that cell value present at third row second column then I'd have to given the name of the data frame first which is fruit I will give in square braces first I will give in the row number which is three then I will give in the column number which is 2 let me render the right so we have extracted the cell value from this data frame over here so these were the different objects with which we could work in are now let's head on to decision-making statements so decision-making statements basically help us to impose a condition and on the basis of that condition execute some statements so in general we basically work with these two decision-making statements so we have F and F else right so now let's start with F so this is how the if statement works so first we will given the condition and check if the condition is true or false so if the condition is true then the rest of the statements will be executed else nothing will be done right so this is how the F syntax works so we'll give an F over here we will give in the condition and if this condition is evaluated to true only then some sort of action is done else nothing happens at all okay so let's go to our studio and work with the F statement right so now what I'll do is I will actually create a new vector and name that vector to be check decision and inside this vector I will given six numeric values which would be 10 20 30 40 50 and 60 right so we have our check decision vector ready let me hit enter right so we have this vector ready now let me use the if statement to find out if the element present over here the value of the element present over here is greater than 30 or not and if the value is greater than 30 I will perform some action so I will type in F over here then I will give in two braces inside this let me give in the condition so the condition would be check decision and I am checking F the element value this value which is present at index number three so if the value present at index number three of this check decision vector is greater than 30 I will print out the element present at index three has value greater than 30.
Now let me select all of this and let me print let's see if we get something or not so guys we don't get anything so we don't get a result so why is it that you think so this is because this condition has been evaluated to false right so the third element was exactly 30 so let me print check decision over here so this is the first element second element and this is the third element in check decision so over here the conditioners of this elements value is greater than 30 only then we will print this else nothing will happen and since 30 is not greater than 30 nothing is happening over here right so now let me make a small alteration over here so what I'll do is I am checking if the element present at the third index the value is equal to 30 and if the value is equal to 30 I will print the element present at index 3 has value equal to 30 let me select this command let me pass it over here right so this time this condition is evaluated to prove so the index which is present at 3 has the value 30 and since this is evaluated true that is why this statement has been executed and we get the result the element present at index 3 has value equal to 30 so now it was the if-statement now let's look at the if-else statement so the problem with the F statement was if this condition was evaluated to false nothing else was happening so this is where if-else statement comes in so here what we do is we check the condition first and if that condition is evaluated to true we do certain set of actions but if the condition is evaluated to false we do an alternate set of actions so we just don't leave it out there right if the condition is evaluated to true we perform first set of actions if the condition is evaluated to false we have an alternative set of actions and this is the syntax for that right F condition this is evaluated to prove then these statements are executed else or in other words else the condition is false then the statements below this are executed right so now let's use the FL statement so now I'll be walking with this same check decision vector so now this time what I'll do is I'll be using the if/else clause and I will check if this value is greater than 30 and if this value is greater than 30 I will print value is greater than 30 else I will print value is not greater than 30 right so it's a very simple thing guys right so I am using the if statement over here and over here I will be using the vector which is checked decision and I am checking of the element 3 of the check decision vector the value is greater than 30 and the value is greater than 30 I will be printing value greater than 30 else I will print while you not greater than 30 right so now let me select all of these and let me piece it over here and let's see what is the result right so you get value not greater than 30 so what has happened over here is so we have checked the condition this condition evaluated to false and since this condition evaluated to false we will come to this else block over here so this else block is printing value not greater than 30 right so these were some decision-making statements then we have looping statements so the looping statements basically help us to either aid a certain task right so I'm repeating it again the looping statements as the name States they help us to loop a certain set of actions or iterate or repeat a certain set of actions and the two most commonly used looping statements are for end while so let's start off with the for looping statement so this is how this works so we have the syntax over here for variable in vector and then these are the statements which are executed so what we are basically doing is we will extract the first element from the vector then we will perform certain set of actions and then we'll check Jabir is the last element or not so if we haven't reached the last element we'll go back we'll extract the next element we'll perform the same set of actions and then again we'll check if we have reached the last element and this process goes on till we have reached the last element all right so let's go to our studio and implement a for loop so this time up what I'll do is I'll create another vector and I will name that vector to be check loop and the values which I'm assigning to check loop would be 100 200 300 400 500 and 600 so let me print check loop over here right so we have a vector ready so now this time using the fur loop what I will do is I will be adding 50 additional so I will be adding additional 50 values to each of these elements right so this hundred will be turned into 150 200 will be turned into 250 300 to 350 this to 450 this to 550 and this to 650 so let's do that using the fur loop so I will use fur and we automatically have the snippet over here so first I need to given the variable name which would pass through the vector so let's say I name it to be I and the vector which I need to iterate through would be check loop so this I would start from index value 1 and thus will either it through the entire vector and after iterating what I am going to do is I'm going to add a value of extra 50 to each of these elements so print of I plus 50 so let me explain this properly so what will happen is for I in check loop so for I in check loop initially the value of I would be hundred and since the value of I is hundred I am adding another 50 to this and the result would be 150 right so once this is done I am going back and I'll check if I've reached the last element or not no I have not this the last element so the next element is 200 so now I'll take in that 200 and I'll add another 50 to it so the value would be 250 next I'll go back and check you have if I have reached the last element or not again I have not reached the last element so this time the next elements value is 300 so this time I'll print 300 plus 50 right so this is how it will go on till the last element is encountered so let me select all of this let me print it over here right so this is the result so initially we had 100 200 300 400 500 and 600 and those values have been changed to 1 52 53 54 55 50 and 650 using the for loop so this is the assignment so we'll start off with the Select function from the deploy package so again we'll have to select the 5th column and the fifteenth column let's do that so again this is very simple we have already seen this how to select some columns from the dataset so which is a fifth column 1 2 3 4 5 dependence that is a fifth column so again first value given the name of the data team then using the pipe operator we will of connect the state asset with the silic function and I am extracting only the fifth column and storing it in customer five view of customer five right so this basically gives us the dependence column from this entire dataset now similarly we can also extract the 15 column and just let that be because again that's very simple and it's redundant right so after this we'd have to extract the column numbers 3 6 9 12 15 and 18 and still written customer 3 multiple all right so again the name of 3 does it the pipe operator the Select function and inside the Select function are given all of the column numbers which I want to select so 3 6 9 12 15 and 18 I'll certain customer 3 multiple view of customer 3 multiple and these are all of the columns that we have just extracted right now I have to extract all the columns starting from column number 10 - column number 20 so for this I'd have to just put in a colon over here so for us the nemeth reader set so inside select so it'll be 10 to 20 right so the first column number a colon and then the last column number so I'll be extracting all the columns starting from column number 10 to column number 20 so seat and 20 view of C underscore 10 20 right all the columns and this is what we have after this we'd have to extract all the columns which start with the name capital P right all the columns which start with capital P so this is something different so now inside the Select function we have the starts with attribute so with the help of the starts with attribute I can extract a particular pattern so over here I will give in a pattern over here so I want to extract all those columns which start with the letter P and I'll store this in customer P view of this right so we have extracted all those columns were the column name starts with P now similar to starts with we also have the ends with attribute and over here we are extracting all those columns which end with s like like this column of you like this column oh yeah correct so we are extracting all those columns which end with AZ or the name of the column ends with an S view of customer this is actually right so these are the five columns where the column name ends with an S alright so these were very simple actually I believe just using the Select function to select some columns and then we have the filter function where we can filter out some records on the basis of a condition so first we'd have to extract all those customers where the internet service is equal to DSL let's do that so customer shown pipe operator and then filter function and the condition is internet service is equal to DSL and I'll stir it in customer underscore TSL view of customer underscore DSL so let me have a glance at the internet service right so we have only those records where the internet service is equal to DSL so there are two thousand four hundred twenty one customers whose Internet services DSL alright so after this we'd have to extract those customers where the contract type as month to month and store it on customer month let's do that so this time.
The conditioners contract is double equal to month-to-month that's it as simple as that so this is a contract column so here basically we have three values month month one year into year and we are supposed to extract only those columns where the contract is equal to month to month and we'll store it in customer month view of customer month contract column over a third right so there are three thousand eight hundred and seventy five customers whose contract types of month to month right and then the next question we are supposed to extract all male senior citizens whose payment method is electronic check and so the resultant CDMA electronic so over here the first condition as gender needs to be equal to me the second condition is senior citizen value needs to be equal to one and the third condition payment method needs to be equal to electronic check so let's do that first well given the name of the data frame the pipe operator and then inside the filter function we you have to given the three conditions so the first condition is gender needs to be equal to me the second condition a senior citizen needs to be equal to one and then the third condition is payment method is equal to electronic check so basically we are extracting all male senior citizens whose payment method Pesek will do electronic check also written senior male electronic so view of senior male electronic so there are 298 senior male citizens whose payment method is electronic check so any doubt with respect to this command over here so basically we have given three conditions inside this filter function and here are still here no grit okay so after this we'll have to extract those customers where the tenure is greater than 70 months or their total charges is greater than $8,000 so till now we have used the and operator over here we'll be using the or operator so this time the first conditioner standard needs to be greater than 70 or total charges needs to be greater than a thousand and we'll store this in customer total tenure view of customer total tenure right so let's have a glance a tenure so we see that the tenure is greater than 70 let's have a glance at total charges right so now even though over here the total charges is not greater than 8,000 so let's actually take right so this first cell value over here even though the total charges is not greater than 8,000 but the tenure is greater than 70 that is why we have this record over here similarly even though the total charges is not greater than 8,000 but still again the tenure is greater than 70 so that's why we have this record so it's or operator so either this condition has to be true or this condition has to be true right so let's head on to the final question on the filter function so now we have to extract those customers whose contract is of two years payment method Ismaili check and the value of churn is equal to yes let's do that so again.
Three conditions force conditioners the contract needs to be of two years the second condition payment method is equal to me check and third condition shown is equal to yes and we'll store this in too many years view of - male yes so this is quite interesting right so we have just three records or three customers where the contract is equal to two years the payment method is equal to male check and the churn is equal to yes or in other words there are only three customers whose contract is of two years pinfeathers music and we'll be churning out to another network so this was all about the filter function right so let's head on to the next function of the deploy package which would be sample and sample frakkin count so we are supposed to extract 333 random records from the customer shown data frame.
Let's do that right so we given the name of the data frame which is customer shown and then the sample n function basically against up so this is basically an analogous to the sample function from the bees our sampling is a part of the plot so you can consider this to be analogous to the sample function from piece art all right so we want 333 random records from the entire customer shown data set and I'm storing that into customer 3 3 3 view of customer 3 3 3 so these are the three hundred thirty three random records similarly if I want thousand random records from the entire data frame I just have to given thousand inside this view of customer thousand so these are the thousand random records which I have now so after sample and we have the sample frac function which helps us to select a random percentage of records from the entire dataset so over here we are extracting 23% of the total records randomly right so we'll have to give in a fractional value 0.23 so this point two three basically means that we are randomly can we sample random samples from the first okay so for that you would have to.
UM so I don't think that is possible with the sample and function for this that wouldn't be possible with the sample and function alright so first you would actually have to maybe select the first okay so you'd have to do two steps for this first would be customer shown I want all the columns and I won the first thousand records so I will store this in customer thousand now out of this I want a random sampling of three three three right so you'd have to do this in two steps basically sample and and then three three three so I'll just roll this urn three we've got three right so you would basically have to do this in two steps so the first step you would have to extract the first thousand records manually and then you'd have to do the random sampling right so sorry for that yeah so as I was saying we were working with the sample frack function and this so I list our prints and water and come guys just give me two minutes you right so we are extracting 20% of records from this entire dataset view of customer 23% so basically or 23% of 7,000 43 comes down to around 1620 so this is a random sampling of 20% of records from the entire customer shown dataset right so after this we have the summarize and group by functions so we will basically not do the first two because again this is slick this doesn't mean anything so we'll lose the third fourth and fifth so we'd have to find out the standard deviation of tenure and group it with respect to payment method let's do that so what we are doing this we want to know the standard distribution the solely the standard deviation of tenure with respect to the different categories of the payment method so I want to know the standard deviation of tenure with respect to electronic check the standard deviation of tenure with respect to credit card standard deviation of tenure with respect to male check and so on so again first I will give the name of the data frame and then I will given the group by function so first I will group it with respect to the payment method column so after I group it and then I will summarize so inside the summarized function as given the standard deviation of the tenure so this is what we get so the standard deviation with respect to bank transfer as 23 months with respect to credit card as twenty-three point three with respect to electronic check as twenty two point four and with respect to male check is 21 point two right now you guys need to keep in mind that the C should be first the group by function and then the summarized function so if you interchange these two then this will not work so let's see I will do control X over here and then control V over here
I also have an apple with me and I decide the store this Apple inside the scart on a temporary basis now after some time I decide to remove that Apple and replace it with a banana now again after some time I decide to replace that banana with a mango so this is the basic premise behind a variable it provides a temporary storage piece where we can keep on changing the values right so now that we've understood what exactly is a variable let's head on to our studio and implement some variables right guys so this is how our studio looks like so we have four panes in total this is the script window this is the console window this is the environment pin and up this is the tab where we can install new packages and also build some plots right so what I'll do is I will create a new variable and name that variable to be fruit and I will assign it a value of mango now I will select this and paste it inside the console window now let me print fruit so I get the result mango over here and since fruit is actually a variable I can change the value which is present in so again I will type in fruit and this time I will assign a value of Apple so I will paste it in the console window now let me print fruit so see that the initial value of mango has been replaced with Apple similarly I'll create the same variable and I'll change the value so this time the value which I'm assigning this would be a banana let me be sit over here now let me type in fruit let me print it so we get a banana so this is how a variable works guys so this is a temporary storage piece where the values can be kept changing again and again so what I've basically done over here is I have taken a variable fruit initially I had assigned a value mango after which I had replaced that value with an apple and again I had to replace the same value in the same object with banana right so now that we've understood what exactly is a variable so each variable is basically associated with a data type so let's start off with the first data type a numeric so a variable can store numeric values such as 5,000 and minus 33 so after numeric data types we have the character data types so variable can also have character values such as these so character value could be either a single character surd or it could be the word Sparta or it could be two words such as hello world right now after the character data type we also have the logical data types so a variable can store logical values such as true or false now you guys need to keep in mind that in our by convention this needs to be in capital right so this keyword true is capital and this keyword false is also capital and then we have the final data type which has complex so a complex data type basically comprised of two parts first the real part and then we have the imaginary part over here so 2 plus 5 I 30 minus 30 i-n minus 100 point 4 7 I all of these are complex data types so similarly a variable can also have complex data types alright so now that you've understood what exactly are data types let's go to our studio and implement some variables with different data types so I'll start off by creating a variable which would have a numeric value so I will name this as my obj and first I will assign it a numeric value which is 123 now I can use the class function to have a look at the class of the object so let me type in class and inside this class function I will pass in the object which I've just created which would be my Oh beachie right so the class of this object is numeric now what I'll do is do the same object I will pass in a character value so let's say I will pass in the word spot R to this let me hit enter now let me have a glance at the class of this object class of my obj so now we see that the class of this object is character right so initially the class of this variable was numeric but then when I had assigned a character value to the same variable the class changed to character now I'll go ahead and assign a logical value to this so let's say I assign a true value to this now again let me have a glance at the class so class of my obj so this time this is of logical type right now finally let me assign a complex type to the so my obg and using this assignment operator let's say I would give this a value of 30 plus 100 I right now let me use the class function and have a look at the class of this object so that would be my Oh beachie right so the class of this object is complex so what we basically understand from this is as we change the type of the value given to the object the class of the object also changed so we started off by assigning a numeric value to the object then when we saw the class of that object was numeric then we had the change the value and we had assign the same object or character type and that's class changed to character right so this basically means that if we change the value given to the variable the class of that variable would also change right so these were data types in art now let's have a look at the different operators which are available in art so these operators basically help us to perform some sort of mathematical or logical operations on top of a variables so basically we have these four operators in general so we can have assignment operators arithmetic operators relational operators and logical operators.
So let's start off with the assignment operators so as you can see what is stated over here assignment operators are basically used to assign a value to an object and these are the three different assignment operators available in our so you can just use the equal to operator to assign a value or we have this another symbol over here so here we have a less than operator after which we give a - so this is another type of assignment operator here again we have the eyghon followed by a greater than symbol this is another type of assignment operator and if you have a look at this we have some examples over here so what we are doing is we are assigning the value of 10 to this variable X using this assignment operator similarly here we are assigning this value 20 to this variable Y using this assignment operator and similarly we are assigning this value of 30 to this variable Z using this assignment operator so again let's head to our studio and up we will be implementing some assignment operators and assigning some values to some objects so let's see I create the same variable X so I will put in the variable name which is X after which I will give in the assignment operator which is equal to and I will given the value now so let's say the value which I want to give the system right now similarly I will assign a value to the variable Y so I will give in the name of the variable which is y and then I will use another variation of the assignment operator so first I will give in the less than operator and then I will give in a - and the value which I give us let's say 20 so I'm basically assigning the value 20 to this variable Y and then I will do I will give the value 30 and I am assigning this value 30 to the variable Z so this is basically inverse assign so first I given the value and after giving in the value I would assign it to the variable right now let me print XY and z values so x y and z so we have successfully assigned the value of 10 to variable X we have assigned 20 to variable Y and have also assign 30 to variable Z right so that is how we could work with assignment operators in R now let's head on to arithmetic operators so these automatic operators are used to perform basic mathematical operations and these are the different arithmetic operators which we have in R so this plus symbol over here helps us to add two variables then we have this minus symbol which helps us to subtract two variables then we have this asterisk symbol which helps us to multiply two variables and finally we have this /r H stands for division to the help of this we can divide two variables or two operands right now we are back to our studio and let's perform all of these arithmetic operations so I'll start with addition first so I just seen that we had created three new variables XY and Z so we will be performing all of those arithmetic operations on these three variables XY and Z so while I'll start off by adding these two variables so X which is the first operand then I will given the operator which is plus after which I will given the second operand which is y and we get the result which is 30 so the initial value of X was 10 and the initial value of Y was 20 when we add 10 plus 20 we get the value to be 30 right now similarly let me perform another addition operation so this time let me add y plus Z so the initial value of Y was 20 and the initial value of Z was 30 and when I add these two I get a value of 50 so these were some addition operations now let me perform some subtraction operations so I want to subtract Y from said so I will given the first operand which is Z and I will give in the operator which is minus and then I will give in the second operand which is y so I am subtracting Y from Z and this is what we get so Z is 30 and Y is 20 and when I subtract 20 from 30 I get 10 now let me subtract X from Z and let's see what do we get so this time what I am doing us so the set value is 30 and x value is 10 and 30 minus 10 gives us a value of 20 right so these were some subtraction operations after which well-loved start with the multiplication operations so let me multiply X with y and let me see what do we get so x value is 20 so x value is 10 and Y value is 20 and when you multiply 10 with 20 we got a value of 200 right now similarly let me multiply Y with Z so Y asterisk Z let me hit enter so we get a value of 600 because Y value was 20 and Z value was 30 and 20 cross 30 gives us a value of 600 so these were some multiplication operations now let's head on to division operations right so let me divide Z with Y and let's see what do we get so Z value was 30 y is value is 20 and 30 by 20 gives us 1.5 now let me do Y with respect to X so this would be 20 divided by 10 and this would give me a value of 2 right so these were some arithmetic operators now let's look at relational operators so these relational operators are used to compare two operands so these are all of the different relational operators which you have so this is the less than operator and this helps us to find out if the first variable is less than the second variable then we have the less than equal to operator so this helps us to find out if the first operands value is less than the second operand value then we have the greater than operator and this helps us to find out the first operand value is greater than the second operand value and then we have the greater than or equal to operator and this helps us to find out that the first operands value is greater than or equal to the second operand value and then we have the is equal to operator so here we just given up to equal to and this helps us to find out if the values of both the operands are equal and thus finally we have the not equal to operator so first we give in the explanation mark and then be given the equal to symbol so this helps us to find out if the first variable is not equal to the second variable again let's head back to our studio and let's implement some relational operations so let me check if the value of x is less than the value of y so I get a true value because 10 is less than 20 now let me check if X is less than or equal to Y now this again is true because 10 is less than or equal to Y now I will go ahead and check if X is greater than Y so this time I actually get a false so this is because 10 is not greater than 20 right now similarly let me check if X is greater than or equal to Y this again is false because 10 is neither equal to nor greater than Y now let me go ahead and check if the value of x is actually equal to the value of y so this time again I get a false value because 10 is obviously not equal to 20 now let me check if X does not equal to Y so this would obviously be true because 10 is obviously not equal to 20 so these were some of the relational operators and then we have the logical operators so the logical operators are basically used to make a decision on the basis of a condition and the two most majorly used logical operators are the and operator and the or operator so this is the and operator so let's have a look at this table to understand how does a and operator work so we have two conditions over here and if both of those conditions are evaluated to true only then the end operator would give a true result so let's take this case over here so here the first condition is evaluated to false and the second condition is also evaluated to false and that is why the result which we get is false let's take the second case so over here the first condition is evaluated to false the second condition is evaluated to true but since both of the conditions are not evaluated to true the final result is false again let's have a look at this third case here the first condition is evaluated to true but the second condition is false and that is why the final result is false so let's take this final case so over here this is the only case where both of the conditions have been evaluated to prove that as the first condition is also true and the second condition is also true and that is why the final result which we get is true then we have the or operator so the or operator gives us a true result when either of the conditions is true so let's take the first case here the first condition is evaluated to false the second condition is also evaluated to false and since both of the conditions are evaluated to false the final result is false then we have the second case over here here the first condition is false and the second condition is evaluated to true and since the second condition is evaluated to true the final result is true then we have this third case here the first condition is evaluated to true but the second condition is evaluated to false but since at least one of the conditions the final result is true and this obviously we get a true result because both of the conditions are resulting true now let's go to our studio and implement these logical operators right so let me get the first condition so the first condition would be X is less than Y then I will give in the and operator and I will put in the second condition so the second one condition would be X is less than or equal to Y now let's examine this command properly so X is less than Y this means that 10 is less than 20 which is true now let's examine the second condition so over here 10 is less than or equal to 20 this again is true so true and true would give us a true result right now let me change the conditions so let me give the condition first condition to be X is greater than Y I will give in the and operator now let me put in the second condition so the second condition would be let's say X is not equal to Y so here X is greater than Y would be yielded to false because 20 is not greater because 10 is not greater than 20 then let's examine the second condition this would be evaluated to true so 10 is obviously not equal to 20 but the final result is false because one of the conditions is evaluating to false so this was the and operator now let's work with the or operator so again let me give in the first condition so the first condition would be X is not equal to Y now let me use the or operator and let me give in the second condition so the second condition is let's say X is greater than Y so guys what do you think would be the result of you so we get a true value this is because the first condition yields a true value so Tan is obviously not equal to 20 but even though the second condition is yielding false since we have at least one condition which is yielding true the final result is also true now let me do a variation over here now let me give in to false conditions and let me see what will happen the first condition would be X is greater than Y then I'll given the or operator and then I'll give in the second condition so let's say the second condition is X is equal to Y so this time we get a false value this is because first condition is false the second condition is also false and when we use our operator with to false conditions the final result is obviously false right so these were some of the operators in are now let's go ahead and understand objects so basically every data element in our is an object as simple as that guys I'm retreating it every single data element is basically an object in our and these are the six most basic used objects in our so we have a vector metrics factor list array in a data frame so let's start off with the most basic object which is a vector so vector is actually a linear object which contains elements of same type so elements of same type or in other words elements which are homogeneous and this is the syntax to implement a vector in R you will be using the c function or the combine function and you will be passing in all of the elements right so over here i am using the c function to pass in three elements which are the numbers one two and three and similarly over here.
I am creating another vector where I'm passing in two elements which are true and false so let's go to our studio and create some vectors right so I will create a vector and I'll name that vector to be my vector now using the assignment operator let me assign some values to this so I'll be giving only the elements of same type so let's say I will give an elements of only numeric type this time one two and three so this vector comprises of three elements and all of those three elements are of numeric type so let me have a glance at the class of this so class of my vector so we see that this is of numeric now what I'll do is to the same my vector object I will assign elements of different type so this time I will be passing in elements of character type so let's say the first element is character e the second element is character B and the third element is character C let me hit enter now let me have a look at the class of this so class of my vector so see that this time it is a character now again to the same object which is my vector let me assign logical values so the logical values would be true false and true now let me take the class so that would be class of my vector and this is logical so see that it the class of the vector is basically determined by the elements which are passed to it and all of the elements are basically homogeneous now what I'll do is I'll actually pass in elements of two different types to the same vector and let's see what would be the result so the name of the vector is my vector I will use in the C function or the combined function and this time what I'll do is I will pass in two numeric elements first and after passing in two numeric elements I will pass in two logical elements true/false so this is a vector which comprises elements of different types so the first type is numeric and the second type is logical so guys what do you think would be the result so do you think there would be an error or we'd be getting some other result so let's see what we'll actually get so let me print in my vector over here so this is quite a strange result isn't it so we had actually passed in one to true and false but the result which you are getting is one to one and zero this is because the logical values have been coerced to numerical values so this means that a vector can have only homogeneous values so the value of one and two are the same so one is a numeric value it remains as 1 2 is a numeric value it remains as two but this ProLogic evalu over here this has been coerced to a numeric value of 1 similarly we have this false logical value over here and this has been coerced to a numeric value of 0 right so this time I will pass in character values and logical values to the same vector let me name that vector to be my vector and let me use the C function to assign some values right so let's say the first two elements are character so the first element as the character a and the next element as the character B after this I will given two logical values again there would be true and false now guys what do you think would be the result for this so let me just check so that would be my vector right so this time the logical values have been coerced to character values so a and B were initially character and they remain as character but these two values which were actually logical their values have been coerced to character so just to verify it let me show you the class of this character so class of my vector this is a character so even though I had passed in two character elements and two logical elements the class of both the logical elements has been changed to character and the class of the entire vector is also character right now finally I will pass in all of the elements of the three different types so I will pass in two numeric values two character values and two logical values so I will pass in two numeric values which are one in two after this I will pass into character values which are E and P and then I will finally pass into logical values which are true and false now let me hit enter my vector so this time all of the values have been coerced to character so this means that character data type has the highest precedence the numeric values has the next highest precedence and the boolean values or the logical values would have the lowest precedence right so again just to verify let me show you the class so that would be class of my vector so this is a character so even though we had passed in elements of three different types the class has been changed to character so uh let me print in my vector again over here so these are the six different elements which are present in this vector so now I'll go ahead and show you guys how to extract individual elements from this character so let's say I'd want to extract this fourth element from this vector so let's go ahead and do that so for that I would have to give in the name of the vector first which would be my vector after that I will give in two square braces and inside that square braces I will give in the index number so the index number is four over here so the numbering starts from one guys right so this is index number one two three four five six and since I want this element over here the index number would be four right so this is how we can extract the element B from this vector similarly if I wanted to extract this sixth element over here all I have to do is given the name of the vector which would be my vector I will give in two square braces and inside that I will give in the index number which would be six right so we have extracted this false value now let's say we wanted to extract up only the alternate values or the alternate indexes from this vector that is the first third and the fifth values from this entire vector so for that I would have to given the name of the vector then I will give in two square braces and using the C function or the combine function I'll be selecting the first the third and the fifth index right so from this entire vector I have extracted the first element the third element and the fifth element now similarly if I wanted to extract up all of the elements starting from index number two to index number five this is how we can do it so I will type in my vector over here I will give in square braces I will put in the first index number which is two and then I will put in a colon and then I will give in the last index number which is five so like this we can extract all of the elements starting from one particular index to the last index so let me hit enter so we have extracted all the elements starting from index number two to index number five so that is how we can work with vectors so next we have less so plus is actually similar to a vector but the only differences a list can contain heterogeneous elements that is even though if you pass in elements of different types to the same list they will be not coerced to the data type of higher precedence so let's take this example over here so this is how you can create a list so first you will use in the list function and then you will given the individual elements now we see that one element is of numeric type and the other element is of character type now even though this is of numeric this will definitely not be coerced to character type so this is a speciality of a list so you can pass in heterogeneous elements where the heterogeneous elements will not lose their core identity right so let me create the first list and name that list to be my list 1 and I'll use the assignment operator again I will use the list function right now using the list function what I'll do is I will pass in three different elements so the first element would be a numeric value 1 the next element would be your character value E and the third element would be a logical value true let me hit enter now let me print out the list so let me show you guys how does it list look like so my list 1 so guys this is how a list looks like so see that there are 3 different components over here plus the first component second component and the third component right so this first component contains the numeric value 1 the second component contains this character element a and this third component contains this logical element proof now let me show you guys the class of each of these individual elements so let me show you how to extract individual elements first so my list 1 now let's say I would want to extract the second component so if I want to extract the second component from the list I will give in square braces now inside the square braces I will give in another set of square braces now inside this I will give in the component number and since I want up the second component over here I will put in 2 so this is how we can extract the second component from this entire list now let me have a glance at the class of this so class off I'll give in the name of the list which is my list one I'll put in two braces inside the braces I'll given another set of braces and let me give in the component number which is two so you see that this is of type character now similarly let me check the class off for this component over here so the component would be three so the class is logical now let me check the class of this first component over here which is one so numeric so see that even though we are assigning elements of different type these elements are holding on to their true classes or their true datatypes right so the numeric values these are the numeric value the character value stays as a character value and the logical value stays as a logical value right so now I'll go ahead and create another list so I will name this list to be my list - and this time what I'll do is I will use the list function and I'll pass in three vectors inside this list so I will pass in a numeric vector inside this so I'll use the C function and the numeric vector would comprise of these three elements one two and three after this this will look comprised of a character vector and the character vector would comprised of a B and C so let me change this to small B over here then I will pass in the third vector or the third component so the third component word comprised of logical values and the logical values are true and true let me hit enter now let me print out this list so let's see what do we get so it's my list too so this is the list guys so this is the first component this is the second component and this is the third component right so the first component contains a vector of three numeric values the second component contains a character actor of three different individual characters then we have the third component which contains a vector of three logical values so now let's understand how can we extract individual elements from this sort of list so let's say I wanted to extract this element from this entire list so this element is present at the second location of the second component right so first I would have to given the name of the list which would be my list two after this I will specify the component so two square braces inside that I will given another set of square braces so now let me specify the component number which is two after this I will give in the element position so in the second component or from the second component I would want the element present at index number two so I will give in to over here and we have successfully extracted this value now similarly let's say if I wanted to extract this true value from this third component so this is element number 1 at component 3 so I would have to given the name of the list which is my list - so I'd have to specify the component number so the component number is 3 then I'd have to specify the position where the element is present so it is presented force position so from the third component I am extracting the first element and we get the true value right so this is how we can work with lists now let's go ahead to the third object which is a matrix so matrix is not actually a linear object it is a two-dimensional object so when I say two-dimensional object it would have both the rows and the columns and this matrix would contain only homogeneous elements so elements of similar-type and this is how you can create a matrix so we can use the matrix function for this matrix function first takes in the data where all the elements come from after this we specify the number of rows present in the matrix so this is the result for this command over here so when we specify the number of rows to be two we get the number of rows to be two over here right so this is how a matrix looks like so we have rows and we have columns and all of these elements are coming from this data over here so let's head to our studio and implement a matrix so let me use the matrix function I'll type in matrix over here then let me pass in the data so the data which should be present in the matrix should be coming from this vector and this vector would be comprising of the elements 1 2 8 so let me tender and let's see what do we get so this is the matrix which you get so we have eight rows and 1 columns so if we do not specify the number of rows in the matrix then it automatically assigns each element to a new row so since we have eight elements over here we have eight rows in one single column so let me go ahead and use the end row parameter to set some predefined number of rows so let's say I'd want two rows in the entire matrix so the end revalue would be two alright so this is a matrix guys so we have two rows and four columns now what you see over here is these elements are arranged with respect to column and not with respect to row Dallas so this is one two three four five six and seven eight these are arranged with respect to the column so if I wanted these to be arranged with respect to the row I have the by row attribute and I will set this by row attribute to be true so these are elements which were actually aligned with respect to the column now have been aligned with respect to the row so one two three four are in one row and five six seven eight are in another row so I will store this in a new object and name that object to be mat one right now let me print my phone over here so this is a matrix now let's understand how can we extract individual elements from this matrix so let's say I wanted to extract this element over here for this element is present at row number two and column number three so again I would have to given the name of the matrix which is matte one then I will give in the square braces over here inside the square braces I will first specify the row number so the row number s2 then I will put in a comma after that I will specify the column number so the column number is three let me enter right so this is how we have extracted the element or the value seven from this entire matrix right now let's say I wanted dub the element this five seven and eight from this matrix so let's do that so again I would have to give in the name of the matrix which is mat one after that I will give in to square braces and from the second row so the row number is two which I'd given and I would require the element which is present at column number one three and four so I will use the combine operator and I will specify the column values which are one three and four right so this is how we can extract these individual elements and similarly let's say if I wanted to extract this entire row from this matrix so for that I would have to type in the name of the matrix which is mat one I will use the square braces so I will give in the row number which is one and since I'd want all of the columns from this row I'll not assign anything over here so I'll just leave it to be blank right and voila we have extracted all of the elements which are part of the four stroke right so that is how we can work for the matrix next we have an array so arrays are basically just an extension to a matrix so matrix was a two-dimensional object so arrays since they are an extension to matrix they have more than two dimensions and elements which are stored in an array they are all homogeneous so this is the syntax to create an array you use the array function and then you pass in the data values after that you set the dimensions so this what you see over here 3 3 2 4 this basically tells you that we will be getting two matrices which would have three rows and three columns each right so if this seems too confusing to you let's go to our and let's actually create an array by ourselves so to create the array let us first create the vectors which would be passed to annouce the data elements inside the array so let me create the first vector I'll name that first vector to be v1 and the values in this vector would be all of the numbers starting from 1 to 8 now I will create another vector and name that vector to be v2 and the elements which would be there in this vector are the numbers starting from 9 to 16 let me hit enter again right so let's have a glance v1 is all the numbers from 1 to 8 and we - where's all the numbers from 9 to 16 now I can go ahead and create the array so I will use the array function now let me give in the data so the data is coming from those two vectors over here.
I will use the C operator to combine those two vectors first vector as v1 second vector is we do so we have successfully given the data now let me set the dimensions right so let's say I would want to matrices where I have each so where I want each matrix to have 2 rows and 4 columns so I will use C so I will given the first value which would be for the row so I would want two rows I would want four columns and I would want two such matrices so this is what we get so this dem value of 2 4 2 basically states that I have 2 Tricia's which are off to cross full type so each of these matrices has two rows and four columns so let me store this in a new object and in the object to be a RR one let me print arr1 now white so this is our resultant array now let's understand how can we extract individual elements from an array so again I would have to give in the name of the array which is arr1 then I will give in the square braces and inside the square braces let's say up I'd want to extract this element over here so this element is present at second row second column of the second Matrix right so these would be the values so I will give in the row number which is 2 I would given the column number which is also 2 then I will given the matrix number which is also 2 right so we can extract this element like this now let's say I would want to extract this element value 8 from this entire array so this is present at row number 2 column number 4 and matrix number 1 so let me give in the name of the array which is arr1 so I will give in the row number which is 2 then I will give in the column number which is 4 then I will give in the matrix number which is 1 right so we have extracted this element value 8 from this matrix so this is how we can work with erase and then we have something known as a factor so factors are basically objects which are used to categorize the data so let's take this example over here so we have a character vector over here but in this character vector we have the same element value repeating twice so we have male female female male so we have males repeating twice and female repeating twice so this basically means that we can convert this into a categorical variable that is we can convert this into a categorical variable which would have two categories the first category would be male and the second category would be female so this is where the factor objects come in so what we'll do is we will take this character object we will put this character object as a parameter the AZ dot factor function and then we can convert this into a categorical variable or a factor variable so let's go to a studio and above it some factors so now what I'll do is I'll of create a vector once so that vector would comprise of three character elements XY and Z and I will repeat those same values five times and do that I would require the IEP function which stands for repeat so inside this repeat function I am creating a new vector which would comprise of three character elements the first character element is X the next character element is y and the third character element is Z so we have three character elements now what I'll do is I will be repeating these three elements five times so let me hit enter right so what you see is X Y Z is being repeated five times so this is once twice thrice four times and five times so now I will store this in a new object and I'll name that object to be my data let me check the clasp of my data plus of my data right so this is a character now since we already know that this character vector has three different categories right so let me print in my data again right so this character vector has three categories which are being repeated again and again and these three categories are X Y and Zed so now if I wanted to convert this character vector into a categorical variable all I need to do is use the AZ dot factor function inside this AZ dot factor function I will pass in the my data variable and I will store it back to my data now let me have a glance at this class of my data so this time we see that this is a factor right now let me print my data let's see what is the difference right see that these are all of the elements and the elements have been segregated right so we have three levels X Y & Z okay so this was how we can create factor objects in our and then finally we have a data frame so data frame is basically a two dimensional structure where we can have heterogeneous elements so here each column can comprise of only homogeneous elements while a row can either contain homogeneous or heterogeneous elements so when this is the command to create a data frame in R so we will use the data dot frame function and inside this data dot frame function we can create different columns so here what we are doing with this command s we are creating two new columns so I am naming the first column as name and the values which would go in the first column are Sam and Bob the name of the second column is age and the values which would go into the second column are 32 and 48 right and this is the resultant data frame which we get so we have two columns the first column is name the second column is age and the values under name are Sam and Bob and the values in the age of 32 and 48 right so now let's go to our n the create a data frame by ourselves so now what I'll do is I want to create a data frame which would be comprising of two columns the first column would comprise of names of roots and the second column would comprise off the cost of those fruits so I will use in the data dot frame function and I will name the first column to be fruit name now let me assign values to this first column by using the C function so let's say I want three fruits over here so the first fruit would be mango the second fruit would be apple and the third fruit would be banana so now we have created the first column after this we will go ahead then create the second column so the second column would be fruit cost so let's assign some cost to those three fruits so the first cost value would be let's see 105 the second cost value would be 57 and the third cost value would be 80 so now I will up store this in a new object and in that object to be fruit let me have a glance at fruit now so we can use the view function to have a glance at a data frame so view of fruit right so we have successfully created this data frame which would comprise or these two columns so the first column is the name of the fruit and the second column is the cost of the fruit the first column comprises these three values mango apple and banana and the second column comprises these three values 105 57 India tea right so now we learn how to extract individual columns from this data frame so let's say I wanted to extract this fruit cost column from this data frame so for that I would have to given the name of the data frame first which is fruit after that I will use in the dollar symbol and using that dollar symbol I will select that column which I want to extract and since I want to extract this fruit cost column I will select this so what I'm basically doing is from the fruit data frame using the dollar symbol I am extracting the fruit cost column so let me hit enter right so we have successfully extracted this column from this data frame now let's also understand how can we extract individual values from this data frame so let's say I wanted to extract this value from this data frame so this value is present at third row second column so if I wanted to extract that cell value present at third row second column then I'd have to given the name of the data frame first which is fruit I will give in square braces first I will give in the row number which is three then I will give in the column number which is 2 let me render the right so we have extracted the cell value from this data frame over here so these were the different objects with which we could work in are now let's head on to decision-making statements so decision-making statements basically help us to impose a condition and on the basis of that condition execute some statements so in general we basically work with these two decision-making statements so we have F and F else right so now let's start with F so this is how the if statement works so first we will given the condition and check if the condition is true or false so if the condition is true then the rest of the statements will be executed else nothing will be done right so this is how the F syntax works so we'll give an F over here we will give in the condition and if this condition is evaluated to true only then some sort of action is done else nothing happens at all okay so let's go to our studio and work with the F statement right so now what I'll do is I will actually create a new vector and name that vector to be check decision and inside this vector I will given six numeric values which would be 10 20 30 40 50 and 60 right so we have our check decision vector ready let me hit enter right so we have this vector ready now let me use the if statement to find out if the element present over here the value of the element present over here is greater than 30 or not and if the value is greater than 30 I will perform some action so I will type in F over here then I will give in two braces inside this let me give in the condition so the condition would be check decision and I am checking F the element value this value which is present at index number three so if the value present at index number three of this check decision vector is greater than 30 I will print out the element present at index three has value greater than 30.
Now let me select all of this and let me print let's see if we get something or not so guys we don't get anything so we don't get a result so why is it that you think so this is because this condition has been evaluated to false right so the third element was exactly 30 so let me print check decision over here so this is the first element second element and this is the third element in check decision so over here the conditioners of this elements value is greater than 30 only then we will print this else nothing will happen and since 30 is not greater than 30 nothing is happening over here right so now let me make a small alteration over here so what I'll do is I am checking if the element present at the third index the value is equal to 30 and if the value is equal to 30 I will print the element present at index 3 has value equal to 30 let me select this command let me pass it over here right so this time this condition is evaluated to prove so the index which is present at 3 has the value 30 and since this is evaluated true that is why this statement has been executed and we get the result the element present at index 3 has value equal to 30 so now it was the if-statement now let's look at the if-else statement so the problem with the F statement was if this condition was evaluated to false nothing else was happening so this is where if-else statement comes in so here what we do is we check the condition first and if that condition is evaluated to true we do certain set of actions but if the condition is evaluated to false we do an alternate set of actions so we just don't leave it out there right if the condition is evaluated to true we perform first set of actions if the condition is evaluated to false we have an alternative set of actions and this is the syntax for that right F condition this is evaluated to prove then these statements are executed else or in other words else the condition is false then the statements below this are executed right so now let's use the FL statement so now I'll be walking with this same check decision vector so now this time what I'll do is I'll be using the if/else clause and I will check if this value is greater than 30 and if this value is greater than 30 I will print value is greater than 30 else I will print value is not greater than 30 right so it's a very simple thing guys right so I am using the if statement over here and over here I will be using the vector which is checked decision and I am checking of the element 3 of the check decision vector the value is greater than 30 and the value is greater than 30 I will be printing value greater than 30 else I will print while you not greater than 30 right so now let me select all of these and let me piece it over here and let's see what is the result right so you get value not greater than 30 so what has happened over here is so we have checked the condition this condition evaluated to false and since this condition evaluated to false we will come to this else block over here so this else block is printing value not greater than 30 right so these were some decision-making statements then we have looping statements so the looping statements basically help us to either aid a certain task right so I'm repeating it again the looping statements as the name States they help us to loop a certain set of actions or iterate or repeat a certain set of actions and the two most commonly used looping statements are for end while so let's start off with the for looping statement so this is how this works so we have the syntax over here for variable in vector and then these are the statements which are executed so what we are basically doing is we will extract the first element from the vector then we will perform certain set of actions and then we'll check Jabir is the last element or not so if we haven't reached the last element we'll go back we'll extract the next element we'll perform the same set of actions and then again we'll check if we have reached the last element and this process goes on till we have reached the last element all right so let's go to our studio and implement a for loop so this time up what I'll do is I'll create another vector and I will name that vector to be check loop and the values which I'm assigning to check loop would be 100 200 300 400 500 and 600 so let me print check loop over here right so we have a vector ready so now this time using the fur loop what I will do is I will be adding 50 additional so I will be adding additional 50 values to each of these elements right so this hundred will be turned into 150 200 will be turned into 250 300 to 350 this to 450 this to 550 and this to 650 so let's do that using the fur loop so I will use fur and we automatically have the snippet over here so first I need to given the variable name which would pass through the vector so let's say I name it to be I and the vector which I need to iterate through would be check loop so this I would start from index value 1 and thus will either it through the entire vector and after iterating what I am going to do is I'm going to add a value of extra 50 to each of these elements so print of I plus 50 so let me explain this properly so what will happen is for I in check loop so for I in check loop initially the value of I would be hundred and since the value of I is hundred I am adding another 50 to this and the result would be 150 right so once this is done I am going back and I'll check if I've reached the last element or not no I have not this the last element so the next element is 200 so now I'll take in that 200 and I'll add another 50 to it so the value would be 250 next I'll go back and check you have if I have reached the last element or not again I have not reached the last element so this time the next elements value is 300 so this time I'll print 300 plus 50 right so this is how it will go on till the last element is encountered so let me select all of this let me print it over here right so this is the result so initially we had 100 200 300 400 500 and 600 and those values have been changed to 1 52 53 54 55 50 and 650 using the for loop so this is the assignment so we'll start off with the Select function from the deploy package so again we'll have to select the 5th column and the fifteenth column let's do that so again this is very simple we have already seen this how to select some columns from the dataset so which is a fifth column 1 2 3 4 5 dependence that is a fifth column so again first value given the name of the data team then using the pipe operator we will of connect the state asset with the silic function and I am extracting only the fifth column and storing it in customer five view of customer five right so this basically gives us the dependence column from this entire dataset now similarly we can also extract the 15 column and just let that be because again that's very simple and it's redundant right so after this we'd have to extract the column numbers 3 6 9 12 15 and 18 and still written customer 3 multiple all right so again the name of 3 does it the pipe operator the Select function and inside the Select function are given all of the column numbers which I want to select so 3 6 9 12 15 and 18 I'll certain customer 3 multiple view of customer 3 multiple and these are all of the columns that we have just extracted right now I have to extract all the columns starting from column number 10 - column number 20 so for this I'd have to just put in a colon over here so for us the nemeth reader set so inside select so it'll be 10 to 20 right so the first column number a colon and then the last column number so I'll be extracting all the columns starting from column number 10 to column number 20 so seat and 20 view of C underscore 10 20 right all the columns and this is what we have after this we'd have to extract all the columns which start with the name capital P right all the columns which start with capital P so this is something different so now inside the Select function we have the starts with attribute so with the help of the starts with attribute I can extract a particular pattern so over here I will give in a pattern over here so I want to extract all those columns which start with the letter P and I'll store this in customer P view of this right so we have extracted all those columns were the column name starts with P now similar to starts with we also have the ends with attribute and over here we are extracting all those columns which end with s like like this column of you like this column oh yeah correct so we are extracting all those columns which end with AZ or the name of the column ends with an S view of customer this is actually right so these are the five columns where the column name ends with an S alright so these were very simple actually I believe just using the Select function to select some columns and then we have the filter function where we can filter out some records on the basis of a condition so first we'd have to extract all those customers where the internet service is equal to DSL let's do that so customer shown pipe operator and then filter function and the condition is internet service is equal to DSL and I'll stir it in customer underscore TSL view of customer underscore DSL so let me have a glance at the internet service right so we have only those records where the internet service is equal to DSL so there are two thousand four hundred twenty one customers whose Internet services DSL alright so after this we'd have to extract those customers where the contract type as month to month and store it on customer month let's do that so this time.
The conditioners contract is double equal to month-to-month that's it as simple as that so this is a contract column so here basically we have three values month month one year into year and we are supposed to extract only those columns where the contract is equal to month to month and we'll store it in customer month view of customer month contract column over a third right so there are three thousand eight hundred and seventy five customers whose contract types of month to month right and then the next question we are supposed to extract all male senior citizens whose payment method is electronic check and so the resultant CDMA electronic so over here the first condition as gender needs to be equal to me the second condition is senior citizen value needs to be equal to one and the third condition payment method needs to be equal to electronic check so let's do that first well given the name of the data frame the pipe operator and then inside the filter function we you have to given the three conditions so the first condition is gender needs to be equal to me the second condition a senior citizen needs to be equal to one and then the third condition is payment method is equal to electronic check so basically we are extracting all male senior citizens whose payment method Pesek will do electronic check also written senior male electronic so view of senior male electronic so there are 298 senior male citizens whose payment method is electronic check so any doubt with respect to this command over here so basically we have given three conditions inside this filter function and here are still here no grit okay so after this we'll have to extract those customers where the tenure is greater than 70 months or their total charges is greater than $8,000 so till now we have used the and operator over here we'll be using the or operator so this time the first conditioner standard needs to be greater than 70 or total charges needs to be greater than a thousand and we'll store this in customer total tenure view of customer total tenure right so let's have a glance a tenure so we see that the tenure is greater than 70 let's have a glance at total charges right so now even though over here the total charges is not greater than 8,000 so let's actually take right so this first cell value over here even though the total charges is not greater than 8,000 but the tenure is greater than 70 that is why we have this record over here similarly even though the total charges is not greater than 8,000 but still again the tenure is greater than 70 so that's why we have this record so it's or operator so either this condition has to be true or this condition has to be true right so let's head on to the final question on the filter function so now we have to extract those customers whose contract is of two years payment method Ismaili check and the value of churn is equal to yes let's do that so again.
Three conditions force conditioners the contract needs to be of two years the second condition payment method is equal to me check and third condition shown is equal to yes and we'll store this in too many years view of - male yes so this is quite interesting right so we have just three records or three customers where the contract is equal to two years the payment method is equal to male check and the churn is equal to yes or in other words there are only three customers whose contract is of two years pinfeathers music and we'll be churning out to another network so this was all about the filter function right so let's head on to the next function of the deploy package which would be sample and sample frakkin count so we are supposed to extract 333 random records from the customer shown data frame.
Let's do that right so we given the name of the data frame which is customer shown and then the sample n function basically against up so this is basically an analogous to the sample function from the bees our sampling is a part of the plot so you can consider this to be analogous to the sample function from piece art all right so we want 333 random records from the entire customer shown data set and I'm storing that into customer 3 3 3 view of customer 3 3 3 so these are the three hundred thirty three random records similarly if I want thousand random records from the entire data frame I just have to given thousand inside this view of customer thousand so these are the thousand random records which I have now so after sample and we have the sample frac function which helps us to select a random percentage of records from the entire dataset so over here we are extracting 23% of the total records randomly right so we'll have to give in a fractional value 0.23 so this point two three basically means that we are randomly can we sample random samples from the first okay so for that you would have to.
UM so I don't think that is possible with the sample and function for this that wouldn't be possible with the sample and function alright so first you would actually have to maybe select the first okay so you'd have to do two steps for this first would be customer shown I want all the columns and I won the first thousand records so I will store this in customer thousand now out of this I want a random sampling of three three three right so you'd have to do this in two steps basically sample and and then three three three so I'll just roll this urn three we've got three right so you would basically have to do this in two steps so the first step you would have to extract the first thousand records manually and then you'd have to do the random sampling right so sorry for that yeah so as I was saying we were working with the sample frack function and this so I list our prints and water and come guys just give me two minutes you right so we are extracting 20% of records from this entire dataset view of customer 23% so basically or 23% of 7,000 43 comes down to around 1620 so this is a random sampling of 20% of records from the entire customer shown dataset right so after this we have the summarize and group by functions so we will basically not do the first two because again this is slick this doesn't mean anything so we'll lose the third fourth and fifth so we'd have to find out the standard deviation of tenure and group it with respect to payment method let's do that so what we are doing this we want to know the standard distribution the solely the standard deviation of tenure with respect to the different categories of the payment method so I want to know the standard deviation of tenure with respect to electronic check the standard deviation of tenure with respect to credit card standard deviation of tenure with respect to male check and so on so again first I will give the name of the data frame and then I will given the group by function so first I will group it with respect to the payment method column so after I group it and then I will summarize so inside the summarized function as given the standard deviation of the tenure so this is what we get so the standard deviation with respect to bank transfer as 23 months with respect to credit card as twenty-three point three with respect to electronic check as twenty two point four and with respect to male check is 21 point two right now you guys need to keep in mind that the C should be first the group by function and then the summarized function so if you interchange these two then this will not work so let's see I will do control X over here and then control V over here