 So there's also an option that you can where you can click and it says install dependencies It's always easier to click back, you know, check it out because then you would you don't have to worry You would install all the packages that are required by that package So once you've done that you should be able to type library some are and it should not give you an error Maybe we'll give you something like a loading library input Because it needs it for me. It's already loaded because I've loaded some are before so it doesn't give me the message anymore Okay, everyone still waiting for it to download or waiting for someone to come see you. Okay It's all right. If you if you haven't managed to load it. You can stay here for lunch and Have a couple of hours to do it Yeah Everything When you love the package it will it will give you an error if something's missing So when you load the package, you will contain all the functions all the data that are needed all the instructions So it's just like just just like our but it's a smaller version that only Contains what you need Yes, you could but you need to you need to download the source code Which is of course available because it's free right so you can unlock the source and you can edit any of the files You can change it you can modify it to suit your needs of course you need to know a little bit more to do that But it's fairly easy. There's a lot of documentation on how to do it So in my group we've got we've created, you know dozens of packages that are available and things It's very easy to do Yeah, you can it's it's all of it is open source open access you can Yeah, so when you don't know the package is going to download the binary for your computer like for windows You can download the binary and things but the places where it gets the package There's always this the source code and you can download the source code and look at the code into everything Yeah, it will give you the instructions, but you won't look I mean you Well, you can you can look at the source of art But you won't get all the details because it's hiding some things from you because you don't need to see them Okay, so we're just gonna take a few minutes to Just talk a little bit about our programming So I wanted to say that ours a true programming language So even though we haven't looked at that yet. So you have if statements you've got four and while loops Just like any programming language And I think it's a good time to take a break because this is where we take too long and we'll look at that after lunch And we don't have too much on the our basics after that So we'll be able to move on very quickly into the exploratory data analysis Which will be slightly more interesting because we look at a few things and you'll see the power of our very quickly Any questions before? We go for lunch Okay, so we're gonna start again Before we start is there any questions? So I know this morning's been a bit slow, you know, we're going slowly through all the things We're gonna finish that so we're gonna talk a little bit more about the our Programming language and then we're gonna move on into application the exploratory data analysis We're gonna see some real data sets how to visualize these things and so forth. So hopefully there will be more interaction afterwards Okay You're late Francis so we've seen we've so we've seen the basics of The the all language but in fact are is really a full programming software So this means that you can write your own functions to do things done and not really available in our So let's say you've got a special data set that you wanted to something That's very particular for that data set that is not building into our well You can go ahead and write your own function to do it Not only you can write your own script But then you can create functions that you can reuse in the future that will be adapted to various things So this is what I mean by a true programming language So one of the first thing you use in programming is called the if statement So typically you will have executions and things that depend on what? what the variable is or The value of the variable and so forth So it's very important to have this if statement that will be sort of conditional on something else So here's an example So I've cut it something very simple here if you look at this What do you think this is doing? Well, it's saying if X is greater than zero print X else print minus X What do you think this is doing? Absolute value right because if X is positive then you just print X if it's negative You take the negative sign of that which would be positive All right, so when you take the absolute value of X in our this is just what it's doing It's just looking at the value of X if it's greater than zero It just returns that if it's not it returns the negative of it. Okay, so let's look at that example X is minus two Okay, and we're gonna do this little exercise to do that Since it's minus two the absolute value is two and therefore it's printing two Okay, so you need this sort of if statements in order to know what to do depending on the value you're looking at You can have if and else if statements so could be if X is greater than zero Otherwise if it's zero and then else something else So you could have different cases that you can play with okay So you can divide it and put as many else health as you want. So you could have if X is greater than zero if it's one if it's two and then you could have different cases So this is that this is the exact same thing, right? But if X is zero then we know it's just you know, it's just to show you an example another thing That's very important when you do Programming so what is the goal of programming is to let the computer do things that? Either you couldn't do by hand or would take too long or needs to be repeated many times So that the computer can do that really silly right right by hand We can do it But it's gonna take us a long time to repeat an addition on a million numbers, right? But for the computer it's very easy and in order to do things automatically and repeat things We need what we call for or wide loops. So this will just try to Repeat something many many times or on a vector or something like that So let's look at an example to understand so let's say and here is a million Okay What we're going to do with the second comment and we're gonna see sign next so I'm not going to And just to explain that in great details, but basically what this does is that it will generate And random numbers from a normal distribution with mean 10 and start at aviation one So this is just a way to generate some random numbers And I'm not going to show X because there's a million number, but we can look at the first 10 numbers Okay, so you can see that it's close to 10 because the mean is 10 But of course, there's some noise there's some variability because of sound deviation is one R is vectorize so we know that we can take the square of X And what this will do is that it will apply the square function to each element of the vector X This is what we've seen earlier another way to do it will will be to say Okay, we've got a vector So what don't we loop for each element of X? I'm going to take the square and I'm going to assign that to a value of my vector So what I can do is that I? Can generate a vector of zero of length n that is a million So this is just a vector of zero Okay, the length of Y is just n which is the million Okay, and what I'm going to do is that I'm going to loop over all the elements of X and Every time I have a new value to take the square and I'm going to put it into the vector Y So this is what you do with a for loop So this says for each I going from one to n I'm going to Replace the value of the if element of the height element of Y with the square root of X of I Okay, so this is So here I wanted to do the square sorry Doesn't really matter Just an example Okay, and we're going to take the square of that So this is going to do exactly the same thing Okay, and if you look at Y from one to ten It's going to be the square of each of the values Okay, is it clear what this statement is doing? Okay, just for each I in one to n you're going to do that Okay, so this is slightly more advanced don't read too much We're not going to play with programming too much, but I wanted to show you that R is a fully Programming language you can and you can do your own scripts and things hopefully Soon enough, you know once you'll play with R you will have more experience with it You will be able to write little scripts that can do things for you in a more efficient way But one thing that I wanted to bring up is that So I said you could do it this way Can take Y and you take the square of X It's vectorized or we could do it You know almost manually tell the computer exactly what to do loop over all the element and say for each element Take the square of it. So what's the difference? That's a lot slower right so if we loop over all the elements And it's going to be very slow compared to do the operation and the vector Well, you need you don't have to initialize But you need to tell him how big Y will be because if you don't do that You won't know that Y is a vector. In fact, you won't even know what Y is so you need to Specify Y before and in fact the way you do that in R you need to initialize because when you create a vector It will always be initialized So the actual value of Y So here I could have put one for example or minus 10 or whatever It wouldn't really matter because I will just overwrite that but what I need is to create a vector of lens and okay, so The point of this exercise as you can see it's it's very challenging and intellectually it's very interesting But it's really to show you that if you do a for loop and it's going to be a lot slower than just doing X squared Right, it's because R is vectorized So one of the default of arts that it can be slightly slow sometimes And in order to be efficient you need to work with vectors Okay, do things on vectors directly do not loop over all the elements because it's lower because are the way I was designed If you tell him if you give him a vector and you say this is going to be an operation on the vector It's going to be a lot more efficient because it's going to know what to do a lot more efficiently so this is just For you to tell you to be a little bit careful about these loops that can be very slow in R Another way to do a loop is with what we call the wild loop So for the for loop we tell him exactly from which Element to which element would go and we're going to loop for each eye from one to end But we could just do we could loop Until a condition is satisfied and here we could have a counter that's a we start in one until it lets We're called an end which is going to loop and do the same thing and we're going to take the square root Or let's say the square here to be consistent We're going to take the square of that element and once you've done it you Increase the counter and then you keep on going until you are less than we call them in so it's the exact same thing We'll give you the exact same answer But it's just it's a different way to do it and it's also a different way to do things because you can't have Conditional statements depending on other values and so forth So I will show you a more advanced example of that later on is this sort of clear what the idea of this is Counter is just it's just counting where you are So you're looping over the vector you start at one and you say okay if it's counter is less We call them and I'm going to increase the counter and I'm going to take this You just increase it until you've done it for the end elements Okay So we've done that just to show you that you can do lots of things you can script you can loop over things So for example, you can apply the square root to a vector x and When you compare the execution speed it's going to be a lot faster when you do it on the vector directly So as I said you can also create your own function So when we look at the absolute value of x right we've got the if x is positive return x Else return minus x it will be nice to write a function that would do that In fact, there's one in arts called apps for absolute value But you can create your own function to do things so that every time you're going to apply that function to a new value You don't have to recopy everything right you can just apply that function to that value So here's a nice a very nice function that I'm sure you're going to use a lot It's called my first function and it takes a few arguments. So the first one is your name Which is a character first one is my name and there's a default value And then there's a number and depending on the number what it will do if number is zero it will return your name Else it will return my name It's a great function Decided to write a package and I'm going to submit it to r so Okay, so we're going to copy and paste that function Okay, and then r knows it's a function so it's going to be like the other functions we we're using r You can just apply that to Um The variables you have so here, you know, your name is francis number is one So we're going to try that Because number was one is going to speed up my name If you try zero Okay, it's francis if you use something else That's going to output that name So the beauty of a function is that you don't have to Recopy and paste the function every time you're using it You can just Modify the arguments that you want Also, something I wanted to show you is that when you write a function You can have variables that have defaults like this one is a default of raffael and this This one is a default of zero. So you don't have to specify these two You don't want to Okay Here's a more advanced example here Okay And I call that function My square root So if we apply that to 81 It's nine So forth, okay So What is this function doing it's actually computing the square root of a number using an algorithm called newton raphson So i'm not going to go into the details But in fact, this is exactly what r uses to compute the square root of a number Because there's no magic magic formula for the square root of a number, right? You know it by hand for by head for some numbers or you can do some calculation But you cannot just guess the square root of a number. It's not like doing an addition Or multiplication, right? So you need something that will be able to know. Well The square root of a number is that if I square that number obtained the number you gave me But you can actually do something that will try to Calculate numerically the square root of a number And this is one way to do it. So in r there's a function that does it and someone has cut it what I cut it just right here Okay, of course, it's a bit more efficient and it's better in orbit just to Show you an example that we can do very fancy things with with functions Say that again Oh, so you're asking if if I could do this for example Bring the product functions That's when you have to change off line after a break. Yes Can you Will definitely need the same line you you could but there's a couple of reasons why you shouldn't do that The first one is that because it's going to be very hard to read any code If you put everything on the same line, right? You can have a line with a bit on characters So it's not very pretty to look at especially when you write a lot of code You need to be very careful about the sort of things you do Typically, you also need to indent your code to make it easy so that you know, there's a jump every time there's a new A new thing you want to do But you could but you have to be a little bit careful. So for example this one You could do x and add something else here If you want to put it on the same line you need to put a semi-colon here So it's better to put each operation on a separate line Yeah, yeah, so that's a good question. So typically It depends right if you want to if you use it a lot what you would want is that every time you load r You will know your functions, right? So what you could do is that you could create a new script Okay, let's say I'm going to create a new one here You don't have to do that. It's just to show you Okay, and I'm going to call that one test that r And I'm going to put it on my desktop Okay, then it contains my function then what I can do is that I can what we call source the file So it will actually execute everything that's in the file so that I don't have to copy and paste you will know the functions Still this is not really a nice way to do it because every time you're going to have to source the file The best way to do it will be to create your own package that contains the function You just type library my functions or whatever you call it and then every time you load the library You will know the functions you use and this is a very efficient way to do things Of course, sometimes I just test a few things and typically when I do that I just have my script. I put all my functions at the top of the bottom I copy them before and then I I execute what I want Okay, so when you just test some functions just need something very quickly I just write it somewhere on my script. I just copy and paste it and then r knows it I don't need to input that that function in more until I quit r and I relaunch it other the next time But in general once you you're starting to to be very comfortable with r we better to try to Create your own library that you can load every time you need it Yes, but at least you don't have to copy and paste all the functions. Everything is is Loaded when you type library Any other questions Yeah Say that again You mean always in like global or local is that okay So the question is Let's say in my function here. I create a variable. I actually let's try that Okay, I assign a variable in my function You can you can try the same so copy and paste that And then I'm going to execute that function And you would think that because I assign the value 3 to y then now y should be 3 It's not okay The reason it's because I assign the variable within the function And this is what we call a local assignment So it's only going to be known In the environment in which you do the assignment If you want to do a global assignment that is something that's going to be known in r everywhere you're working You can do like this so you put two Two less signs like that and this will be a global assignment and this will be known in r everywhere But this is a really bad idea. You should never do that because global assignments can be very very tricky Because then you will think that you've got a variable Everywhere, but then if you reassign a value you won't know it anymore. So it's it's typically a bad idea Never do global assignments So forgot I just said that You cannot do it Okay Okay, so we're seeing how to create a function Oh, yeah, there's yeah, go ahead That's a good question, but I They might be actually that's let's try So you you can You can lock some variables. So typically global Yes, typically global variables that are part of r would be locked out. That is you cannot overwrite these variables And in fact, you can even do that yourself when you do create a variable You could also lock it so that no one can modify the variable. But again, that's a really bad idea to do I mean, it's it's good for r because there's the core variables But let's say so there's the absolute value function right And here we had So I could do that, for example, could try to reassign absolute value to that function And we'll see what it's going to say Okay, if you don't give me if you don't like oh Actually here was a bad choice because there's already the absolute value here. So But it's uh, apparently you can do it, but it's a bad idea to do it. So I should have tried something else actually Yeah, so you Yeah, even still here. It doesn't really like it. So I guess It it's probably going to give you an error if you try to do that Okay, so we've seen how to write functions. Why don't you try to write a function? Um, and you can call it whatever you want That will create um, that will compute the inverse of a value x So your function should take one, uh input value, which is x and it should return the inverse of x Very very difficult function to write So try to do that. So just follow the the template that you have Take x compute the inverse and and return that I'm going to help you a bit. So this is what it should look like So let's put my inverse Should take a value x and it should return something you should do something inside Try to think about it So what you could do is okay, let's create a new variable one over x and then I return That value y If you really want to be explicit, you could type return y You you could do that. There's just I wanted to make it more explicit just to show you. Yeah. Yeah, you can exactly Which is in general, you know, you would want to make um some temporary assignments and then return that at the end Okay, so this should work and of course we'll return the inverse of x because you're just doing one over x Let's let me check whether x is there Yeah, so now we're going to try to check whether x is zero or not So we're going to use because dividing by zero is kind of a bad idea, right? In fact r knows how to how to deal with it If you divide by zero It will know that you know the limit is infinity and you will know how to deal with it Okay, but here we're going to do something like okay if x is uh Zero we're gonna We're going to print a warning okay And otherwise we're going to say it's fine And we can just return that Okay, so this will test if x is zero if it is it will print are you crazy And otherwise it will just return the value Okay, and this is your function. You watch fine any questions exactly Any number? I just want to Make a special check if if it is zero. I'm going to say be careful because you can divide by zero What is x Okay Okay, that's it's okay, so that's a good question. What if you input something that's not a number right for example like a character Well, you're going to be in trouble because you don't know what to do if it's a character And you're probably going to get an error in fact you can try so Something like that, right It's going to tell you it's not numerics. I don't know how to compute the the inverse of it, okay so something I should say is that One good thing about ours that it's what we call object oriented So you can define what we call class and methods So a method is a particular kind of function that we'll know what to do depending On the the kind of object your input So you could define various functions whether you've got a character whether it's zero whether it's positive Whether it's a matrix whether it's a vector and then Depending on the type of the input the function would know what to do Basically, you would have sub functions for each of these Each of these different cases and then The function will know which one to use at which time. So this is a beauty of r. It's that it's object oriented So you can do some very nice and easy programming that will take up all these special cases for you For example, uh, if you plot Let me give you an example Well, we'll see an example of that later When we'll look at the summary function Okay, so We know how to write a function. We've looked at That more advanced function, which is computing the square root