How can I change the working directory in R using the setwd and getwd functions?

How can I change the working directory in R using the setwd and getwd functions?

The setwd and getwd functions in R are used to change and retrieve the current working directory, respectively. The working directory is the location on the computer where R will read and write files. To change the working directory, the setwd function is used, followed by the path to the desired location. This will set the new directory as the working directory. To retrieve the current working directory, the getwd function is used. These functions are helpful when working with different files and directories in R, allowing for easy navigation and organization of data.

Use setwd / getwd in R (With Examples)


Whenever you use R, your environment is always pointed to some working directory.

You can use the following functions in R to get the working directory and set the working directory:

  • getwd() – Get the current working directory
  • setwd(‘Path/To/Some/Directory’) – Set current working directory

The following examples show how to use these functions in practice.

Example 1: Get Working Directory

We can use the getwd() function to display the current working directory in R:

#display current working directory
getwd()

[1] "C:/Users/Bob/Desktop"

Example 2: Set Working Directory

We can then use the setwd() function to set the working directory to some new location:

#set working directory
setwd('C:/Users/Bob/Documents')

We can then verify that the working directory has changed by using the getwd() function again to get the current working directory:

#display current working directory
getwd()

"C:/Users/Bob/Documents"

Example 3: View Files in Working Directory

Once we’ve set the working directory, we can use the list.files() function to view the file names within the directory:

#view number of files in working directory
length(list.files())

[1] 147

#view first five file names in working directory
head(list.files())

"output.yml"  "analysis3.R"  "analysis3-1.R"  "testdoc.R"  "final_model2.Rmd" 

We can also use the %in% operator to check if a specific file is located in our current working directory:

#check if file 'analysis3.R' exists in working directory
'analysis3.R' %in% list.files()
[1] TRUE

The following tutorials explain how to perform other common functions in R:

Cite this article

stats writer (2024). How can I change the working directory in R using the setwd and getwd functions?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-change-the-working-directory-in-r-using-the-setwd-and-getwd-functions/

stats writer. "How can I change the working directory in R using the setwd and getwd functions?." PSYCHOLOGICAL SCALES, 5 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-change-the-working-directory-in-r-using-the-setwd-and-getwd-functions/.

stats writer. "How can I change the working directory in R using the setwd and getwd functions?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-change-the-working-directory-in-r-using-the-setwd-and-getwd-functions/.

stats writer (2024) 'How can I change the working directory in R using the setwd and getwd functions?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-change-the-working-directory-in-r-using-the-setwd-and-getwd-functions/.

[1] stats writer, "How can I change the working directory in R using the setwd and getwd functions?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I change the working directory in R using the setwd and getwd functions?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top