When working with multiple datasets belonging to different projects, it is best to keep things organised. By this, you have to understand that it is best to save your data files, processed datasets and the content of the workspace in R (script, etc) in a specific folder, the working directory. That specific folder, once saved, can be reloaded at any time. You can thus work with a project, save your workspace, load a different workspace, keep working with an older/newer dataset, etc, without the risk to mix or lose anything.
Here is how to set a working directory. First, if you are curious, you may check what R is pointing by default. Use to getwd()
to get the default directory:
[code language=”r”]
getwd()
[/code]
Then, set the new working directory with the command setwd()
and the path to the directory indicated between the parentheses and quotation marks:
[code language=”r”]
getwd()
setwd(‘d:/biostats/R/examples/testdirectory’)
getwd()
[/code]
Make sure that the folder you are pointing at already exists! setwd()
does not create a folder, it sets up an existing one as target. If the folder does not exists, an error message will show up in the console. You may use getwd()
to confirm that your folder is correctly set up.