R tutorial 4: setting working directories

I thought I'd just list a few very useful things that can make life simpler in R.

One of the first things that R beginners have to get to grips with is setting the working directory. I've seen students struggle with this concept but it only takes a bit of practice for them to get comfortable with the idea. I've touched on this before but I'll recap here. There are two ways to set your working directory:

1) through the menu

In Windows: go to the File menu, select Change Working Directory, and select the appropriate folder/directory

In Macs: go to the Misc menu, select Change Working Directory, and select the appropriate folder/directory

2) by using the function

setwd("...")

in which, the "..." is the specific pathway, e.g.,

in Windows:

setwd("C:/Users/User Name/Documents/FOLDER")

in Macs:

setwd("/Users/User Name/Documents/FOLDER")

In this example, the working directory has been set to a folder FOLDER within the Documents directory. You have to type in, or copy and paste the appropriate pathway on your computer, of course.

You can check that your working directory has been correctly set by using the function:

getwd()

Typing this should return something like this:

[1] "/Users/User Name/Documents/FOLDER"

Normally, R's default working directory is

in Windows:

[1] "C:/Users/User Name/Documents"

in Macs:

[1] "/User/User Name"

which is also defined as "~/", so as long as you've got your desired target folder consistently under these directories, you can always set your new working directories like this:

setwd("~/FOLDER")

or

setwd("~/Documents/FOLDER")

where, again, the "~/" bit refers to the home directory.

Now let's move on to some simple, but useful, operations using the setwd() function. For instance, if you want to change your working directory to a subdirectory (a folder within the current working directory), then you can simply do this:

setwd(paste(getwd(),"/SUBFOLDER",sep=""))

This combines the functions, setwd(), getwd(), and paste(). The first two functions, we've already covered. The third function, paste() converts R objects into characters and concatenates them. For example:

V <- 1

paste(V)

which should return:

[1] "1"

The numeric object V has been converted into a character. Now if we try this:

paste(V, ".txt", sep="")

we should get this:

[1] "1.txt"

The numeric object V has been converted into a character and concatenated with ".txt", with no separation between the two characters (sep="").

We can apply the same logic to workspaces:

currwd <- getwd()

where currwd should be [1] "/Users/User Name/Documents/FOLDER" (or whatever the current working directory is). Now if we paste() this with "/SUBFOLDER":

paste(currwd, "/SUBFOLDER", sep="")

Then we should get:

[1] "Users/User Name/Documents/FOLDER/SUBFOLDER"

We get the same thing if we do:

paste(getwd(),"/SUBFOLDER",sep="")

And thus,

setwd(paste(getwd(),"/SUBFOLDER",sep=""))

is the same thing as

setwd("/Users/User Name/Documents/FOLDER/SUBFOLDER")

Conversely, if you want to move back up to the parent directory (e.g., FOLDER) then type:

setwd("../")

Checking with getwd() should return:

[1] "Users/User Name/Documents/FOLDER"

or equivalent.