Difference between revisions of "R/C2/Working-directories-in-RStudio/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "<div style="margin-left:0cm;margin-right:0cm;">'''Title of script''': Working directories in RStudio </div> <div style="margin-left:0cm;margin-right:0cm;">'''Authors''': Shai...")
 
Line 19: Line 19:
  
 
Objectives
 
Objectives
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | In this tutorial,''' '''you will learn about:* <div style="margin-left:1.947cm;margin-right:0cm;">'''Working directories '''in '''RStudio'''</div>
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | In this tutorial,''' '''you will learn about:
 +
* <div style="margin-left:1.947cm;margin-right:0cm;">'''Working directories '''in '''RStudio'''</div>
 
* <div style="margin-left:1.947cm;margin-right:0cm;">How to read and store '''csv '''files in '''R'''</div>
 
* <div style="margin-left:1.947cm;margin-right:0cm;">How to read and store '''csv '''files in '''R'''</div>
 
  
 
|-
 
|-
Line 28: Line 28:
 
Pre-requisites
 
Pre-requisites
  
 
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | To understand this tutorial, you should know:  
 
+
* <div style="margin-left:1.947cm;margin-right:0cm;">'''R''' and '''RStudio'''</div>
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | To understand this tutorial, you should know: * <div style="margin-left:1.947cm;margin-right:0cm;">'''R''' and '''RStudio'''</div>
+
 
* <div style="margin-left:1.947cm;margin-right:0cm;">Basic commands of '''R''' </div>
 
* <div style="margin-left:1.947cm;margin-right:0cm;">Basic commands of '''R''' </div>
 
 
  
 
If not, please locate the relevant tutorials on '''R''' on this website.
 
If not, please locate the relevant tutorials on '''R''' on this website.
Line 41: Line 38:
 
Systems requirements
 
Systems requirements
  
 
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | This tutorial is recorded on
 
+
* <div style="margin-left:1.947cm;margin-right:0cm;">'''Ubuntu Linux OS 16.04''' </div>
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | This tutorial is recorded on* <div style="margin-left:1.947cm;margin-right:0cm;">'''Ubuntu Linux OS 16.04''' </div>
+
 
* <div style="margin-left:1.947cm;margin-right:0cm;">'''R '''version''' 3.2.3 '''</div>
 
* <div style="margin-left:1.947cm;margin-right:0cm;">'''R '''version''' 3.2.3 '''</div>
 
* <div style="margin-left:1.947cm;margin-right:0cm;">'''RStudio''' version''' 1.1.463'''</div>
 
* <div style="margin-left:1.947cm;margin-right:0cm;">'''RStudio''' version''' 1.1.463'''</div>
 
 
  
 
Install '''R''' version '''3.2.0''' or higher.
 
Install '''R''' version '''3.2.0''' or higher.
Line 55: Line 49:
 
Download '''CaptaincyData.csv''' file from the '''Code files '''link
 
Download '''CaptaincyData.csv''' file from the '''Code files '''link
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | We will use the '''captaincy data set '''which we created earlier in this series.
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | 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.  
 
Download this data set '''CaptaincyData.csv''' from the '''Code files''' link of this tutorial.  
Line 63: Line 56:
 
Highlight '''CaptaincyData.csv''' in the folder '''myProject'''
 
Highlight '''CaptaincyData.csv''' in the folder '''myProject'''
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | I have downloaded and moved this '''data set''' to the folder '''myProject''' on my '''Desktop'''.  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | I have downloaded and moved this '''data set''' to the folder '''myProject''' on my '''Desktop'''.  
 
  
 
I shall set this folder as my '''Working Directory.'''
 
I shall set this folder as my '''Working Directory.'''
Line 70: Line 62:
  
 
R scripts
 
R scripts
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | * <div style="margin-left:1.947cm;margin-right:0cm;">Remember we had created two scripts, '''myfirstscript.R''' and '''mysecondscript.R'''</div>
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" |  
 +
* <div style="margin-left:1.947cm;margin-right:0cm;">Remember we had created two scripts, '''myfirstscript.R''' and '''mysecondscript.R'''</div>
 
* <div style="margin-left:1.947cm;margin-right:0cm;">Both were saved in the folder '''myProject''', on the '''Desktop.'''</div>
 
* <div style="margin-left:1.947cm;margin-right:0cm;">Both were saved in the folder '''myProject''', on the '''Desktop.'''</div>
 
  
 
|-
 
|-
Line 78: Line 70:
  
 
Working directory  
 
Working directory  
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | * <div style="margin-left:1.947cm;margin-right:0cm;">'''R''' always points to a default '''directory''' on our computer</div>
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" |  
 +
* <div style="margin-left:1.947cm;margin-right:0cm;">'''R''' always points to a default '''directory''' on our computer</div>
 
* <div style="margin-left:1.947cm;margin-right:0cm;">This '''directory''' is where '''R''' will look, by default, for files you ask it to load.</div>
 
* <div style="margin-left:1.947cm;margin-right:0cm;">This '''directory''' is where '''R''' will look, by default, for files you ask it to load.</div>
 
  
 
|-
 
|-
Line 111: Line 103:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Click on the button with '''three dots''' at the right corner
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Click on the button with '''three dots''' at the right corner
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Go to the upper right corner of the same window.
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Go to the upper right corner of the same window.
 
  
 
Below the refresh icon, click on the button with three dots.  
 
Below the refresh icon, click on the button with three dots.  
Line 129: Line 120:
  
 
Select '''More >> Set As Working Directory'''
 
Select '''More >> Set As Working Directory'''
 
 
 
 
  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Now, below the '''Files''' tab, click on the '''More''' button with the blue gear symbol.  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Now, below the '''Files''' tab, click on the '''More''' button with the blue gear symbol.  
Line 166: Line 153:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | [RStudio]
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | [RStudio]
 
  
 
'''captaincy = read.csv("CaptaincyData.csv")'''
 
'''captaincy = read.csv("CaptaincyData.csv")'''
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | In '''Console '''window, type '''captaincy''' space '''equal''' sign space '''read.csv'''
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | In '''Console '''window, type '''captaincy''' space '''equal''' sign space '''read.csv'''
 
  
 
Now, '''CaptaincyData.csv''' inside double quotes in '''parentheses '''and press '''Enter'''.  
 
Now, '''CaptaincyData.csv''' inside double quotes in '''parentheses '''and press '''Enter'''.  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Highlight '''read.csv '''
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Highlight '''read.csv '''
 
 
  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | '''read.csv()''' '''function '''is used to read a file and to create a '''data-frame''' from it.  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | '''read.csv()''' '''function '''is used to read a file and to create a '''data-frame''' from it.  
Line 197: Line 180:
  
 
Summary
 
Summary
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | At the end of this tutorial, we have learnt how to* <div style="margin-left:1.947cm;margin-right:0cm;">get and set a '''working directory'''</div>
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | At the end of this tutorial, we have learnt how to
 +
* <div style="margin-left:1.947cm;margin-right:0cm;">get and set a '''working directory'''</div>
 
* <div style="margin-left:1.947cm;margin-right:0cm;">read and store a '''csv''' file </div>
 
* <div style="margin-left:1.947cm;margin-right:0cm;">read and store a '''csv''' file </div>
 
  
 
|-
 
|-
Line 205: Line 188:
  
 
Assignment
 
Assignment
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | We now suggest an assignment.* <div style="margin-left:1.947cm;margin-right:0cm;">Create a new folder and make it your '''Working Directory'''.</div>
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | We now suggest an assignment.
 
+
* <div style="margin-left:1.947cm;margin-right:0cm;">Create a new folder and make it your '''Working Directory'''.</div>
  
 
|-
 
|-
Line 213: Line 196:
 
About the Spoken Tutorial Project
 
About the Spoken Tutorial Project
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | The video at the following link summarises the Spoken Tutorial project.
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | The video at the following link summarises the Spoken Tutorial project.
 
  
 
Please download and watch it.
 
Please download and watch it.
Line 252: Line 234:
 
Thank You
 
Thank You
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | This script for this tutorial was contributed by Shaik Sameer (FOSSEE Fellow 2018).
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | 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.
 
This is Sudhakar Kumar from IIT Bombay signing off. Thanks for watching.

Revision as of 19:05, 19 December 2018

Title of script: Working directories in RStudio
Authors: Shaik Sameer (IIIT Vadodara) and Sudhakar Kumar (IIT Bombay)
Keywords: R, RStudio, working directory, read csv


Visual Cue Narration
Show slide

Opening slide

Welcome to this tutorial on Working directories in RStudio.
Show slide

Objectives

In this tutorial, you will learn about:
  • Working directories in RStudio
  • How to read and store csv files in R
Show slide

Pre-requisites

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.

Show slide

Systems requirements

This tutorial is recorded on
  • Ubuntu Linux OS 16.04
  • R version 3.2.3
  • RStudio version 1.1.463

Install R version 3.2.0 or higher.

Text on screen

Download CaptaincyData.csv file from the Code files link

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.

[Computer screen]

Highlight CaptaincyData.csv in the folder myProject

I have downloaded and moved this data set to the folder myProject on my Desktop.

I shall set this folder as my Working Directory.

Show slide

R scripts

  • Remember we had created two scripts, myfirstscript.R and mysecondscript.R
  • Both were saved in the folder myProject, on the Desktop.
Show slide

Working directory

  • 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’s switch to RStudio.

[RStudio]

Console >> type getwd()

In Console, type getwd parentheses and press Enter.
Point to /home/fossee in Console The current Working Directory is shown in the console.

Note down the directory path.

Now, we will learn how to set our Working Directory.
[RStudio]

Highlight myProject folder on Desktop

I will set the folder myProject on my Desktop as my Working Directory.
Go to graphical output window at the bottom right >> select the Files tab At the top of the Files and Plots window of RStudio, click on the Files tab.
Click on the button with three dots at the right corner Go to the upper right corner of the same window.

Below the refresh icon, click on the button with three dots.

Point to Go To Folder Go To Folder window appears.
Select myProject folder >> click Open Select the folder which you want to make as your Working Directory and click Open.

I am selecting myProject located on my Desktop.

Point to the two scripts under Files tab Under Files tab, you can see that the two scripts created earlier, have been loaded.
[RStudio]

Select More >> Set As Working Directory

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.

Point to the Console You may verify this by looking at the Console.
There is one other way to set the Working Directory also.
[RStudio]

Console >> type setwd("/home/fossee/

Desktop/myProject")

In the Console, type setwd.

Now, inside parentheses, type the desired folder path within double quotes.

Press Enter.

Highlight CaptaincyData.csv in the File tab 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.
[RStudio]

captaincy = read.csv("CaptaincyData.csv")

In Console window, type captaincy space equal sign space read.csv

Now, CaptaincyData.csv inside double quotes in parentheses and press Enter.

Highlight read.csv read.csv() function is used to read a file and to create a data-frame from it.
[RStudio]

View(captaincy)

Highlight the data frame in Source window

Now, we use View() function to see the stored data frame.

In Console window, type View captaincy in parentheses and press Enter.

The contents of captaincy data frame appears in the Source window.

This brings us to the end of this tutorial. Let us summarize.
Show slide

Summary

At the end of this tutorial, we have learnt how to
  • get and set a working directory
  • read and store a csv file
Show slide

Assignment

We now suggest an assignment.
  • Create a new folder and make it your Working Directory.
Show slide

About the Spoken Tutorial Project

The video at the following link summarises the Spoken Tutorial project.

Please download and watch it.

Show slide

Spoken Tutorial Workshops

We conduct workshops using Spoken Tutorials and give certificates.

Please contact us.

Show Slide

Forum to answer questions

Pls post your timed queries in this forum.
Show Slide

Forum to answer questions

Pls post your general queries in this forum.
Show Slide

Textbook Companion

The FOSSEE team coordinates coding of solved examples of popular books.

We give honorarium and certificates for those who do this.

For more details, please visit these sites.

Show Slide

Acknowledgement

The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India
Show Slide

Thank You

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.

Contributors and Content Editors

Nancyvarkey, Sudhakarst