Set Working Directory in R

How to Set Working Directory in R

If you want to read files from a specific location or write files to a specific location you will need to set working directory in R. The following example shows how to set the working directory in R to the folder “Data” within the folder “Documents and Settings” on the C drive.

# Set the working directory
setwd("C:/Documents and Settings/Data")

Remember that you must use the forward slash / or double backslash \\ in R! The Windows format of single backslash will not work.

Here’s the official R-manual page on setting the working directory: http://stat.ethz.ch/R-manual/R-patched/library/base/html/getwd.html.

Thanks for reading! This website took a great deal of time to create. If it was helpful to you, please show it by sharing with friends, liking, or tweeting! If you have any thoughts regarding this R code please post in the comments.

JM

3 thoughts on “Set Working Directory in R

  1. Gautam Saripalli

    When I try to set the working directory in excel using the command setwd(“C:\\myGAPIT”), i get an error “Cannot change working directory”. Please suggest what should I do?
    Regards,
    Gautam.

    Reply
    1. Justin@RProgramming.net Post author

      Your code looks right. Are you sure that the folder “myGAPIT” exists in C:?

      Reply
  2. Gautam Saripalli

    When I try to set the working directory in R using the command setwd(“C:\\myGAPIT”), i get an error “Cannot change working directory”. Please suggest what should I do?
    Regards,
    Gautam.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *