Data Science & Statistics: Indexing an element from a matrix
Skills:
ML Maths Basics80%
Key Takeaways
The video demonstrates how to subset and select single and multiple values from a matrix in R, using indexing and slicing techniques similar to vectors, but taking into account the two-dimensional nature of matrices.
Full Transcript
all right now that we know how to create and name a matrix let's learn how to subset and select single and multiple values from it remember what we said about matrices they are natural extensions to vectors well subsetting matrices works in much the same way indexing and slicing happens with vectors we just need to take into account that matrices have two dimensions instead of one ok let's get to it I will create a matrix very very quickly you this matrix gives us the box office grosses of Oh 8 Harry Potter movies on US soil and worldwide I have intentionally left the column and row names blank we will have no difficulty adding them later okay take a look at the matrix we just made it has eight rows into columns and our index system accordingly rows are indexed with the number of the row followed by a comma in square brackets whereas columns are indexed by a comma followed by the column number again in square brackets so why is that well this is the way our notation system is all set up extracting a value happens with square brackets and the values you won't extract it in those square brackets if you're working with chloride dimensional objects think of ours extraction schema as looking like this basically the element that's always there is the comma okay let's try and call a single value from the matrix 897 it is located on the sixth row and in the second column and just like indexing vectors we can type HP mat open square bracket roll number comma column number close the square brackets and we get the value we wanted 897 great easy what if I wanted to call an entire row however with all the values on that row let's say I want to get the US and worldwide box office figures for the movie on the sixth row if I just type this our returns 290 a single value but why does that happen well here is why when we give our this specific instruction it looks for the sixth value not the row not the column so R goes through all the values in the matrix one by one column by column left to right and it counts 290 is the sixth value so this is what is returned if we really want to get the entire six row I should have typed the comma after the row number and use the blank space to indicate that I want every value on that row open bracket six comma blank space closed brackets this returns 290 and 897 all the values in the sixth row the exact same logic applies to extracting an entire column but this time the comma precedes the column index so HP mat square bracket blank space comma to close the square bracket and voila all eight values for the second column notice that our returns the result as a vector instead of a matrix this is because we're only asking for a one-dimensional object and a vector suffices to store such information which brings me to the next type of subsetting selecting multiple elements this and more will be the topic of the next lesson for more videos like this one please subscribe
Original Description
👉🏻 Download Our Free Data Science Career Guide: https://bit.ly/2EdpgRl
👉🏻 FREE MONTH! Get full access to our newly redesigned platform and all our courses (18th October - 18th November): https://bit.ly/3ga3g6Y
Learn how to select and extract single and multiple values from a matrix in R. Extracting a value from a matrix happens with square brackets, and the values you want extracted in them. If you’re working with more-dimensional objects, think of R’s extraction schema as looking like this: [ , ].
If we want to extract a row from a matrix, we should type row number, COMMA, and use a blank space to indicate that you want every value on that row.
The exact same logic applies to extracting an entire column from a matrix, but this time, the comma precedes the column index.
Selecting multiple elements.
When talking about vector slicing we learned that we can just pass a vector in the square brackets that indexes the multiple elements we want to extract. The same rule applies to matrices. For example, to select the 1st, 3rd, and 7th rows, all we need to do is open square brackets, pass in a vector of indexes where the rows element is – close the parentheses – comma, and close the square brackets.
We can subset matrices using the column names, and row names, too.
To extract a named row from a matrix, pass in the name of the row, and leave the space for columns blank.
► Consider hitting the SUBSCRIBE button if you LIKE the content: https://www.youtube.com/c/365DataScience?sub_confirmation=1
► VISIT our website: https://bit.ly/365ds
🤝 Connect with us LinkedIn: https://www.linkedin.com/company/365datascience/
365 Data Science is an online educational career website that offers the incredible opportunity to find your way into the data science world no matter your previous knowledge and experience. We have prepared numerous courses that suit the needs of aspiring BI analysts, Data analysts and Data scientists.
We at 365 Data Science are committed edu
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from 365 Data Science · 365 Data Science · 34 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
▶
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Population vs Sample
365 Data Science
Data Science & Statistics: Levels of measurement
365 Data Science
Statistics Tutorials: Mean, median and mode
365 Data Science
Skewness
365 Data Science
What is a distribution?
365 Data Science
The Normal Distribution
365 Data Science
Central limit theorem
365 Data Science
Student's T Distribution
365 Data Science
Type I error vs Type II error
365 Data Science
Hypothesis testing. Null vs alternative
365 Data Science
The linear regression model
365 Data Science
Simple linear regression model. Geometrical representation
365 Data Science
INDEX and MATCH application of the two functions separately and combined [Advanced Excel]
365 Data Science
INDIRECT Excel Function: How it works and when to use it [Advanced Excel]
365 Data Science
VLOOKUP and MATCH another useful functions combination [Advanced Excel]
365 Data Science
VLOOKUP COLUMN and ROW - Handle large data tables with ease [Advanced Excel]
365 Data Science
The ELIF keyword [Python Fundamentals]
365 Data Science
Working with Tuples in Python
365 Data Science
Database Terminology - A Beginners Guide
365 Data Science
Relational Database Essentials
365 Data Science
Database vs Spreadsheet - Advantages and Disadvantages
365 Data Science
Conditional Statements and Loops
365 Data Science
Backpropagation – The Math Behind Optimization
365 Data Science
Monte Carlo: Forecasting Stock Prices Part I
365 Data Science
Monte Carlo: Forecasting Stock Prices Part II
365 Data Science
Monte Carlo: Forecasting Stock Prices Part III
365 Data Science
365 Data Science Online Program
365 Data Science
Data frames - Creating a data frame
365 Data Science
Data Science & Statistics: Slicing a matrix in R
365 Data Science
Data frames in R - Exporting data in R
365 Data Science
Data frames in R - Transforming data PART II
365 Data Science
Data Frames in R - Subsetting a data frame
365 Data Science
Data Science & Statistics: Matrix arithmetic in R
365 Data Science
Data Science & Statistics: Indexing an element from a matrix
365 Data Science
Data Frames in R - Extending a data frame
365 Data Science
Data Science & Statistics: Creating a matrix in R FASTER
365 Data Science
Data Science & Statistics: Creating a Matrix in R
365 Data Science
Data frames - Importing data in R
365 Data Science
Data frames in R - Getting a sense of your data
365 Data Science
Data frames in R - Transforming data PART I
365 Data Science
Data frames in R - Import a CSV in R
365 Data Science
Data Science & Statistics: Matrix operations in R
365 Data Science
Data Science & Statistics: Matrix recycling in R
365 Data Science
Tableau vs Excel: When to use Tableau and when to use Excel
365 Data Science
Download Tableau: Learn how to download Tableau Public
365 Data Science
Connecting data sources: Useful tips when connecting data sources to Tableau
365 Data Science
The Tableau interface: See how to navigate through the Tableau interface
365 Data Science
Tableau data visualization: Create your first Tableau visualization!
365 Data Science
Duplicating sheets: This is how to duplicate a sheet in Tableau
365 Data Science
Build a table in Tableau: The steps needed to create a simple table in Tableau
365 Data Science
Custom fields in Tableau: Using Tableau operators to create custom fields
365 Data Science
Custom fields in Tableau: Add calculations to tables through custom fields
365 Data Science
Totals in Tableau: Learn how to display subtotals and totals in Tableau
365 Data Science
Gross Margin calculation in Tableau
365 Data Science
What is a filter in Tableau: Set up a filter in Tableau to specify the data you want to show
365 Data Science
Joins in Tableau: Inner, outer, left, or a right join in Tableau
365 Data Science
Building a Tableau dashboard: Three types of charts you want to have in a Tableau dashboard
365 Data Science
Creating great looking charts in Tableau: Real life Exercise on charts in Tableau
365 Data Science
Joins in Tableau: Choose the correct join type
365 Data Science
How to make a data check in Tableau: A quick data check is better than no data check
365 Data Science
More on: ML Maths Basics
View skill →Related AI Lessons
🎓
Tutor Explanation
DeepCamp AI