# R Studio orientation 00:00:01,280 --> 00:00:34,000 In this clip I will just try to make you familiar with the R interface, and particularly the R Studio interface. As you load R Studio what you will see certainly when you load it for the first time, it's these three windows: one which is titled the 'Console', then you have two more on the right hand side - one which has two tabs 'Environment' and 'History'. Here you will be able to see sort of all sorts of past commands which you used in R and you can reuse them.   00:00:34,560 --> 00:00:54,240 Environment will get to this that's very important. And then on the bottom right hand side is  a little window with a number of tabs: 'Files' that is like little file explorer, 'Packages' - we'll learn about that, 'Help' - access to some help, 'Plots' - we haven't talked about plots yet.   00:00:54,369 --> 00:01:37,280 So a number of useful things which we'll get to at a later stage. Now importantly here this Console window: this is really the way you communicate with R and here you can do little simple calculations like 5 times 9 is equal to 45. But importantly for most of the sort of practical work which you may want to do you need, in a way, a fourth window. You need what's called a script, so you can create one either by going to 'File', 'New file', 'R script' or you can just press this little button here and you get to R script - here a new script. 00:01:37,280 --> 00:02:16,754 And this is now in essence just a text file but in it we will save a collection of commands to R, which then can be executed either one by one or altogether. So as a starter let's just save this under a certain name. So I'll save it under a particular folder here and I shall just call it 'FirstSteps' and it will get the extension .r and that's an indication that it will include R code. 00:02:18,320 --> 00:03:46,640 It is good practice to start this code with a comment. Now a comment in R is preceded by this hash (#) sign. Whatever follows a hash sign the software will basically ignore, but it's good practice to write for yourself something so you remember what you did here. So here we will write 'These were my first steps in R' and sometimes you may want to to write who wrote that code - that's myself and when you actually did it that can come in useful at times. So the first substantial thing you should do is set a working directory. That tells R what your default directory is from which you're working, where you get data from,where you want to save things and the command is setwd and then in parentheses the working directory in inverted commas. Now what I often do is I'll just go to Explorer and I find the working directory where I want to work in. So it's here. I just go into that little breadcrumb up here, I copy this and then I paste it in here. 00:03:49,200 --> 00:04:45,760 So you can now execute this command. So far nothing has happened: R hasn't done anything, so you can execute these commands. If you want to execute all commands that you have in your script file you can press this little source button. So far we only have one command that's only this one. If you want to execute a command at a time you put your cursor in here and then you click the 'Run' button -  and as you can see, the Run button. You can also press Control+Enter or for the source the equivalent is Control+Shift+S. So let's do that. Let's press 'Source'. Now very important: R will tell you if it didn't understand your command, okay, and it's very important. 00:04:45,760 --> 00:05:13,920 So whether you believe me or not I created a mistake on purpose and it will tell you what it didn't like: '\E' is an unrecognized escape in character string starting ""T:\E". Now curiously - and there are some curious things in R - R doesn't like backslashes when it comes to of the folder structure - it needs forward slashes. 00:05:13,920 --> 00:05:44,837 So let's see: if we change all these to forward slashes and we press source again now everything worked. You can see in the command window what R has done: it has taken the entire active R studio document - that's this one up here - and has executed it without error message. We are this 'larger than sign' - that's the command prompt, so that worked, that's fine.   00:05:44,837 --> 00:06:49,760 Now the last thing I want to show you in in this  short clip is how to create variables. So if you have commands which you know you want to save because they are sort of really properly part of your project, what you usually want  is: you would want to add these up here in the script file. But very often you will first  try a certain command. You can try that in the console and once you are convinced that it does what you want it to do then you'll put it into the script file script file and sort of save it for eternity. Let's create a variable, say, test1 equals to six. Now this will look extremely familiar if you've done  any sort of programming before. We are defining a variable called test1 and we assign a value of six to it and that equal sign in programming is what's called an assignment operator. So let me just press Enter and that fortunately happens. 00:06:49,760 --> 00:07:48,400 So we have this variable test1, it has a  value of six, so I could now for instance, say test1, and if I wanted to have another  value, let's say 8, I just type this command and press enter again and you see  that the the value for that variable changes. Now curiously while this works, this equal assignment  operator is not really how things are done in R and there are particular reasons for that. In fact that equal assignment operator will sometimes not work. How you should really do it is the following way. Test smaller than and a minus sign so this looks like an arrow  pointing to test1, and that's really what it is and we'll give it a new value 34. And you see that now the test1 variable has the value 34. 00:07:48,400 --> 00:09:19,360 Right so all these commands basically did the  same, just that we use different values. Once you have a variable here that has a certain  value, you can actually use that in further definition. So let's say test2 should be a new  variable and that should be test1 minus 56. So let me press Enter here. So we get test2 and test2 is -22. Why is that? Because test2 takes the value of 34 minus 56, which is negative 22. So far so good. This was just to give you a nice little introduction. Perhaps what I should  do - let's say you know that test1 and test2 - this is exactly what you wanted and  what you want to save you can put this now into your script file you just have to get rid  of these prompts and let's say you use different let's use different values so we know things have  changed and if you now want to execute all these three commands setting the working directory and  defining these two variables. What you do is you either click on the source button or you press  Control+Shift D and we can see that these commands have been executed. test1 now takes the value of  12 and test 2 is 12 minus 13 which is negative 1.