Getting started with R and Rstudio

Congratulations on starting your R programming journey! The first step is to download R and Rstudio. Well, actually Rstudio is not strictly necessary, but it does make life easier. You can start by going to r-project.org to download the R program. Alternatively, you can simply google “r program” if you do not want to follow the link provided (which I understand). Once you navigate to the website, you will want to click “CRAN” found under the “Download” header on the left side. Then you will be directed to pick a “mirror”, which just describes the places the download is stored. You will want to pick a server close to you for the best download speed, but any mirror should work. Now click the link corresponding to your operating system. Note that you may find a more up-to-date version of R by using the terminal in Linux, and there are many useful online tutorials for how to do that depending on your flavor of Linux. Finally, click the “base” button that will give you the R installation file.

Now that you have the R program installed, you can optionally choose to install Rstudio. I recommend it but that is up to you. To do this you can go to posit.co (the company recently renamed itself), or like before you can google “Rstudio”. Go to the “products” tab and select “Rstudio IDE”. Now you can navigate to downloading the free version of Rstudio for whatever type of operating system you are using. The Rstudio interface can be intimidating at first, but it is packed with useful features you will want. Once you open Rstudio, you may need to navigate to “File”, hover over “New File” and select “R script”. This will open a new R script for you to edit. Let’s go through some of the key elements of the default Rstudio layout, which by the way is changeable in “Global Options”. The new page that appeared in the top left is the R script, the code that you type here will be savable and editable, to run the code you can highlight it and click “Run”. In the bottom left pane, you will see the console, which shows the code execute and output. In the console, you can also type and run R code, but you will not be able to save the code you ran. The only time I run code in the console is when I install a new package using install.packages(), because I likely will not need to run that particular code more than once. In the top right pane, you will find your “environment” which stores the variables you have created and the datasets you have imported. This is a huge benefit of using Rstudio, you can much more easily evaluate the dimensions of variables, determine which variables you have, and view datasets. Finally, in the bottom right pane is “files”; this shows the files R can “see” (the files in your working directory). For loading/importing and saving data, it is very important to know what these files are. A very common problem when attempting to load files is that the file you think is in your working directory is actually not, so you will want to check this first. At this point, you should have the very basics of the interface to start writing code, which we will start in the next blog!

Previous
Previous

Creating Variables in R