Welcome to this tutorial on Working directories in RStudio.
In this tutorial, you will learn about:
Working directories in Rstudio
How to read and store csv files in R
To understand this tutorial, you should know:
R and RStudio
Basic commands of R
If not, please locate the relevant tutorials on R on this website.
This tutorial is recorded on
Ubuntu Linux OS 16.04
R version 3.2.3
RStudio version 1.1.456
Install R version 3.2.0 or higher.
We will use the captaincy data set which we created earlier in this series.
Download this data set CaptaincyData.csv from the Code files link of this tutorial.
I have downloaded and moved this data set to the folder myProject on my Desktop.
I shall set this folder as my Working Directory.
Remember we had created two scripts, myfirstscript.R and mysecondscript.R
Both were saved in the folder myProject, on the Desktop.
R always points to a default directory on our computer
This directory is where R will look, by default, for files you ask it to load.
We will find out what our current Working Directory is.
Let us switch to RStudio.
In the Console window, type getwd parentheses and press Enter.
The current Working Directory is shown in the Console.
Note down the directory path.
Now, we will learn how to set our Working Directory.
I will set the folder myProject on my Desktop as my Working Directory.
I am maximizing the Files and Plots window of RStudio.
At the top of the Files and Plots window, click on the Files tab.
Go to the upper right corner of the same window.
Below the refresh icon, click on the button with three dots.
Go To Folder window appears.
Select the folder which you want to make as your Working Directory and click Open.
I am selecting myProject located on my Desktop.
Under Files tab, you can see that the two scripts created earlier, have been loaded.
Now, below the Files tab, click on the More button with the blue gear symbol.
From the dropdown menu, select the Set As Working Directory option.
Now this folder is set as your Working Directory.
You may verify this by looking at the Console.
Now, I am minimizing the Files and Plots window.
There is one other way to set the Working Directory also.
In the Console, type setwd.
Now, inside parentheses, type the desired folder path within double quotes.
Press Enter.
Now, we will learn how to read and store a csv file called CaptaincyData.csv.
We shall declare a variable captaincy to store CaptaincyData.csv.
In the Console window, type captaincy space equal sign space read.csv
Now, CaptaincyData.csv inside double quotes in parentheses and press Enter.
read.csv function is used to read a file and to create a data-frame from it.
Now, we use View function to see the stored data frame.
In the Console window, type View captaincy in parentheses and press Enter.
The contents of captaincy data frame appears in the Source window.
I am maximizing the Source window to see the captaincy data set.
This brings us to the end of this tutorial. Let us summarize.
In this tutorial, we have learned
How to get and set a working directory
How to read and store a csv file
We now suggest an assignment.
Create a new folder on your computer and make it your Working Directory.
The video at the following link summarises the Spoken Tutorial project.
Please download and watch it.
We conduct workshops using Spoken Tutorials and give certificates.
Please contact us.
Pls post your timed queries in this forum.
Pls post your general queries in this forum.
The FOSSEE team coordinates coding of solved examples of popular books.
We give honorarium and certificates to those who do this.
For more details, please visit these sites.
The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India
This script for this tutorial was contributed by Shaik Sameer (FOSSEE Fellow 2018).
This is Sudhakar Kumar from IIT Bombay signing off. Thanks for watching.