Difference between revisions of "R/C2/Indexing-and-Slicing-Data-Frames/English"
Sudhakarst (Talk | contribs) |
Sudhakarst (Talk | contribs) |
||
| Line 44: | Line 44: | ||
* <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.456'''</div> | * <div style="margin-left:1.947cm;margin-right:0cm;">'''RStudio''' version''' 1.1.456'''</div> | ||
| − | |||
Install '''R''' version '''3.2.0''' or higher. | Install '''R''' version '''3.2.0''' or higher. | ||
|- | |- | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Show slide | | style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Show slide | ||
| − | |||
Download Files | Download Files | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | For this tutorial, we will use the '''data frame CaptaincyData.csv''' and a '''script '''file '''mydataframe.R'''. | | style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | For this tutorial, we will use the '''data frame CaptaincyData.csv''' and a '''script '''file '''mydataframe.R'''. | ||
| − | |||
Download these files from the '''Code files''' link of this tutorial. | Download these files from the '''Code files''' link of this tutorial. | ||
| Line 61: | Line 58: | ||
Highlight '''CaptaincyData.csv '''and''' mydataframe.R '''in the folder '''myProject''' | Highlight '''CaptaincyData.csv '''and''' mydataframe.R '''in the folder '''myProject''' | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | I have downloaded and moved these files to the folder '''myProject''' on my '''Desktop'''. | | style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | I have downloaded and moved these files to the folder '''myProject''' on my '''Desktop'''. | ||
| − | |||
I have also set this folder as my '''Working Directory.''' | I have also set this folder as my '''Working Directory.''' | ||
| Line 71: | Line 67: | ||
Click '''mydataframe.R''' in '''RStudio''' | Click '''mydataframe.R''' in '''RStudio''' | ||
| − | |||
Point to''' mydataframe.R''' in '''Rstudio.''' | Point to''' mydataframe.R''' in '''Rstudio.''' | ||
| Line 77: | Line 72: | ||
For this, click on the '''script mydataframe.R''' | For this, click on the '''script mydataframe.R''' | ||
| − | |||
'''Script mydataframe.R '''opens in''' Rstudio.''' | '''Script mydataframe.R '''opens in''' Rstudio.''' | ||
| Line 87: | Line 81: | ||
'''View(captaincy)''' | '''View(captaincy)''' | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Here, we have declared a''' variable captaincy''' to store and read '''CaptaincyData.csv'''. | | style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Here, we have declared a''' variable captaincy''' to store and read '''CaptaincyData.csv'''. | ||
| − | |||
Also, '''View function''' is being used to see the contents of the file. | Also, '''View function''' is being used to see the contents of the file. | ||
| Line 93: | Line 86: | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Highlight''' <-''' symbol in the '''Source''' window | | style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Highlight''' <-''' symbol in the '''Source''' window | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Remember, you may also use '''equal''' to sign in place of '''less than''' symbol followed by '''hyphen'''. | | style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Remember, you may also use '''equal''' to sign in place of '''less than''' symbol followed by '''hyphen'''. | ||
| − | |||
However, we recommend <span style="color:#434343;">'''l</span>ess than''' symbol followed by '''hyphen'''. | However, we recommend <span style="color:#434343;">'''l</span>ess than''' symbol followed by '''hyphen'''. | ||
| − | |||
For this, there is a shortcut in '''RStudio'''. | For this, there is a shortcut in '''RStudio'''. | ||
| − | |||
Suppose we want to assign a value of 2 to a variable named '''testvar'''. | Suppose we want to assign a value of 2 to a variable named '''testvar'''. | ||
| − | |||
| − | |||
|- | |- | ||
| Line 135: | Line 123: | ||
'''captaincy[3,]''' | '''captaincy[3,]''' | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | In the '''Source '''window, type '''captaincy ''' | | style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | In the '''Source '''window, type '''captaincy ''' | ||
| − | |||
Type '''capt''' and press '''Enter '''to make the variable '''captaincy '''autocomplete. Now''' '''within square brackets 3 followed by '''comma. '''And press '''Enter'''. | Type '''capt''' and press '''Enter '''to make the variable '''captaincy '''autocomplete. Now''' '''within square brackets 3 followed by '''comma. '''And press '''Enter'''. | ||
| − | |||
Press '''Enter '''key''' '''at the end of every command. | Press '''Enter '''key''' '''at the end of every command. | ||
| Line 184: | Line 170: | ||
'''captaincy[c(2,3),]''' | '''captaincy[c(2,3),]''' | ||
| − | |||
| Line 253: | Line 238: | ||
|- | |- | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Highlight 1 in square brackets | | style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Highlight 1 in square brackets | ||
| − | |||
| − | |||
| style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Please note that I have not used a '''comma''' inside the square brackets. | | style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | Please note that I have not used a '''comma''' inside the square brackets. | ||
| Line 297: | Line 280: | ||
'''captaincy[c("names", "won")]''' | '''captaincy[c("names", "won")]''' | ||
| − | |||
| − | |||
| style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | In the '''Source '''window, type the following command and press '''Enter'''. | | style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | In the '''Source '''window, type the following command and press '''Enter'''. | ||
| Line 325: | Line 306: | ||
|- | |- | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;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:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | [RStudio] | ||
| − | |||
Highlight '''victory '''column of '''captaincy '''in the '''Source '''window | Highlight '''victory '''column of '''captaincy '''in the '''Source '''window | ||
| Line 333: | Line 313: | ||
|- | |- | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;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:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | [RStudio] | ||
| − | |||
Highlight '''names''', '''played '''and '''won '''columns of '''captaincy''' | Highlight '''names''', '''played '''and '''won '''columns of '''captaincy''' | ||
| − | |||
| − | |||
| style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | <span style="background-color:#ffffff;">For this </span><span style="background-color:#ffffff;">'''subset'''</span><span style="background-color:#ffffff;">, we shall only show the </span>* <div style="margin-left:1.947cm;margin-right:0cm;"><span style="background-color:#ffffff;">names</span></div> | | style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | <span style="background-color:#ffffff;">For this </span><span style="background-color:#ffffff;">'''subset'''</span><span style="background-color:#ffffff;">, we shall only show the </span>* <div style="margin-left:1.947cm;margin-right:0cm;"><span style="background-color:#ffffff;">names</span></div> | ||
* <div style="margin-left:1.947cm;margin-right:0cm;"><span style="background-color:#ffffff;">number of matches </span><span style="background-color:#ffffff;">'''played'''</span></div> | * <div style="margin-left:1.947cm;margin-right:0cm;"><span style="background-color:#ffffff;">number of matches </span><span style="background-color:#ffffff;">'''played'''</span></div> | ||
* <div style="margin-left:1.947cm;margin-right:0cm;"><span style="background-color:#ffffff;">number of matches </span><span style="background-color:#ffffff;">'''won'''</span></div> | * <div style="margin-left:1.947cm;margin-right:0cm;"><span style="background-color:#ffffff;">number of matches </span><span style="background-color:#ffffff;">'''won'''</span></div> | ||
| − | |||
|- | |- | ||
| Line 362: | Line 338: | ||
|- | |- | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;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:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | [RStudio] | ||
| − | |||
Highlight '''select ''' | Highlight '''select ''' | ||
| − | |||
'''print(<span style="background-color:#ffffff;">subData </span>)''' | '''print(<span style="background-color:#ffffff;">subData </span>)''' | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | <span style="background-color:#ffffff;">The</span><span style="background-color:#ffffff;">''' select parameter'''</span><span style="background-color:#ffffff;"> is used to select the required columns, </span><span style="background-color:#ffffff;">'''names'''</span><span style="background-color:#ffffff;">, </span><span style="background-color:#ffffff;">'''played'''</span><span style="background-color:#ffffff;">, and </span><span style="background-color:#ffffff;">'''won'''</span><span style="background-color:#ffffff;">. </span> | | style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | <span style="background-color:#ffffff;">The</span><span style="background-color:#ffffff;">''' select parameter'''</span><span style="background-color:#ffffff;"> is used to select the required columns, </span><span style="background-color:#ffffff;">'''names'''</span><span style="background-color:#ffffff;">, </span><span style="background-color:#ffffff;">'''played'''</span><span style="background-color:#ffffff;">, and </span><span style="background-color:#ffffff;">'''won'''</span><span style="background-color:#ffffff;">. </span> | ||
| − | |||
<span style="background-color:#ffffff;">In the </span><span style="background-color:#ffffff;">'''Source '''</span><span style="background-color:#ffffff;">window, type </span><span style="background-color:#ffffff;">'''print'''</span><span style="background-color:#ffffff;"> within </span><span style="background-color:#ffffff;">'''parentheses subData '''</span><span style="background-color:#ffffff;">.</span> | <span style="background-color:#ffffff;">In the </span><span style="background-color:#ffffff;">'''Source '''</span><span style="background-color:#ffffff;">window, type </span><span style="background-color:#ffffff;">'''print'''</span><span style="background-color:#ffffff;"> within </span><span style="background-color:#ffffff;">'''parentheses subData '''</span><span style="background-color:#ffffff;">.</span> | ||
| Line 420: | Line 393: | ||
Summary | Summary | ||
| − | | style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | In this tutorial, we have learnt how to:* <div style="margin-left:1.947cm;margin-right:0cm;">Extract rows or columns from a '''data frame'''</div> | + | | style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.203cm;padding-right:0.191cm;" | In this tutorial, we have learnt how to: |
| + | * <div style="margin-left:1.947cm;margin-right:0cm;">Extract rows or columns from a '''data frame'''</div> | ||
* <div style="margin-left:1.947cm;margin-right:0cm;">Create a '''subset''' from a '''data frame '''</div> | * <div style="margin-left:1.947cm;margin-right:0cm;">Create a '''subset''' from a '''data frame '''</div> | ||
* <div style="margin-left:1.947cm;margin-right:0cm;">Retrieve data using double square brackets </div> | * <div style="margin-left:1.947cm;margin-right:0cm;">Retrieve data using double square brackets </div> | ||
| − | |||
|- | |- | ||
| Line 429: | Line 402: | ||
Assignment | Assignment | ||
| − | | style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;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 '''subset '''from '''captaincy data frame''' with the captains who have played > 20 matches and lost < 14 matches. </div> | + | | style="background-color:#ffffff;border-top:none;border-bottom:1.25pt solid #000001;border-left:1.25pt solid #000001;border-right:1.25pt solid #000001;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 '''subset '''from '''captaincy data frame''' with the captains who have played > 20 matches and lost < 14 matches. </div> | |
|- | |- | ||
Revision as of 19:15, 11 February 2019
Title of script: Indexing and Slicing Data Frames
Author: Shaik Sameer (IIIT Vadodara) and Sudhakar Kumar (IIT Bombay)
Keywords: R, RStudio, data frames, indexing, slicing, working directory, video tutorial.
| Visual Cue | Narration |
| Show slide
Opening slide |
Welcome to the spoken tutorial on Indexing and Slicing Data Frames. |
| Show slide
Learning Objectives |
In this tutorial, we will learn how to:
|
| Show slide
Pre-requisites |
To understand this tutorial, you should have knowledge about
If not, please locate the relevant tutorials on R on this website. |
| Show slide
System Specifications |
This tutorial is recorded on
Install R version 3.2.0 or higher. |
| Show slide
Download Files |
For this tutorial, we will use the data frame CaptaincyData.csv and a script file mydataframe.R.
Download these files from the Code files link of this tutorial. |
| [Computer screen]
Highlight CaptaincyData.csv and mydataframe.R in the folder myProject |
I have downloaded and moved these files to the folder myProject on my Desktop.
I have also set this folder as my Working Directory. |
| Let us switch to RStudio. | |
| Point to script mydataframe.R.
Click mydataframe.R in RStudio Point to mydataframe.R in Rstudio. |
Let us open the script mydataframe.R in RStudio.
For this, click on the script mydataframe.R Script mydataframe.R opens in Rstudio. |
| [RStudio]
captaincy <- read.csv("CaptaincyData.csv") View(captaincy) |
Here, we have declared a variable captaincy to store and read CaptaincyData.csv.
Also, View function is being used to see the contents of the file. |
| Highlight <- symbol in the Source window | Remember, you may also use equal to sign in place of less than symbol followed by hyphen.
However, we recommend less than symbol followed by hyphen. For this, there is a shortcut in RStudio. Suppose we want to assign a value of 2 to a variable named testvar. |
| [RStudio]
Alt + - testvar <- 2 |
In the Console window, type testvar and then press
Alt and -(hyphen) keys simultaneously. Then type 2 and press Enter. |
| Highlight mydataframe.R in the Source window | Let us get back to the Source window.
Run the script mydataframe.R by clicking on the Source button. |
| [RStudio]
Highlight third row of captaincy |
Now let us extract the contents of the third row of the captaincy data frame. |
| Click on the script mydataframe.R | |
| [RStudio]
|
In the Source window, type captaincy
Type capt and press Enter to make the variable captaincy autocomplete. Now within square brackets 3 followed by comma. And press Enter. Press Enter key at the end of every command. Remember one of the most important features of RStudio include intelligent auto-completion of function names, packages, and R objects. |
| Highlight comma in the Source window | We use a comma within square brackets when we wish to extract a row. |
| Press Ctrl + S >> Press Ctrl+Enter keys. | Save the script and execute the current line by pressing Ctrl+Enter keys. |
| Highlight the output in the Console window | The third row of the captaincy data frame is seen in the Console window. |
| Highlight comma in captaincy[3,] | Now, let us run the same command without a comma. |
| [RStudio]
captaincy[3] |
In the Source window, type captaincy then within square brackets 3. |
| Press Ctrl + S >> Press Ctrl+Enter keys. | Save the script and run this line only, as shown earlier. |
| Highlight the output in the Console window | The contents of the third column of the data frame, are displayed on the Console window. |
| So, to extract a column, we shouldn’t use a comma within the square brackets.
When we extract data using row number or column number, it is known as numeric indexing. | |
| In the Source window, click on the captaincy data frame. | |
| [RStudio]
Highlight second and third rows of captaincy |
Let us now extract the contents of second and third rows of the captaincy data frame. |
| [RStudio]
|
To retrieve more than one row, we use a numeric index vector. Click on the script mydataframe.R
In the Source window, type the following command and press Enter. |
| Highlight the c() function | c function is being used to concatenate the second and third rows. |
| Press Ctrl + S >> Press Ctrl+Enter keys. | Save the script and execute the current line. |
| Highlight the output in the Console window | The second and third rows of captaincy data frame are seen in the Console window. |
| In the Source window, click on the captaincy data frame. | |
| Now, we’ll find who has played 25 matches from the played column of captaincy data frame.
Extracting this type of information is known as logical indexing. | |
| Click on the script mydataframe.R | |
| [RStudio]
|
In the Source window, type captaincy
Within square brackets captaincy dollar sign played equal to equal to 25 comma. Press Enter. |
| Highlight dollar sign
|
Remember, dollar sign allows you to extract elements by name.
Please note that there is no space between the two equal to signs. |
| Press Ctrl + S >> Press Ctrl+Enter keys. | Save the script and execute the current line. |
| Highlight the output in the Console window | The details of captain Dravid are shown in the Console window. |
| click on the captaincy data frame in Source window. | In the Source window, click on the captaincy data frame. |
| Now let us learn how to get the values of any particular attribute for all the players.
We will fetch the names of all the captains. | |
| [RStudio]
Highlight first column of captaincy captaincy[1] |
For this, we need to know the values in the first column.
Click on the script mydataframe.R In the Source window, type captaincy and within square brackets 1. |
| Highlight 1 in square brackets | Please note that I have not used a comma inside the square brackets. |
| Press Ctrl + S >> Press Ctrl+Enter keys. | Save the script and execute the current line. |
| Highlight the output in the Console window | The names of the captains are seen in the Console window. |
| Usually we use column names instead of column numbers. | |
| [RStudio]
captaincy["names"] |
To know the names of the captains, type captaincy.
Within square brackets inside double quotes names |
| Press Ctrl + S >> Press Ctrl+Enter keys. | Save the script and execute the current line. |
| Point to the names in the Console window. | Names of the captains are shown in the Console window.
Extracting data by column names is known as name indexing. |
| Highlight the broom icon in the Console window | Clear the Console window by clicking on the broom icon. |
| In the Source window, click on the captaincy data frame. | |
| cursor in the Source window. | Now let us view the names of the captains along with the number of matches they have won. |
| Click on the script mydataframe.R | |
| [RStudio]
captaincy[c("names", "won")] |
In the Source window, type the following command and press Enter. |
| Highlight c() | Here c function is used to concatenate names and won. |
| Highlight names and won | Observe that names and won have been written within double quotes. |
| Press Ctrl + S >> Press Ctrl+Enter keys. | Save the script and execute this line. |
| Point to the names of the captains in the Console window. | The names of captains and the number of matches won are seen in the Console window. |
| Now let us extract a subset from captaincy data frame.
We will create a subset of captains, who have won more than 30% of their matches. This is called slicing a data frame. | |
| click on the captaincy data frame in Source window. | In the Source window, click on the captaincy data frame. |
| [RStudio]
Highlight victory column of captaincy in the Source window |
Please note that there is a column named victory in the captaincy data frame.
For required subset of captains, their victory should be greater than 0.3. |
| [RStudio]
Highlight names, played and won columns of captaincy |
For this subset, we shall only show the * names
|
| Click on the script mydataframe.R | |
| I will resize the Source window. | |
| [RStudio]
subData <- subset(captaincy, victory > 0.3, select = c("names", "played", "won")) |
In the Source window, type the following command
Now, press Enter after the comma followed by 0 point 3. |
| You can press Enter after a comma for better visibility. | |
| [RStudio]
Highlight select print(subData ) |
The select parameter is used to select the required columns, names, played, and won.
In the Source window, type print within parentheses subData . |
| Highlight two lines
Press Ctrl + S >> Press Ctrl+Enter keys. |
Save the script and run these two lines. |
| I am resizing the Console window to see the output properly. | |
| Highlight the output in the Console window | The subData is shown in the Console window. |
| Click on the captaincy data frame in Source window. | In the Source window, click on the captaincy data frame. |
| [RStudio]
|
Finally, let us learn how to extract a particular entry from some column of a data frame.
|
| Click on the script mydataframe.R | |
| [RStudio]
|
In the Source window, type captaincy within double square brackets 4 within single square brackets 3. |
| Press Ctrl + S >> Press Ctrl+Enter keys. | Save the script and execute the current line. |
| Highlight the output in the Console window | The expected value 14 is seen in the Console window. |
| For more information on indexing and slicing data frames, please refer to the Additional materials section on this website. | |
| Let us summarize what we have learnt. | |
| Show slide
Summary |
In this tutorial, we have learnt how to:
|
| Slide 8
Assignment |
We now suggest an assignment.
|
| 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 |
Please post your timed queries in this forum. |
| Show Slide
Forum to answer questions |
Please post your general queries in this forum. |
| Show Slide
Textbook Companion |
The FOSSEE team coordinates the TBC project.
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 |
The script for this tutorial was contributed by Shaik Sameer (FOSSEE Fellow 2018).
|