setwd() Function in R

The working directory is the folder or path where you are currently working. For example, if you are in the Program Files folder, you would see C:Program Files. Every process has a current directory. Similarly, R is pointed at a specific directory on your computer by default. This guide will explain how you can change the current working directory using the setwd() function in R.

Find current working directory

To see the current working directory run the following code:

getwd()

Output

"C:/Users/gaura/Documents"

Change working directory

Use the setwd() function to set a working directory.

Syntax

setwd("file path")

Example

In this example, I am changing my current working directory to a different path.

setwd("E:Reports")

To verify if the working directory has changed, use the getwd() function again to get the current working directory.

getwd()

Output

"E:/Reports"

Leave a Comment