 Hi everyone it's MJ and in this video we're going to look at creating functions in R. But let's maybe take a step back and ask ourselves well what is a function? And functions these things are mini sub-programs that contain sets of instructions and they're called on when needed and they can be repeated. Now what does a function contain? It has its input, a set of instructions as well as an output. Now where are the functions in R? Well either you can use pre-made ones in packages or you can create your own and that's what we're going to be doing in this video. But now if we want to make functions we do need a little bit of logic. So it's good to practice logic in R by trying to solve simple problems. If you don't develop your logical programming skills then you will become frustrated with this software. So let's do a simple problem together. Let's sort a bunch of numbers from smallest to largest. Now yes there is the built-in sort function in R but we want to develop our logic so we're going to try and make it ourselves. So how would we sort five random numbers? Well the first thing we need to do is we need to generate five random numbers in R. Let's say between 1 and 10 we can use the following code. MyNumbers is equal to a sample from 1 to 10 and we want five numbers and this will return a selector of 5, 10, 4, 2 and 3. Then I mean we could use the R function of sort myNumbers and we'll get 2, 3, 4, 5 and 10 but we are going to create our own function to try and mimic this. So it will kind of be of the structure. It will be mySort is equal to function and then with the open brackets you want to put in your parameters and then in your curly brackets you're going to put in the code. But it's best to think about a function before you start typing. So we're just going to go through a little bit of the logic and then we're going to go to our studio and program it together. So one of the ways that we can sort is to use this algorithm known as the bubble sort. So let's walk it through. How the bubble sort works is you first compare position 1 and 2. So we compare 5 and 10 and we will swap if 5 is bigger than 10. In this case it's not so we don't do anything. We then compare position 2 and 3. So we're comparing 10 and 4 and we will swap if 10 is bigger than 4 and it is. So the new arrangement is going to be 5, 4, 10, 2 and 3. So we can see we've swapped position 2 and 3. We then compare position 3 and 4 and now this new position 3 is 10. And so we're comparing 10 and 2 and we're going to swap if 10 is bigger than 2. It is so the new arrangement is 5, 4, 2, 10, 3. So we can see 10 has moved from position 2 all the way up to position 4 and then we compare position 4 and 5. So we're comparing 10 and 3 and again we're going to swap if 10 is bigger than 3 and it is so the new arrangement is 5, 4, 2, 3 and 10. Now what we do is we repeat the steps but we don't have to do this final comparison of 4 and 5 because the first cycle will have kicked the biggest number to the final place. So the biggest number is in place. After we've done the second repeat we'll then repeat for a third time but now we don't have to compare positions 3 and 4 because now the second biggest is in place. And then we repeat again but we don't have to compare 2 and 3 because the third biggest is in place and essentially what we're doing this is we're doing the cycles four times with one less comparison in each and essentially we are sorting the numbers by kicking the biggest one to the end then reducing the amount of comparisons we're doing and kicking those to the biggest and that's how it will eventually get sorted. So this is our algorithm but how do we translate this into code? Well what we're going to need is we're going to need two loops. We're going to need an inner loop and this is going to be doing the comparing positions and we can use a while loop for this and then there's going to be our outer loop which is going to be repeating the steps and we can use a for loop. So let's maybe discuss very quickly what a for and while loop is and then we're going to join the two together with the algorithm of swapping to come up with our function. So the for loop example if you had to go type in r 4k in 1 to 10 and then say print and then in brackets k then what will happen is you like the return will be an ascending list from 1 to 10 and you can also do it in the other way around you can say 4k in 10 to 1 print k and this will return a descending list from 10 to 1 and I recommend that you type this into r and you see that for loops they're not that difficult to actually set up. Now for our function we want the outer loop to depend on the amount of numbers we have. So the first thing we will do is you'll say n is equal to length and then the vector that we created and then our for loop will be k in n to 2 and we then will open up our curly brackets and have all of our code. So in our example our length is 5 so the first cycle we're comparing four pairs in the second cycle we're comparing three pairs, third cycle we're comparing two pairs and the fourth and final cycle we're comparing one pair. So we would want our k to be 5, 4, 3 and 2 and because this number is descending this is where we're going to be getting our while loop. So let's maybe talk very quickly about a while loop. With a while loop you want to have your parameter so I sometimes just use i so i is equal to 1 and then I say while i is less than 5 do something so let's say print i very very important that you have this thing where the i then steps up or changes otherwise your while loop is going to go on forever because i will always be less than 5 if it stays as 1 so you want to make sure that you increase it that way your while loop will end and if we had to type in this code in r we'd get numbers like 1, 2, 3 and 4. Now what we want to do is we've got our after loop and our inner loop and we can now combine the two together so we can say 4k and n to 2 open up our curly brackets i equals 1 while i is less than k and then we will open up put in our our code over there with the dot dot dot remember to say i equals i plus 1 and then you just want to make sure that you close the brackets and this is a little bit confusing having a loop within a loop that's why I say it's good to to play around with this in r with the print function so that you can see how the the loops are changing but yes this is a complicated idea is to have two loops one inside of each other and now just to make matters a little bit more complicated we're going to talk about the swapping process so the code we're going to use is we're going to say if my numbers at position i is greater than my numbers at the next position then what we want to do is we want to say the temporary variable is going to be equal to my numbers 1 plus i we're going to say my numbers 1 plus i is equal to my numbers i and then my numbers i is going to be equal to this temp and that's going to allow for a swap in positions so for example if my numbers 2 is equal to 10 and my numbers 3 is equal to 4 and you know if 10 is greater than 4 then we will create this temp value to be equal to 4 my numbers 3 we can now make it as 10 and my numbers 2 we are going to make it equal to the temp which is 4 and I guess y'all we need this temporary temp variable because in programming you can't change two values simultaneously so that's why you kind of need need a little bit of that but I have gone through this quite quickly so maybe watch this again if I lost any of you but we are going to go through it now in in r so let's go and do that so let's program this bubble sort function in r and maybe one thing that we can do just so that we're on the same page is we can set the same seat this way we will get the same random numbers I like the number 23 but you can type in any number that you want then what we need is our vector of numbers so we're going to say my numbers is equal and we're going to use the sample function and we want numbers between 1 and 10 and we want five of them so that's what my so if we had to quickly just run this we will set that set the seeds of 23 and we click run and then if we bring up my numbers and we run that as well we should get eight three ten one and nine and if you use the same seat you're going to get the same numbers okay now what we want to do is program my sort so this is the function that we're going to be creating so we call it function and it's just going to have the single parameter which is the vector so we've got our little function and first thing we want to do is we want to know the length so we want to just say n is equal to the length and we're going to say the length not of my numbers but of this parameter x remember when we run the function instead of writing x we've been going to write my numbers so that's why we're putting x in over there then as we discussed in the earlier we're going to need the two loops and the outer loop is going to be being our for loop so we're going to say 4k in in to two and what we're going to do is then open up the brackets over here now inside these brackets we're going to have our while loop and remember with the while loop you just want to start with saying i is equal to 1 and then you say while i is less than k we're then going to carry out our little function so once we've got inside the while loop we're now going to have our if statement so we're going to say if and now we're comparing our two numbers so we're going to be first comparing you know x with i and we're going to be seeing is this going to be bigger than the next position so we're going to say i plus 1 and then you can see there's a lot of and this is the level thing about using the script editor as you can see it does this indentation for us on our own remember we do need the the temporary variable just to kind of hold what is in the x i plus 1 so let's go i plus 1 otherwise we're not going to be able to we're going to be overwriting it because yeah x i plus 1 is now going to be equal to the previous one and if we didn't create the temporary then when we come to our x i is equal we're going to say x i plus 1 because sorry keep going i plus 1 we can't do it is that because then it's just going to equal itself because of this one here and that's why we had created the temporary so this is why we create the temp and then that's going to allow the swapping to occur then when you create while loops you just want to make sure that you now increase your i so i plus 1 otherwise the while loop is going to go on forever and what we can do now is we can even have it so that in our sorting function it returns our variable of x so what we can do now is we can just run this all into the code and now when we come to our console so we come down to the bottom of our console and we say my sort and we put in my numbers lo and behold we now get the numbers sorted perfectly and what's quite cool is let's say we wanted to set the seed to 25 okay so we set the seed to 25 and we now generate a new um bunch of numbers so let's get our numbers over here we've now got seven eight four one and ten then we can simply sort these numbers and we've got them one four seven eight and ten and because we've made it so that our length is you know it's n we can even have a longer vector so let's say we want to do it with ten numbers we can then come in and say my numbers two and we now have ten numbers and now we can go my sort on my numbers two and we can see that it will now even sort ten numbers for us as well so that's pretty cool so yeah we've got my numbers at seven eight four one and ten we've got my sort of my numbers and it'll give us one four seven eight ten look you could always have used the built in sorting function of our um and if we'd come in my numbers it would have done exactly that for us but remember the point of this exercise was to develop a little bit of our logic and to see how to create quite an advanced function because I mean we had two loops and we had an if statement so quite a lot was happening in this function it is quite a complicated one but I think it's a great way to to learn I think if you can if you can master this function you can you can master the others as well so in conclusion I mean what we've seen is that there are many useful functions have already been created and you can access them by installing the relevant packages in R but sometimes you will want to create your own function and look it it's not an easy thing to do and that's why if you are serious about R it's good to practice logic by giving yourself simple problems to solve and that kind of ends this R beginner course so now you must be asking yourself well what's next and I mean gosh this list it's it could go on forever but there are so many different topics that you can now use R and explore so we will be creating future courses so stay tuned watch the space new things will be popping up but if you're very impatient and you want to watch something right now I do have some videos on YouTube where I'm using Android Studio which is also an object oriented program and I'm kind of creating more advanced functions around just sorting what I'm doing is I'm creating a poker game and I'm checking to see if I've got a you know a pair or a full house or a flush or a straight and different things like that and it's quite cool to program a game because then you have like an end result of a nice little mini project and so you can go and see what I did with poker of course Android Studio the syntax will be a little bit different to R but at the end of the day logic is the same throughout programming but as always thanks so much for watching and I hope to see you all soon cheers