Getting Started with R and RStudio

You have R and RStudio installed and keen to get started with your first exercise. But before you do so, I would like to suggest some routine steps that will make your future work with data easier and more organized. If you are already experienced with RStudio and have your own routine, feel free to skip this section.

1. Creating work folder and project

First, let’s create a directory (and then a project) for keeping all work associated with this particular analysis. This is a very convenient way to work, because in R/RStudio, you are working in a specific directory (“project” directory). Unless all data and other files that you need are in the same directory as your project, you will have to specify full paths such as "C:/Users/annabrown/Documents/R Exercise book/Exercise 1/data.txt". This can get tedious very quickly. But within a project, you can just refer to file names, such as "data.txt". But more importantly, when all the project-related files and objects are kept together, it is easy to get back to your work at any time by simply opening the project (using RStudio menu File / Open Project…).

Begin by creating a new directory called “R Exercise 1” on your computer, and download/move the data file SDQ.RData into this directory.

To create an R project associated with this folder, in RStudio click File and then New Project. You will have a box popping up asking you to select where to create a new project. Select the folder you have just created (in my example, it is “R Exercise 1”). You should see your project name appearing in the Files tab of the bottom/right RStudio window, and the file SDQ.RData should be visible there too.

2. Creating a Script

You can type commands directly into R Console and execute them one at a time. This can be good for trying out some functions. However, in the long run, you will need to save your analysis and modify or add to it, sometimes over several sessions. So it is much better to create a Script (a text file with your R code), from which any number of commands can be sent to Console at any time.

To create a new script, select File / New File / R Script. It will open up in its own window. Write all code suggested in a particular exercise on this script. Run any command in the script by moving your cursor to that command and pressing Ctrl + Enter, and you will see how the command gets passed and executed in Console.

3. Installing and loading packages

R contributors have created thousands of functions to perform all sorts of analyses and packaged them into “packages”. Whenever we need to use function X from a particular package, we should first install the package containing X on the computer. (Note that many basic functions are part of base R and you do not need to install or load anything to use them). We will need package psych for Exercise 1. To install it, click menu Tools, and then Install Packages and type psych. You need to install packages once, but should load them in every session that you use them (unless they are already loaded). To load package X, use function library(X), like so:

library(psych)

4. Saving your work

It is a good idea to regularly save your R script by pressing the Save icon, or choosing menu File / Save. Give the script a meaningful name, for example “Likert scaling”.

After you finished working with an exercise, save your R script. Also you may want to save your entire workspace, which includes the data frame with added columns and also all the new objects you created. You might need these again when revisiting the exercise, or in other exercises using the same data set. When closing the project, File / Close project, select Save to save your Workspace image (it will be saved with extension .RData). The project will close, your script and workspace will be saved, and R session will be terminated (Console cleared). Close RStudio.