2 Setting Up
2.1 R and R Studio
To begin using R, you must first download it here: https://www.r-project.org/
I also recommend using R Studio, which keeps all of your R-related materials easily accessible and visible in one screen. You can download it here: https://rstudio.com/
Once both are downloaded, open R Studio. You will have 4 quadrants in your R Studio workspace.
2.2 R Scripts
If you’ve worked with other statistical software or done any coding, you know the importance of having a reproducible document that contains all of your code, along with annotations. In R, this is called an R script.
When you open R Studio for the first time, you will need to start a new R script. Go to File > New File > R Script. R scripts will appear, by default, in the top left quadrant of R Studio. You can have multiple R scripts open at one time, all running on the same R session.
Note: You can also use R Markdown. I personally prefer using R scripts, especially if you are new to R. I also highly recommend taking advantage of multiple R scripts (e.g. separate scripts for data cleaning and analysis, etc).
Using an R script ensures that you remember what you did during analysis and that your analysis is replicable.
R scripts are color-coded. You can write notes using a “#”. Like an academic paper, R scripts should have a heading that includes the title of your project, your name, the date you last updated the R script, and a short description of what the R script does.
2.3 R Console
R scripts represent the “input” for R. The “output” is returned in the console, which is the bottom left quadrant of R studio.
To run commands from an R script in the console, highlight the code and press Cmd+Return (on a Mac) or Ctrl+Enter (on a PC).
Note: You can type code directly into the console, but it’s not reproducible. It’s best practice to type all of your code in a script.
2.4 Working Directories
For any project class, assignment, etc. for which you’re using R, you should have a designated working directory. A working directory is a folder on your computer that contains everything you need for analysis, including R script(s) and data. Your R Script should live in your working directory (make sure you save it there! File > Save).
You will need to set your working directory within your R Script using the working directory’s file path. This is so that R can locate the files you’re using in your analysis.
Note: To get the file path for a folder on a Mac, right-click on the folder, press Option, and click on “Copy ‘Folder’ as Pathname.” To get the file path on a PC, click on the “Home” tab and then on the icon that says “copy path.”
If you are unsure of whether you have the correct working directory, you can always check your working directory using:
2.5 A brief note on functions in R
The basic format for functions on R is this:
You will see the output in your R console. You can save the output as an R object like this:
2.6 Installing packages
One of the best things about R is the variety of packages that are available to use. Packages are extensions of R’s basic software.
The first time you use an R package, you have to install it. You only need to install each package once. (Note the quotation marks!)
After the package is installed, you will have to tell R you want to use the package during this session by adding it to your library. (Note the lack of quotation marks!).
Usually, at the top of every R script, the first code I write is a list of the packages I use in that script.
Note: This tutorial will use a mix of base R and tidyverse approaches. For more information about tidyverse see https://www.tidyverse.org/.
2.7 Documentation
You can access documentation using a “?” followed by the function name. Help pages will open in the bottom right quadrant of R Studio.
2.8 Activity
Open a new R script. Save it to a folder that will be your working directory.
Create a heading. Link to your working directory within the R script.
Install and load the tidyverse package.