When would you want to write a function? Well you want to write a function if you've repeated the same code more than, if I have repeated it a second time I attempted to write a function. But definitely if you have to copy and paste it for a third time, just modifying one little piece. That's your signal it's time to write a function. Now the steps to creating a function are here in your text. We will always figure out the logic in a very simple case that would give it a meaningful name, usually a verb because functions do things. We'll list the input inside the function. They will put the code for the function inside the curly brackets. It will start with simple, right? We'll make it work for the simple case. We'll test it with some different inputs. Then lastly we'll go back and add error checking of our inputs. Now, our functions we're going to save as a text file which we do this in our studio. We're actually saving them in our markdown for the most part. So they will be inside of a text file but you will invent the code and limit the width and you don't want to make your functions too long, right? You want each function to do one small task. You don't want it to be doing a lot of different things. So let's start with a very basic function. So here I'm going to just write an addition function, okay. So here, I'm going to name it, I'm going to call it f which is not a good name, right? I should probably call it my adder or something and I say, okay, this is a function and it's a function of let's say A and B. Those are going to be my inputs. And I start my curly bracket. Now when you're doing this on the console, you have to delete that second bracket and you just tell it what you want the function to do. I'm going to do a + b, okay? And I'm going to end my function. So now my function has the name F, and if you look in your environment tab, you will see that it has a function F. And so when I do F( 2, 3), it gives me the answer. I could do F of 2 negatives, let's say 3.5. So I want to test it with both positive and negative withholding up, with integers, with non integers and be sure it works for a very simple example. Now we're going to look at the example that was in our textbook. So I'm going to start with just creating a data frame and my data frame is going to have this is random normal. So this picks 10 numbers from a random normal distribution, and since I didn't give it the other optional inputs, it's going to use a mean of zero and a standard deviation of one. So let's look at what's in this data frame. So I've got four columns As, Bs, Cs and Ds and they all have numbers from randomly normally distributed, okay. So now let me look at something I might want to do. I might want to re scale, let's say I want to re scale column A. And I wanted to instead be between 0 and 1, okay. Now if you look at this, this is what this will do. So this takes the item in A subtracts the minimum. So the minimum will be this one then minus 2.08. It removes the NA. So if there were an NA in it, it takes them out. And it divides that by the maximum of this column which will be 1.37 divided by the minimum. So if I'm at The maximum, if I'm at 1.37, the top will be 1.37 minus something over minus the minimum. So minus the -2.08 but it's dividing by that same thing, right? So the maximum value will go to one because anything divided by itself is one. The minimum value, you see I'll be dividing by the, it'll Be the minimum minus the minimum which will be zero divided by the maximum modest mean. So that will go to 0. So if I look at the data frame now you see those numbers got scaled in the eighth column, where the smallest one, right, went to zero and the largest one went to one and everything else is scaled in between. So now I have re scaled that column, but suppose I say let's do the same thing to all of the other columns. So I copy and paste my code, right, while I change from the As, to the Bs and the so forth to the Cs and the Ds. But when I look at this and try this and then I look at my data frame, let's see okay even though I made a typo here on the B, so C look at this, B didn't come out quite so well. Gotta look really closely to see that I've got a 1.55 it's not going between zero and one. I looked back up and say, Man, I forgot to replace that one point there that I should have replaced. So that's one of the dangers of just copying and pasting this code. Now one of the things I want to look at is how many inputs does each line have and then I'm going to stop and simplify each of these. So each line has one input, right? It's the column that I'm working with, the df $ whatever, right? So let's redo this with the inputs. So let's see I'm going to just rename. Let me let me regenerate my data frame. So I have clean data that I'm starting with. So I'm going to start back here so I have one that's not scaled, okay. So I have now my data frame, okay. And so now I'm going to let X be df $a. All right I'm just going to give it a i a different name so that I can verify what my input is. And then I looked at what was the code that I was using, okay. So the code that I was using, I'm going to repeat that but now I'm going to put Xs instead of where I had the DF dollars A, okay. So if I look at that, now I can look at there's what I get if I do the same thing and then I think well is there a way to simplify this, get rid of any duplication, make it more readable. And then I think well let's see, there's a range function and so I am going to do the range of X with removing the NAs. And if I now look at range, it tells me the maximum and minimum so I can find that maybe I can make my code a little simpler by putting that now in instead. So I will make that [COUGH] so I'm getting the same answers. This is good. Okay, so now that I have my function down I am going to write it now as a function and I'm going to give it a better name. So I'm going to call my function re scale 01. Because that's what it does, is it re scales my numbers of my vector that are in my vector between zero and one. And so I'm entering that and I put copy the same code that I had above right here and here into my function. And then I try testing it with some simple cases. So I can test it first with the vector this maybe just 05 and 10. All right, that works. I can try the same thing and let's put a negative ten on there. I want to be sure it works with negative numbers. That comes out right as well so I want to double check all those numbers and be sure they come out right. And then I can look at okay my original problem. What if I did I could do df $a gets re scale 01 of df $a, all right. And now if I look at my data frame, that one is re scaled and I could do the same thing with B, C and D. And I can say now by doing those simple things I've used my function on each of the columns that I wanted to use it on. I didn't have to worry about forgetting to type something quite right in there. So, if we had a change in requirements now we only have to change it in one place. I'm going to stop here at the end of this video.