Working Directory
& Paths in R
Learn how R finds your files using getwd(), setwd(), and the difference between absolute and relative paths.
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?
Visualizing the File System
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.
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.
setwd("/Users/student/r_projects")
# Verify the change
getwd()
[1] "/Users/student/r_projects"
Absolute vs Relative Paths
Example:
/Users/student/r_projects/data.csv
Example:
data.csv
Reading Path Notation
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
Example 1: Simple File Read
Suppose your working directory is /Users/student/r_projects and the file data.csv is inside that same folder.
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.
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.
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.
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
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?