Skip to main content
Research Guides


A brief guide to the open source statistical software, R.

Getting Started in R: First Steps

R Commands

R is a command-driven program based on the programming languages C and S, which makes it very flexible. You can either use the interactive command interface or write scripts. R is based on the concept of functions. Functions are a fundamental building block of R--in the simplest sense, functions take an input, do something to it, and then give an output.

Functions are always followed by parenthesis (), and your arguments go inside the parenthesis. The arguments are the specific instructions telling R what to do, including the input. For example, running hist(mydata) will create a histogram of the object "mydata". The function hist() tells R that you want to make a histogram, and the argument mydata inside the parenthesis tells R what to make a histogram of. You can add further instructions to a command, such as specifying a subset of the data, changing the appearance of the histogram, and much more.

Click here for an overview of functions in R.

Set Your Working Directory

R reads and saves data from the working directory (i.e., folder). The default working directory is usually the Documents folder, and you'll usually want to change it to where you keep your project data.

To find out the current working directory, type:  getwd()

To set the working directory, type:  setwd("pathname") where pathname is the folder on your computer or you can use the menus or navigate the directories in RStudio

On a Mac, this would look like setwd("/Users/username/Documents/myproject")
On a PC, this would look like setwd("C:/Users/username/Documents/myproject")
                                       or setwd("C:\\Users\\username\\Documents\\myproject")
Note: On PCs, you should use forward slashes or double backslashes for the path rather than backslashes (the Windows default). This is because backslashes are used in other ways in R.

Once you set the working directory, you can refer to a file in the working directory using just its name.
Rather than having to type mydata <- importcsv(“C:/Users/username/Documents/myproject/nlsy.csv”)
you can just type                mydata <- importcsv(“nlsy.csv”)

R Workspace

This is the working environment inside of R, which includes any objects (matrices, vectors, lists, dataframes, arrays) that you have defined during your session. In RStudio, this is the Environemnt tab.

When you exit R, it will ask you if you want to save your workspace image--we highly recommend that you choose "no", or R will begin to run more slowly over time as the workspace fills up. Instead, use R scripts to regenerate objects when you begin a new session. If your work session created objects that are large or time-consuming to recreate, you can save the workspace as its own file or save the objects using the save function.

For more details about the R workspace and working directory:

Data Structures in R

The term 'data structures' refers to how R stores and retrieves data. R has a more flexible set of data structures, unlike traditional statistical software. These data structures include vectors, matrices, data frames, and lists.

For a very clear explanation of data structures in R, check out this guide (go to section Basic data structures).