 Can everyone hear me? Just to be sure that I'm not too loud or too quiet Just shout and chat. All right. Tocqueville says yes, we can hear you very good. Very good. Very good. Very good Yeah, so Let's start then. I hope everyone had a good week. I hope everyone was was able to do the Do the assignments We've had some votes on the poll that I put up on Moodle, so that's all good So, yeah Stream will start soon. No, we already start it. So today more introduction We had the first introductory lecture last week this week. We will have another introductory lecture It's it's just gonna be very very basic are So you will have two lectures followed and we will not even have discussed how to load in data Matta cloud. Thank you for following Is the following sound okay, or is that too loud as well? I Didn't change it from last time and if we don't get too many followers, then it should be fine Didn't hear it. Oh, okay Yeah, no, that's true. I actually muted the follower sound so The next follower should be should be audible All right, so yeah more introduction but of course first we have two things to do so In which order do we want to do this do we want to first do the answers to the lecture or do we first? Want to look at the zoom meeting votes? Answers, okay. Good. All right. So my answers for lecture one So I'm not going to do the question zero thing because I hope everyone was able to install our And install a good text editor So that should be fine So These are my answers. So the first question was just to use our as a calculator So hey, you just have to type in stuff like 123 plus 567 And then some other things the logarithm is just log which is the natural logarithm So if you want to have the logarithm base 10, you would have to type log 10 You can divide you can multiply you can do Euclidean division Actually, we ran into an example of Euclidean division yesterday Since people generally ask why is Euclidean division meaningful, but We were talking about Mother's Day and how it moves every year And that's of course the same as your birthday like if you're if your birthday is on a Monday Then next year your birthday won't be on a Monday and that of course is because there's a difference in 52 times 7 And then since there's only 365 days in a year You you end up with kind of an overflow and Euclidean division would be able to tell you if you Would be having your birthday on a Monday or on a Tuesday or on a Sunday next year So Euclidean division is just done by the percent percent Or the Euclidean division remainder, of course in this case it should be four Square root if you want to take the square root of a negative number remember that you have to add the plus zero i You have to tell our that they are imaginary numbers So let's quickly go to our And show you guys that when I copy paste in The answers that it will just work So hey, you can see that it just adds up numbers. So hey using our it's just a very fancy calculator And that that's because it's a Rapples system, so you just type into and it just executes your commands one by one So nothing to do yet for okay, so there's the sound Evo Lucy. Thank you for following Sound not too loud. I Think I can just talk over the sound, but I hear it relatively loud in my headphones I was listening to some music before we started so Still didn't hear it The hell's going on here Well, then the sound might be broken and then I have to check the code But I'm not gonna do that now Very you can hear it as well. All right, then I will put it up a little little little bit more So I'm just hoping that I don't get I Don't need to hear it though. Yeah, no that that's true That's true. It already shows on the screen right that people follow. So that should be good enough All right, so let's go back to my Questions so in the next part So hey, I hope that everyone understands that we can just use ours a calculator And then of course we want to define some vectors because hey It only starts becoming fun when you have more than a couple of numbers and when you do things like vectors So the first question is is use the Combine so the C function to create a vector from one to ten Of course if you want to do it with the with the combine operator Then of course you have to specify all of the numbers independently You don't really have to You could answer this question Like this as well, which is a little bit sneaky But this would work as well. So and then you don't have to type one two three four five six seven eight nine But first question just use the C function. So C is a function So remember functions you call using round brackets not square brackets round brackets Vector to be so head using the double point operator So the double point operator is very similar to the sec function which defines a sequence But it has the bi parameter So the the step size is always one or minus one when you use the double point operator So the double point operator can go up and it can't go down But it will always go up and down by just a single number Um question to see we can use the sec function to create more complex factors create a factor from one to a hundred going in steps of five So you do that like this. So you just call the sec function the sec function has three parameters It has the parameter from to and then by All right, and then we come to the really well the first question Which is a little bit harder and where people have to think about it and this already becomes a little bit tricky, right? So the question was Use the letters constant and the sec function to create a vector that stores all the even numbers All the even letters, right? So the even letters like numbers can be odd or even so odd means that You're not divisible by two even means that you're divisible by two So, of course, there's 26 letters in the alphabet. So the even letters But we can specify the even numbers by going from two to twenty six stepping by two And we can then use this vector that we create to select from the letters vector So letters is just a vector. So if we select something from a vector, we use square brackets So we just say letters square brackets open and then we put in the sequence that we want to select So we want to select letter number two four six and so forth Yes, so that's what we do using the sec function and now vector 2d will contain all the kind of even letters Although letters can't really be even. All right, and then to question 2e was what is the type of vector 2a? Either use the class function or ask explicitly using the is numeric So here just to kind of show you guys that a vector is always of a single type, right? So vector 2a is of course a numeric vector because we only put numbers in there And then we can ask what is the class and then it will say numeric If we want to use the class later on in things like an if statement All right question from general Gulag. Could you leave the buy is away basically like yes Yes, it has three parameters and it will automatically go The nice thing by specifying the name of the parameter You can actually move it around so this would be the same thing So buy five one to a hundred And if you name your parameters, you don't have to adhere to the order So the order is from to buy but if you specify them yourselves if you explicitly name the parameter You can put it anywhere you want Question from Rigoletti, can you quickly operate 2d in R? Yeah, yeah sure sure sure sure sure So let's go to R. So let's show you guys the R window. So 2d That's the one with the Letters, right? So if you would look at just letters, right, then it would just contain all of the letters And if you would say sequence From two to twenty six by two Then it will just give you the the ones that we want to select so two four six eight and so forth And now we can combine it so we can just from letters select this And then you get the the even letters Or was this to see no this was 2d. It's clear. It's it's it's just selecting from a vector And many people don't realize but like these build-in constants like letters with capitals and small letters And even the months like yes, so you have month dot up Forgetting it. So these are the abbreviations for the months. So if I want to have the Even months of the year then I can do the same thing I can say check from two to twelve since they're twelve months by two and now these are the even months So February, April, June, August, October and December are even So hope that that's clear. All right, let's go back to my answer stand so, yeah So you can use the class to to get a character which holds the class But you can also ask explicitly and this is very handy Because the is numeric will give you a logical value Which we later can use in things like for loops or while loops or if statements So that's the way that you want to do it And then to F is Was a question head when you combine vectors to a and 2d What is the type of the resulting factor so vector 2a is a character vector or is a numerical vector Vector 2d is of course a character vector because it contains letters And if we combine them together into a single vector, and I will show you how this looks in R So when we go to R Like this then we can combine them. I didn't save them. Let me first run the whole code so Defining all the four vectors. All right, so now when we try to combine these two vectors into one Then what you will see is that now everything gets these double quotes surrounding it. So that means Yeah, because a character cannot be transformed into a numeric are does it the other way? So it takes the numeric values and it then transforms them into kind of character values And then of course when you combine them and then you ask for the class of it And then of course the class of the whole vector gets upgraded to being a character vector And that's the level or the lairdness of the type system Hey, it will always try to put it in the lowest type And in this case the lowest type is character because numbers can be Caught in a character vector, but the other way around is not possible All right, then the next assignment was a little bit matrix says so the matrix says We can look so the first question is is we can use the matrix function to create a matrix create a 10 by 10 matrix That holds the numbers one to a hundred So of course we can just use the matrix function and I'm just gonna store this in variable 3a So let's quickly move to R and define this matrix and now when I type matrix 3a, of course It will show me the matrix, right? So the first thing that we notice is that our fills matrices on a column wise base So it first fills column one then it fills column two And then if you look at the help file of the matrix function, so question 3b You see that there's a parameter called by row. So if you use the by row parameter It it fills it the other way around so it first fills row number one then row number two So let's do that. So let's define matrix 3b and now we're just going to say by row is True and now when we look at matrix 3b, we now see that it filled it the other way around So we just go by row like the parameter says All right So question 3c select the fifth column of matrix 3a and select the fifth row of matrix 3b So, of course, these will be equal. So from matrix 3a, I can select the fifth row by just saying fifth column So comma five, right? So I say give me all the rows and then Only column number five. So that's why I don't put anything here. I could put like One to the number of rows of matrix 3a But this is kind of the default value, right? But so you don't have to specify that you can just leave it empty and usually For clarity, I would put a space here So just to make sure that it's obvious that you're selecting the column and because in in the in this in the indexing Hey, you first of the rows and then you have the column So I generally tend to put a space there to kind of visually have the Q that I'm selecting all of the rows So hey, this is the fifth column from 3a and the fifth row from 3b And of course, that will be the same. So both of them are vectors They are of length 10 and they contain numeric values All right, so then Question 3b our 3d was how can we translate matrix 3a into 3b? and here there was a little typo because it's There should be a hint Because the word here is how can we translate and I didn't mean translate But I meant transpose because we looked at the transpose function So hey, if we would take matrix 3b and we would transpose matrix 3b Then instead of being now on a row-wise basis, it will now go to a column-wise matrix So that means that when I transpose matrix 3b It becomes identical to matrix 3a and of course we can we can check that that is true So I can just say is equal to which is the double is because a single is character is is Assigning to so we don't want to assign to but we just want to check and then we can say matrix 3a And now it will say true true true true true for all of the values in in these matrices Is there a difference to using this command as opposed to the command from 3b the transpose function you mean so using the t on a matrix When when you're defining your matrices yourself, you generally don't transpose them so you make them in the correct format but if you It's often that when when you're doing bioinformatics, right? Or when you're doing just programming then generally you get data, which is in a certain format and for example you download data from a website online somewhere and They might have the data in the opposite format as that you want Have for example if I look at matrix 3a, right? Matrix 3a right and I want to calculate the correlation, right? So that would be a bad example because the correlation would be one I think for each of them So I can use the core function to calculate the correlation matrix and the correlation function correlates column 1 with column 2 column 1 with column 3 column 1 with column 4 and so on so it works on a column wise basis so if I want to get so Let's just give some names to this thing So add column names using the letters constant. So I can say call names of matrix 3a is letters And I have 10 columns. So I take the first 10 letters, right? So now when I look at matrix 3a it now has letters on the top and I could for example give it also row names so I could say the row names of This matrix are not the letters, but let's do 3f so you can use the paste function to say Well, I want to have the word individual and then combined with one to ten And if I would do this like this without assigning it then it would just say individual one two three four and so on and of course I can do this and As a little trick, right? If you already have written what you want and then want to assign it to something you can also use the forward pointing arrow So it just the arrow just points to where you want to assign. So I can say something like this as well So assign this what I just created into the column names of matrix 3a and now when I look at matrix 3a Then you can see right that now I have column names and individuals here are in the rows So imagine that I'm using the correlation function, but I actually want to have the correlation between individuals Then of course I I can I can use the transpose the transpose function, right? So I could say correlation of matrix 3a Which would normally give me the correlation of each column with each other column And of course, this is a bad example because they're all one But if I want to have the correlation between the individuals then I can easily do the same thing But now I just have to transpose the function to transpose the matrix before throwing it into the function And then of course now I have the correlation of each individual to each other individual so the transpose function is really useful when you're When you're kind of layout of the matrix or of the data that you have loaded is in the opposite format Compared to what your function wants and many functions work on a on a column based system And some functions work on a row based system So it really depends on the function that you are trying to use if you have to transpose your matrix or not And I think those were already it so those were the assignments So we already used the paste function in this case in the assignments. It should be measurement But have here we just used it for individual So as a little tip the the the arrow can be turned around and you can assign stuff instead of having your variable Arrow pointing in the variable and then what you want to sign to it What is paste zero good for so paste zero is the same as a paste But if you do paste right, then you can see that it automatically has a space Between the first thing and the second thing that you're trying to paste paste zero Doesn't do the space. So it just paste things together So and of course you can do so paste zero is just defined as paste Where the separator is nothing? That's the only difference Paste zero is equal to paste But it just said that the default separator is different. So the default separator for paste is a space and for paste zero It's nothing So it's an empty string like this Any more questions? So I hope everyone was able to do the assignments and that that everyone was able to figure it out I think the hardest one is the the even numbers and even letters Because there of course you have to take two steps have one a step is to Realize that you can use the sec function quite easily to get all the even numbers And then of course the second step is to realize that letters is just a vector So you can just select from the vector using the square brackets and then putting the things in All right, so I will put my my answers online. So this this file I will just put it online on Moodle for everyone to see and That's kind of It for the assignments. So I hope that everyone was able to do them Make sure or at least like program cleanly, right? So give it a header Write down the names of the questions and make it look good things which look good tend to be more correct than things Which is sloppy, although there's no direct correlation. It's just something that I found so all right, so then for the Oh the little Let me see. Let's go back to the platform to zoom meeting. So Last week I asked you guys if you wanted to do a one hour zoom meeting So that we so that I can help you with the assignments or that you guys can help each other with the assignments And I think it would be a good idea So we put it on Moodle right and everyone voted or not everyone voted, but at least we had some votes and Instead of just telling you what the what the result is I Thought it would be nice to just load it into R. Oh Okay, so now we should have the sound crazy umbrella. Thank you for following sound not too loud Can be did people hear it now or is it still broken if it's still broken then I will just disable the Sound altogether because I hear it quite loud in my headphones So What a beautiful, yeah, yeah, I spend a lot of time I actually have another sound like if I say hi bot because that I just have a little robot listening to the channel And that's under my name. So when I say hi bot Then you get like the little witch cackle in in the background That's just for me to check that the overlay is working and that it follow that it can follow the followers and follow the chat All right, so the votes for the zoom meeting, I don't know exactly how many votes we had we can look at that but of course, I thought it would be nice to just Analyze it using R because that's what we're here for right. So why not use R to look at the votes? so So Here's a small script to tabulate the votes for the weekly zoom meeting Again copyrighted by me first written then and I didn't put any comments in there So let me go. Let me show you guys actually if I if I take the results from Moodle I just get a little comma separated file and fortunately, this is more or less How do you call it anonymized, right? So this is how it looks like When I downloaded the file from Moodle, so you guys vote it I don't know how many people exactly vote it But you see here that there's dates and the times and this is just the format that you get from from Moodle So I made a little script to do that So the first thing that I do is set my working directory and in this case It's of course in my downloads folder because I just click download and then save So let's go to our move to the downloads folder and of course I have a bunch of stuff there, but Also the votes are in there, so I can just go there, right? So now when I do like a deer Then I see all of the files that I downloaded since I started using this computer, which is just like a lot Which you can interested in what I have on my downloads folder But the the file that I downloaded was called and let me see that so if we go back to notepad plus plus Then the file that I downloaded was vote for the weekly zoom meeting date and time dot csv So in R you have a function which is called read csv We will get into that function in detail when we do lecture number three to load data But the read csv function takes as a parameter the file name that you want to read So and then it reads in this thing as a little matrix So this thing has two columns And the first thing that I do is read csv So I read this file that I just showed you guys and I read it into a variable called votes So let's do that and show you guys what happens So let's go to votes and then what when I type votes It looks like this So it's a little bit unstructured But the way that you can can see it is that well you have the first is the response number So in total 17 people filled in the questionnaire And then in the second column you see the answer and since that since people could Choose multiple times These answers are separated by slash n and the slash n means new line So that's the enter key on your computer Or on your keyboard So the slash new line is separating each of the answers. So some people only chose like Two dates some people chose one and this was me choosing all of them Because well it was my poll I was the first one to fill it in and I don't care because like I was available all the different times So that was good so the first thing that we want to do when we want to analyze this and make a little graph out of it is to Take the second column, right? So I did this very explicitly step by step So I just take the second column which contains the answers and then put it in something called vote vector right because votes itself is a matrix which has two columns and 17 rows and now I just take the second column which means that I get a vector and Hey, of course selecting a a row or a column from a matrix will always give you a vector. So So let's do that. Let's go back to R for you guys and now when we look at the vote vector It already looks a little bit better So now it's just a single vector which has 17 elements and you can see that from from the numbering in the front And now each of these answers still has like the multiple new lines in there And or if people answered multiple times So the first thing that we want that I wanted to do was use the string split function Right because I want to split all of these answers from each other because I just want to know How often people chose Wednesday 15 or Wednesday at 3 or how often they choose Wednesday Tuesday at 3 So there's a function called str split which splits strings into their individual Components so if we look at the vote vector we can split it By this new line character right which separates the different answers And then what we get is something which looks like this So it sneakily turned it into a list And that's you can see that it's a list by the double brackets Like I told you last week because a list is something special And because now at the first element of the list There is a vector and this vector has a length of six So the first person who answered it gave six or selected six different options The second person that answered it gave two different options or selected two different options So but I don't want to have it in a list. I want to have just everything in in kind of one vector So then I can use the unlist function. So the unlist function Will take a list and then just put every element of the list behind each other So if I do an unlist on this If I do an unlist on the list that I just created by string splitting Then now it looks like this and now you can see that we have all of the different votes And now it's not clear That the first six came from the first user and then the next two came from the second user But that this is something that we can save let me look at my cheat sheet And I saved it in something called separate votes separated votes So the separated votes now just contains all of the individual answers that people gave and where we've kind of Removed the fact that one person could give multiple answers So now the only thing that left to do is to table this So we can just make make a table of this and then r will take all of the elements And count how often each element occurs So we see that there were two votes for monday at 11. There were four votes for monday at three And we see that we have a tie for tuesday and wednesday so tuesday at three and wednesday at three had a tie We can actually make it a little bit better because we can actually just say boxplot No, not boxplot barplot Yeah, we can barplot it and we can create a nice barplot showing the votes that people gave So here you see the individual time points. Let me move it up so that It's a little bit too small and A little bit more come on Come on axis. No, you can see that it doesn't it doesn't have enough space to print this axis So can I do that? Well, it doesn't really change the the size of the axis But this was the the voting layout if we wanted to show it in a bar graph But tuesday and wednesday ended up being equal But someone mailed me that they wanted to change their vote Which you couldn't because it's a vote once So let me see what the change was Yeah, so skritta mailed me saying that Why is the barport limited to 10 but the number of answer it is? Oh, I didn't See that. Oh, yeah, it doesn't have the full axis Well, it's higher than than 10, right? It it it's just a barplot. I I don't like the barplot that much, but It's it's just it doesn't have a real axis at the bottom either And I kind of like I try to actually like make the axis a little bit smaller And so You could do the same thing like this cx is 0.7 to force all of the numbers or force all of the letters being plotted to be smaller And we'll get to that And now you can see that at least this axis now has all of the different Things so cx stands for the magnification of the point size But the y-axis I have no idea why it doesn't put the whole axis here No idea. I I know that I could probably do something like this To to flip everything around now at least the y-axis is in the correct ordering But not or at least the correct way of reading the numbers, but now the bottom is not but But I don't know why it doesn't plot 11 or at least 12 No idea. I I almost never use the barplot function if I want to have barplots I make them myself. So I just use the standard plot function And and just draw the bars myself But um scurrita wanted to change her vote and she says that she voted for Wednesday But she would like to change it to Tuesday afternoon So that means that we will have the zoom meeting at Tuesday afternoon Because if we subtract one vote from Wednesday and put it on to Tuesday afternoon Then Tuesday wins by a margin of two So yeah Tuesday afternoon will be the zoom meeting So it's decided. So if anyone really really really Really doesn't want to have it on Tuesday afternoon, then I'm sorry, but I'm not going to spend three days a week to Yeah, well, it's already an extra investment for me to to do an extra hour, but I'm perfectly fine with that So um, but yeah, so Tuesday five or Tuesday at three. We will have the zoom meeting Um, I will put the zoom link on Moodle so that everyone can just click the zoom Moodle link and then just go there Um, and again, if you have any issues with that, just um, send me an email. All right. So the zoom meeting Tuesdays okay, so The meeting will be to discuss the assignments. Yes. Yes, georgio. That's true We will just discuss the assignments and um, if people get stuck then I can give a little bit of tips or help um, and the nice thing is is that uh I mean, there are not any additional info which we miss if we cannot participate. No, no, no, no, no It's just to have you guys so that hey if you get stuck Um, because like the the assignments come online on today, so um, hey, you can then work during the weekend on the assignments But it's always annoying if you get stuck That you have to wait The whole week before you see the answers to the lecture. So the idea is is that when on Wednesday, you you're still stuck or Sorry, Tuesday When you're stuck then on Tuesday, we can have like a one hour meeting and help people so that on Wednesday They still have time to finish the rest of the assignment But I'm I'm not gonna give a lecture or put stuff for the exam into the uh Will they be recorded? I could There's a little bit of a dot and shoots issue there of course because it's germany and like I could record myself using obs so I I think that that should be fine. Um, so yeah, let's just record them I actually forgot to start the recording today. I am so stupid. So remind me next hour Then I have to just get the first hour from twitch. So I hope that twitch doesn't mess up and and does save the the stream That's so stupid Let's just start it again. Anyway, it doesn't matter So the last lecture, um, we used r as a calculator We looked at the different types of data So I hope that everyone still remembers logical numeric character vector and list and matrix and data frame If we want to index data We use a square bracket a single square bracket for all of these types And we use the double square bracket only for the list type And that's because the list is special because the list can hold anything that it wants even a matrix or another list So that's why you need to double double square brackets We also talked about using the dollar sign for indexing. Um, if you want to index by name Um, I also told you about the sequence the repeat and the 120 function Is it possible for you to record the zoom meeting and upload it to moodle as the assignment clarification because I didn't know that Before about the date and I looked my worksheet next to okay. Yeah, no, I will I will record them So I will I will do my best to not forget to record them So it should be fine. And of course, if you have any questions in the assignments and you're working on it during the weekend Um, and you get stuck and you really want to continue Then just send me an email, right? That's probably the quickest way to get get out of a I'm stuck moment. Um, because it it's really annoying if you get stuck and then you don't want to wait three days Um to join the meeting So if you if you if you are working on one of the assignments You get stuck and you really want to continue then just shoot me an email I generally answer between like 30 minutes to an hour Um, it depends a little bit on netflix like if I've just started a netflix Like showed and it it might be that it's an hour and a half But generally I'm pretty good at that monitoring my emails. So just shoot me an email if you get stuck All right, then let's start today's lecture. So today's lecture. We are going to Talk about uh, well the answers to the previous assignments. We already did that But I want to talk a little bit more about variables and then we will start introducing some control structures so that we can do stuff with the variables Um, and I want to talk to you about the difference between a statement and an expression um, we will very very Coarsely look at what functions are and how we can use them and how we can create them ourselves Um, I will talk to you guys about brackets I already do that a lot like him when you call a function use round brackets when you select something from a vector Use square brackets But when we are starting to do functions and control structures, we will also introduce curly brackets So the curly brackets are the third and last type of bracket that you need For doing stuff with r Then I also want to talk to you about escaping the inevitable Because some things we want to or we might want to print to the screen or to a file But we need to make sure that we can escape some characters I want to talk to you guys about some randomness. So, um, how because r is for statistics. So it knows a lot about different, um Random distributions. So it knows what a Gaussian distribution is and it knows what a what a uniform distribution is So we will be talking about that and I will show you how you can generate random numbers from different distributions Um, and of course clean and reusable code I always try to kind of hammer that in as much as possible because like It it really helps Making clean code means that in the end code can be reused by other people or by yourself Good. So variables, um in my mind and I showed you this slide before I think variables are boxes So you can put things in and then after you put something in a variable or in a box You can use the box without knowing what is in it and that is the big abstraction of a variable right because you can have a number or you can have a character Or you can have a vector or a matrix and you put it in a variable And after that you can use this variable and you can more or less forget what's in there because like R can tell you what what's in there. Um, so the nice thing about these boxes is is that you can do things with these boxes But you don't have to know exactly what's in there. So what do we generally do with these boxes? So we we can ask things from them, right? So have they are the workhorse of programming So we do things with variables so we can ask for example the length how many how many things are in the box Um, does the box have a number of rows or have a number of columns, right? If we are talking about a matrix Um, the str function, um is really useful when you are dealing with more complex objects Because the str function just prints out a a structure of the object. So how does it look and this is useful when you have For example a list which at the first index has another list which has a matrix in there And then you go back to the first list which then the second element has a vector So hey when objects become really complex and when you're storing a lot of data into a single variable Then the str function can help you to kind of Graphically see how these how this object is structured Of course, we can ask the class and we have these two functions to force A box to be a certain type and we can ask if the box is of a certain type So as numeric or is numeric So the next thing what do we want to do with these boxes? Well, these boxes need to go somewhere So and for moving boxes in programming we have something called control structures So control structures in my mind are something like conveyor belts. So when you take a box Hey, you did guides it to the correct destination on where it needs to go So everything in programming is based on a fixed algorithm Have because we want to go and have we want to load in a matrix Then we want to do some manipulation with the matrix and then we want to write out a new matrix or a vector Or a summary statistic after we've gone through the whole thing And of course this algorithm that we are designing Contains two very important control structures. One of the control structures is for branching Like taking a conveyor belt and then sending some boxes left and some other boxes right and we have looping So looping is something that we can do A repeated number of times right we can think of a matrix and for example for each row of the matrix I want to calculate the mean, right? So then I have to do something so for each row in the matrix Calculate the mean, right? So and these are the only two Well, actually, well, there's some more control structures But these are the most fundamental control structures in any computer language So of course also in r they exist. So branching and looping So let's take a little bit of a of a closer look at branching. So branching is Done by the if statement and you can also branch using the switch statement So when I when I when you when you do something with the if statement, for example, imagine that I have a variable called box And I can put something in this box, which I don't know, right? So I can put a logical value in there or I can put 12 in there or I can put c in there So a character vector And then I can do an if statement, right? So I can ask if the class of this box So the class of the variable is a character then call the function right on this box If if the box is not a character then we want to call the function left on this box So this is just a way of Looking what is in the box and then based on the contents of the box call function one right or call function two left And this is of course Useful because sometimes you have Values in your box and then you can calculate a mean But sometimes you you have characters in in in the column of your matrix So you cannot calculate a mean And so if you if you have the if statement then you could skip The columns of the matrix which are character values so that you don't run into errors Because if I want to calculate the mean of a character vector then it will just say error and it will quit So I'll have when I then would write a little Script which then looks first what the classes of the column that I'm looking at If it is numeric calculate the mean if it is character do nothing and just move on to the next one The switch statement works very very similar to the if statement It just allows you to switch multiple things. So the if statement checks one thing And then calls a function or does something and then you have the l statement Which then is for everything which does not match the if statement The switch statement is is a little bit more flexible in a way or it's well Not so much more flexible, but it it allows you to to define three or four or five options Right. So here we say the same thing So we switch on the class of the box if the class of the box is logical then we call the function left If the if it is numeric then we call the function middle and if it's a character then we call the function right So you will use this during the the assignments to Look if a value of a random number for example is below a certain value or if it's above a certain value And then print out something Of course, we can also make multiple roots or multiple Comparisons using the if statement. So this is the same if statement This is the same as the switch statement now just written using if statements And so if the class of the box is a character Go right or call the function right on the box Else if the class of the box is logical take call the function middle on the box and else call left So here we do too. Of course, this is not exactly identical to the switch statement because the switch statement Makes all of the character boxes go right, but it doesn't have anything for a matrix, for example Well, this one the else statement is just a catch all right If it doesn't match this if it doesn't match that then we do this So it's an if else if else so the else catches everything All right, so you can of course use if statements for comparison So I can say if some x some variable x is smaller than five And so if x is a single number then this works perfectly fine And then we can for example say print x is smaller than five We can also compare two variables to see if they of if variable x is smaller than y And this this works for single numbers If you have a vector right and you want to know if all of the values in the vector are smaller than five Then you have to use the all function So you say x smaller than five So now it will compare every element of x or every element in the in vector x to the number five And if it is lower, then it will return true So in the end you get if you have three Three elements in your vector it will say true True true if they're all lower than five or if the the second one is higher than five Then it will say true false true So the all statement You can use to kind of flatten these things into a single true or false So if all of them are smaller than five then it prints and otherwise it just skips this statement altogether If you want to check if if there is any number So if one of the of the three numbers in your vector is smaller than five then you can use any So all is to make sure that everything is is true And any that means that at least one of them is true So that's the if statement It's just comparison and based on the comparison you can do a or you can do b If you have the else if statement then you could do three things or four things or five things You can you can add as many else if statements as you want. All right. So for while and repeat so This is looping So looping is like this for example, I have a a variable called box and I put a thousand in there And then I do a four statement. So I say four x in one to ten Take the variable box subtract the current value from x and then put it back into the box so when I when our hits this piece of code the first time it will do a thousand minus one And then put nine hundred and ninety nine back in Then it will recognize that it still needs to do two three four all the way up to ten So then it executes the same statement again now with x having the value two So it takes nine hundred and ninety nine Minus two is nine hundred ninety seven Then it executes the statement again Nine nine seven minus three Is nine nine four Then it executes the statement again. So it just continues The statement so you can do the statement once you can do it twice, but in this case we are doing it 10 times So the for loop is when you know how often you need to do something For example for each row of the matrix. So for x in one two So double point number of rows so n row of a matrix Because now you know how many rows the matrix has If you do not know how many rows there are or if you want to repeat something until a certain condition is reached Then you have to use a while statement So a while statement is a slightly slightly more complex than the for statement because you have to define this variable x yourself Here in the for statement. We are defining x But here in the while statement. I have to Do that beforehand So this is the exact same thing as the for statement you can see that it's almost twice as long But it does the same thing But the advantage of using a while statement is is that you can use it without knowing your bounds so to speak So if you don't know how many rows there are in the matrix Well, that's not really something that occurs. Yeah, but you could do something while something is true, right? I have a sensor and this sensor measures temperature And while the temperature is below 30 degrees celsius do something For example add the temperature to a plot But I don't know how long the temperature will be below 30 degrees celsius It could be like one day, but it it could be an hour. It could be like 30 seconds And so the while statement allows you to loop an undefined or an undetermined number of times Which also allows you to loop forever Which is really annoying in r because it won't tell you that it's looping forever And you can wait like minutes until you get the output But this does the same thing So we define our box again Then we define a variable called take out which sneakily is of course similar to the x variable that we have here And now I say while take out is smaller or equal to 10 Do something so I I do the same statement. So box minus take out is box So so and now I have to remember that I have to increase the value by one So I say take out is take out plus one and of course this this statement here is the same as what happens here So x in 1 to 10 Take out is take out plus one and then I compare if the take out value is smaller or equal to 10 If not, then I then it executes the statement again Or if if this is true, it executes the statement again If this is not true, then it just continues with the rest of the statement The repeat statement is also there. I never use the repeat statement. So just forget about it It's there you can and there's also a do while and stuff But everything you can or ever everything that you ever wanted to do in programming You can do with a for loop or you can do with a while loop So the for loop allows you to iterate between known bounds If you have unknown bounds because you you don't know how long something will or how often you have to do something Then you can use a while statement And then usually I say at this slide that's it There's nothing more to programming If you know what a variable is if you know what a control structure is and you can you can write an if statement And you can write a for statement Then you can program anything Because your cpu in the computer Does not know anything more than this it knows what a variable is it can do branching based on ifs And it knows how to do a looping statement like a for or a while And and there's nothing more Everything that we will see from this point on Can be expressed by these three very basic concepts variables branching and looping. There's nothing else Good. So then we're done with the first part of the lecture Can we quickly run them? Yeah, sure. Sure. Sure. Um You want to you we want to know the answer what's in the box? What's in the box? All right, let's go to uh to notepad. I will quickly type them in so and this is why I use This is why I never directly type into r right Because if I would type directly into r and I would type for x in 1 to 10 Right, then now I have to do something and then I have to press enter and now you see it changes to a plus and now I can close it. Ooh, there's so no write your for and Write your if statements and and while statements in a text editor and then copy them to r. It's just so much easier Um, all right. So we want to know what's in the box So let's make a new one What's in the box and we just save this as example one All right, so um, we put some value in the box a thousand then we say for x in 1 to 10 Oh, that's a little bit too quick and then we say box equals box minus minus x And then we just copy paste We go to r and we paste it in So it does this 10 times and now we say box And then there's 945 Surprising right because people never think that there's so few things left in the box But you take out one thing then two things then three things then four things then five and six and seven Then eight and then nine. Uh, the while statement works exactly the same. So the while statement is The same so we have Box again being a thousand Then we say while and we have to define our own iterator. So generally you would use i or x So let's use x right so x is 1 so while x is smaller than or equal to 10 What do we want to do we want to say box is box minus minus x and an x Is x plus 1 If I don't do this if I don't Submit this line it will run forever Forever and ever and ever and we'll never stop. So you you have to Increase x right otherwise x will just be one So it will take one out of the box then it will take one out of the box But since x will never be larger than 10 it will never stop. Um, so let's just copy paste this in So let me move you guys to the r window again Same thing and of course the box also contains 945 That's it And of course the if statement I can show you an if statement. So um Renif will give you a random number Give me one random number right so every time that I run this it will give me a different random number These random numbers will be between Zero and one So if I go then to my notepad plus plus window I can say well if run if one smaller than 0.5 And just print the word smaller Add a new line. So here i'm using the Slash n to press the enter key or programmatically press the enter key So when I take this piece of code, right then When I run it in r Around half of the time this should say smaller Um, and sometimes it won't say anything right because it's not it the number that it drew is not smaller than than 0.5 And of course like Kind of half of the time it will say smaller Good. All right, then, um I'm gonna do a short break I have prepared some nice gifts for you guys now. I don't know if I To cats first or squirrels So it's going to be either cats or squirrels, but uh, I'll have to see and when I get back remind me to Um start the recording because otherwise I have to do a whole bunch of video editing to get the recording out of out of twitch All right, so let me switch you guys back to the powerpoint and then I will see you in around 10 minutes so three ten and