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('/Users/cadavis/Documents/GitHub/SurvivalGuide600/docs')

This is where you should save the data sets for your project.

Two types of errors are common when setting a working directory:

  • Using the wrong separator for file paths. By default, R uses a forward slash (/) to separate directories in file paths. This is consistent with some operating systems, but Windows uses a backslash (\) by default. To enter a Windows file path into R, replace all the single backslashes with either forward slashes or, as above, double backslashes, \\.
  • Indicating a file path that does not exist [on your computer]. The file path above is specific to my computer! You’ll have to specify your own path. Don’t know your path? 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, say, 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

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

  getwd()
## [1] "/Users/cadavis/Documents/GitHub/SurvivalGuide600"

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

  list.files()
##  [1] "_bookdown_files"       "_bookdown.yml"         "_build.sh"            
##  [4] "_deploy.sh"            "_output.yml"           "_render.R"            
##  [7] "00-UsingGuide.Rmd"     "01-GettingStarted.Rmd" "02-BasicR.Rmd"        
## [10] "03-WorkingData.Rmd"    "04-Descriptives.Rmd"   "05-Tabulation.Rmd"    
## [13] "06-Regression.Rmd"     "07-DataMunge.Rmd"      "08-Visualization.Rmd" 
## [16] "apps"                  "biopics.xls"           "book.bib"             
## [19] "css"                   "dc_weather_2018.csv"   "DCPS testing.RData"   
## [22] "dcps.csv"              "dcps.rdata"            "deploy_github"        
## [25] "docs"                  "film.csv"              "film.rdata"           
## [28] "gphexport.jpg"         "index.Rmd"             "krantz.cls"           
## [31] "LICENSE"               "Makefile"              "now.json"             
## [34] "packages.bib"          "preamble.tex"          "README.md"            
## [37] "rstudiowindow.jpg"     "sampledata.rdata"      "settingwd.jpg"        
## [40] "spssData.sav"          "stataData.dta"         "stats2.pdf"           
## [43] "SurvivR_files"         "SurvivR.log"           "SurvivR.Rmd"          
## [46] "SurvivR.tex"           "toc.css"

Note that my list includes several 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.