Get & Set Working Directory in R (3 Examples) | getwd & setwd Functions

 

This tutorial explains how to identify and change working directories using the getwd and setwd functions in the R programming language.

Table of contents:

So now the part you have been waiting for – the examples.

Definitions & Basic R Syntaxes of getwd and setwd Functions

 

Definitions: In the following, you can find the definitions of the getwd and setwd functions.

  • The getwd R function returns the filepath of the current working directory.
  • The setwd R function specifies a new working directory.

 

Basic R Syntaxes: You can find the basic R programming syntaxes of the getwd and setwd functions below.

getwd()                                          # Basic R syntax of getwd function
setwd("Your_Path")                               # Basic R syntax of setwd function

 

I’ll explain in the following three examples how to use the getwd and setwd functions in R programming.

Example 1: Get Working Directory Using getwd() Function

In this Example, I’ll illustrate how to return the currently used working directory in R. For this, we can run the getwd command as shown below:

getwd()                                          # Apply getwd function
# "C:/Users/Joach/Documents"

As you can see, the getwd function returns a character string containing the filepath of our current working folder to the RStudio console.

 

Example 2: Set Working Directory Using setwd() Function

The following R syntax explains how to change the working directory using the setwd function in R. Have a look at the following R code:

setwd("C:/Users/Joach/Desktop/my_folder")        # Apply setwd function

We had to specify a character string containing the filepath we want to use within the setwd function. Note that you have to use a slash (not a backslash) to specify your path when you are working on a windows computer.

We can check if the switch to a different working directory worked well by using the getwd function again:

getwd()                                          # Checking new working directory
# "C:/Users/Joach/Desktop/my_folder"

Looks good, the working directory was changed.

 

Example 3: Store Path of Working Directory as Character String in Data Object

In the R programming language it is not necessary to set a working directory explicitly. We can also store the filepath of the working directory we want to use in a data object and use this data object whenever we need it:

my_path <- "C:/Users/Joach/Desktop/my_folder"    # Save path

The previous R code stored our path in the data object my_path. For instance, we can use this data object to write a csv file to our folder:

write.csv2(1:5,                                  # Write csv file to path
          paste0(my_path, "/some_data.csv"))

Have a look at the folder you have specified. It should contain a new csv file called some_data:

 

folder on computer

 

Video, Further Resources & Summary

Do you need more explanations on the R programming codes of this page? Then you may want to watch the following video that I have published on my YouTube channel. In the video, I show the examples of this tutorial.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

In addition, you may want to read some of the other posts of my homepage:

 

In summary: In this R tutorial you learned how to apply the getwd and setwd functions. If you have additional questions, don’t hesitate to let me know in the comments below. Furthermore, don’t forget to subscribe to my email newsletter in order to get updates on the newest articles.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


4 Comments. Leave new

  • Hello Joachim,
    Thanks for this elaborate course. However, I have been trying to import data from Documents Folder to the RStudio. I have used the path:

    > getwd()
    [1] “C:/Users/Obed Fung/Documents”
    > write.csv(data_PCA_Fertility,
    + “data_PCA_Fertility.csv”,
    + row.names = FALSE)#
    The output from is: Error in is.data.frame(x) : object ‘data_PCA_Fertility’ not found

    I have also tried to use this path which can permit me work in FactoMineR, after loading FactoMineR of course:
    don = read.table(“C:/Users/Obed Fung/Documents/data_PCA_Fertility.csv”, header=TRUE, sep=”;”, check.names=FALSE, row.names=1,stringsAsFactors = TRUE)#
    The output I have had hundreds of times, which has prompted me to take this introductory course in R is:

    Error in file(file, “rt”) : cannot open the connection
    In addition: Warning message:
    In file(file, “rt”) :
    cannot open file ‘C:/Users/Obed Fung/Documents/data_PCA_Fertility.csv’: No such file or directory

    Please what seems to be the problem? This has not permitted me to continue with my learning of R Programming and FactoMineR. Your help will be invaluable

    Reply
    • Hey,

      Thank you for the kind comment!

      The “Error in is.data.frame(x) : object ‘data_PCA_Fertility’ not found” indicates that the data object data_PCA_Fertility doesn’t exist in your workspace at the point when you run write.csv. Could you check if this data object really exists?

      Regards,
      Joachim

      Reply
  • Hello,
    Thanks for the course Joachim. This is a path I used and the output for data saved under the filename ‘data_PCA_Fertility’ on Documents folder:

    data_1 <- read.csv("data_PCA_Fertility")#
    Error in file(file, "rt") : cannot open the connection
    In addition: Warning message:
    In file(file, "rt") :
    cannot open file 'data_PCA_Fertility': No such file or directory

    I wanted to have it imported to R.
    Thanks

    Reply
    • Hi again,

      I just saw your second question.

      This indicates that you have either not specified the path correctly, or the file does not exist in your working directory.

      In your specific case, it seems like your file name is missing “.csv” (i.e. read.csv(“data_PCA_Fertility.csv”)).

      Regards, Joachim

      Reply

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top