01 / 21

Working Directory
& Paths in R

Learn how R finds your files using getwd(), setwd(), and the difference between absolute and relative paths.

R Programming

What is a Working Directory?

Every time R runs, it has a home base on your computer: a specific folder it looks in first when you try to read or write files. This folder is called the working directory.

If you ask R to open data.csv without specifying a full path, R will search for that file inside the working directory. If the file is somewhere else, R will not find it.

Why Does the Working Directory Matter?

✓ Reading Data
Functions like read.csv() look for files relative to the working directory by default.
✓ Saving Output
When you export plots or tables, R saves them into the working directory unless you specify otherwise.
✓ Reproducibility
Knowing your working directory ensures scripts run consistently on any machine.

Visualizing the File System

/ (root) Users etc var student Documents r_projects data.csv ← Working Dir

Checking Your Location: getwd()

The function getwd() stands for "get working directory." It returns the full path to the folder R is currently using as its home base.

# Ask R: "Where am I right now?"
getwd()

# Output (example on Mac/Linux):
[1] "/Users/student/r_projects"

# Output (example on Windows):
[1] "C:/Users/student/r_projects"

Changing Location: setwd()

The function setwd() stands for "set working directory." It moves R's home base to a different folder you specify.

# Move R to a specific folder
setwd("/Users/student/r_projects")

# Verify the change
getwd()
[1] "/Users/student/r_projects"

Absolute vs Relative Paths

Absolute Path
The full address from the root of your file system all the way to the target. Works regardless of where R is currently located.

Example:
/Users/student/r_projects/data.csv
Relative Path
A shortcut that starts from the current working directory. Shorter and more portable, but depends on where R is.

Example:
data.csv

Reading Path Notation

ABSOLUTE PATH /Users/student/r_projects/data.csv Root Folder Folder Working Dir File RELATIVE PATH (from r_projects) data.csv GO UP ONE LEVEL, THEN INTO "Documents" ../Documents/report.pdf .. = parent folder . = current folder

Windows vs Mac/Linux Paths

Windows Mac / Linux
Separator \ (backslash) / (forward slash)
Root C:\ /
Example C:\Users\student /Users/student
In R code C:/Users/student
or C:\\Users\\student
/Users/student

Step-by-Step: Loading a File

1
Check your current location by running getwd() in the console.
2
Identify where your file lives. Open your file explorer and note the folder containing your data file.
3
Match them up. If they differ, use setwd() to move R to the correct folder, or use an absolute/relative path in your read function.
4
Verify with getwd() again, then call read.csv("data.csv") using just the file name.

Example 1: Simple File Read

Suppose your working directory is /Users/student/r_projects and the file data.csv is inside that same folder.

# Step 1: Confirm location
getwd()
[1] "/Users/student/r_projects"

# Step 2: Read the file (relative path)
my_data <- read.csv("data.csv")

# This works because data.csv is
# in the working directory!

Example 2: File in a Subfolder

Now imagine data.csv is inside a subfolder called raw_data, one level below the working directory.

r_projects (WD) raw_data data.csv
# Use a relative path with the subfolder
my_data <- read.csv("raw_data/data.csv")

# OR use an absolute path
my_data <- read.csv("/Users/student/r_projects/raw_data/data.csv")

Example 3: File Outside the Working Directory

What if your file is in a completely different branch of the file tree? You need to navigate up with .. or use an absolute path.

student r_projects (WD) Documents report.csv ../Documents/
# Go UP one level (..), then into Documents
report <- read.csv("../Documents/report.csv")

# OR use the full absolute path
report <- read.csv("/Users/student/Documents/report.csv")

Building Paths with file.path()

Manually typing slashes can be error-prone. R provides file.path() to safely construct paths with the correct separator for any operating system.

# Build a path piece by piece
my_path <- file.path("raw_data", "data.csv")
print(my_path)
[1] "raw_data/data.csv"

# Use it in read.csv
my_data <- read.csv(my_path)

Common Mistakes

✕ Single Backslash on Windows
Writing C:\Users\data in R code. The \ is an escape character. Use / or \\ instead.
✕ Forgetting Quotes
Calling setwd(my_folder) without quotes. R will look for a variable named my_folder, not the path string.
✕ Assuming the Wrong Directory
Running a script without checking getwd() first. R may be pointing to the home folder, not your project.

Quick Reference Summary

Function / Concept What It Does Example
getwd() Returns current working directory "/Users/student"
setwd() Changes working directory setwd("/Users/student")
Absolute path Full path from root /Users/student/data.csv
Relative path Path from working directory data.csv
.. Go up one folder level ../Documents/file.txt
file.path() Build cross-platform paths file.path("data", "f.csv")

Question 1 of 5

What does the R function getwd() return?

Question 2 of 5

Your working directory is /Users/alex/projects. Which command reads a file called scores.csv located in the same folder?

Question 3 of 5

On a Windows machine, which of the following is a valid path string inside R code?

Question 4 of 5

In a file path, what does ".." (two dots) represent?

Question 5 of 5

Which statement best describes the difference between an absolute path and a relative path?