2.5 Setting a directory

Every data project needs a home. Your working directory is the folder on your computer from which R will load data, images, etc and to which it will save your output. It’s important to designate your working directory at the beginning of any script using the setwd('filepath') command. For example:

  setwd('C:\\Users\\ahart\\Documents\\SIS600')

This is where you should save the data sets for your project. Note the use of the double backslash, \\, when specifying the directory. Note also that this path only works on MY computer! You’ll have to specify your own path. Don’t know your path? No problem. Navigate to your project folder on your computer. Similar to the left-hand panel below, you may have the option to copy the path from a button. Alternatively, right-click on your data set, and use options like “Properties” or “Copy path as text” to get the path.

Finding your directory's path

FIGURE 2.2: Finding your directory’s path

In either case, you can copy the path and paste into setwd() in your R session. Don’t forget to mind your quotes and slashes!

If you’re unsure about the current directory, check using the getwd() function:

  getwd()
## [1] "C:/Users/ahart/Documents/SIS600"

You can verify that the correct files are there using list.files():

  list.files()
## [1] "biopics.xls"        "DCPS testing.RData"

Note that my list includes the two companion data sets to this guide. If you don’t see them after you run list.files(), confirm that you downloaded the data to your computer and move them to your project folder.