1

Quick one - I am a beginner in R Studio so trust asking a silly question .

What is the difference between the two code of setwd as not sure ? or is their any difference at all ?

> setwd("C:/Users/RAMIT PAUL/Downloads/Rate Cat")
> setwd("C:\\Users\\RAMIT PAUL\\Downloads\\Rate Cat")
3
  • Yes the latter will fail. You need to use the first or use double backslashes
    – Hugh
    Commented Nov 22, 2017 at 8:15
  • @Firebug Where did OP make that clear? Commented May 10, 2022 at 16:25
  • 1
    @Firebug If you look at the markdown of the first revision the double backslashes were there already then, but the markdown interpreted them as an escaped single backslashes. Besides the title explicitly said "Setwd in "R" with \\". So to me it looks like they mean double backslashes. Commented May 10, 2022 at 20:04

1 Answer 1

0

Any number of forward slashes will work (this is an OS thing). So the both the following will work:

setwd("C:/Users/RAMIT PAUL/Downloads/Rate Cat")
setwd("C://////////Users///////////RAMIT PAUL///////////Downloads///////////Rate Cat")

Now, backslashes are imbued with special meaning. They are used for escape sequences. So you have to escape the slash itself so it's correctly parsed. Hence, this won't work:

setwd("C:\Users\RAMIT PAUL\Downloads\Rate Cat")

While these work:

setwd("C:\\Users\\RAMIT PAUL\\Downloads\\Rate Cat")
setwd("C:\\\\\\\\\\\\\\\\\\\\\\Users\\RAMIT PAUL\\Downloads\\Rate Cat")

In R 4.0.0 you can actually use (thanks Mossa):

setwd(r"(C:\Users\RAMIT PAUL\Downloads\Rate Cat)")
3
  • @user817995 You're welcome :). Please consider accepting my answer (clicking the green check mark below the score arrows) if it thoroughly answered all your inquiries.
    – Firebug
    Commented Feb 19, 2018 at 12:56
  • 1
    Also add the setwd(r"(C:\Users\RAMIT PAUL\Downloads\Rate Cat)") that was introduced in R 4.0. See this
    – Mossa
    Commented May 10, 2022 at 12:02
  • 1
    Cool, thanks for the tip @Mossa! I haven't kept up with new R changes, that is nice to have
    – Firebug
    Commented May 10, 2022 at 12:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .