- Home
- Training Library
- Big Data
- Courses
- 4. Intermediate Data Structures in R

# Matrices in R

## Contents

###### Fundamentals of R

## The course is part of this learning path

**Course Description**

This module looks at more complex data structures, building on what was covered in the beginner data structures module.

**Learning Objectives**

The objectives of this module are to provide you with an understanding of:

- Different data types
- Integers
- How to coerce elements, and force coercion
- How to construct a matrix
- How to construct an array

- How to construct a list

**Intended Audience**

Aimed at all who wish to learn the R programming language.

**Pre-requisites**

No prior knowledge of R is assumed

Delegates should already be familiar with basic programming concepts such as variables, scope and functions

Experience of another scripting language such as Python or Perl would be an advantage

Understanding mathematical concepts will be beneficial

**Feedback**

We welcome all feedback and suggestions - please contact us at qa.elearningadmin@qa.com to let us know what you think.

- [Instructor] In order to construct a matrix, we use the matrix constructor. We input in a vector and then we ask or specify for dimensions using nrow argument for the number of rows and ncolumn argument for the number of columns. A Matrix constructor will create a two-dimensional shape or a two dimensional structure. And by default, fill in column-wise. When I say column-wise, I mean, as we ran through the column and finished the column, we moved on to the second column, three, then four and as I ran through that column and finished that column, I moved on to the third column, five and then six. There is another way to construct a matrix, I can construct a vector of numbers in advance. Say the height vector, I can create this with just a simple case of running the numbers from 161 through to 180 or inside a vector. Either case, return the same output. I can ask for the class of this and show that it is of integer class. Let's take note of the type of the underlying data that we have is integers. This will not change when I manipulate this vector into a matrix. I can ask for the length so I understand what my dimensions of my matrix should be. So any product of 20, any factors of 20, I can use those as my dimensions for my matrix using the dim function for dimensions I can transform my vector into a matrix. I have now changed the dimensions of my height vector into a matrix. If I was to run this function here, I would see a change that we've moved from a class of integers to a class of matrix. What is noteworthy is that the underneath data has not changed. The display for height will have changed, where previously we had a vector ranging from 161 to 180. We now have a matrix ranging from 161 to 180 but each number has not only just one index, but two indices. To make this a bit clearer on the screen, I will use a 5 by 4, two numbers that multiply to 20 using different dimensions to transform our matrix. And now we see that we have constructed this column-wise 161, 162, 163, 164, 165 and as that column has now come to a close, we now move on to populate the second column, and so forth. I can access each element where previously I would have used just height one, now I use row and column indices. I can access any element in this manner, say, for example, I would have liked to pick 175 here in the fifth row in the third column. Swapping these indices will not return anything sensible because the third row does not contain a fifth column. Subscripts out of bounds. So we return a useful error as a result. Another way to construct a matrix is to use the rbind or cbind functions which allow us to bind together different vectors into a matrix or even different matrices. And here I you cbind to have column binding so my first vector is low, my second vector is high. I can use rbind to have things, have these vectors bound one on top of the other. I can construct a matrix row-wise, so let me remind you what we had earlier, which was column-wise by default. 1, 2, that column comes to a finish. Move on to the second column, 3, 4. So this is how you might fill data in by default in the world of mathematics, by pen and paper. By introducing the argument byrow, we're now considering entering and constructing a matrix row-wise. We fill in our data horizontally. Once that row comes to a close, we move on to the second row. This may be how data is input into a computer or into a database in the computer, so you'd fill in each row one by one. As opposed to the default case, which is column-wise, which is the world of mathematics. I can construct a matrix as such, creating it with a vector of 0, 1 and making it 2 by 2. Here we have column-wise fils and we have vector recycling. I can also name my dimensions and use those dimensions to access my data. So here I've decided to use dimnames as the argument of the matrix function and I'm inputting in a list of the names that I'd like to assign to each of my different rows and columns. And I can then use elements access to access, any of my data points. Here, I'm using Row1 so I can pick Row1 and column two. And thus I pick the last entry there and I see an output of zero.

Kunal has worked with data for most of his career, ranging from diffusion markov chain processes to migrating reporting platforms.

Kunal has helped clients with early stage engagement and formed multi week training programme curriculum.

Kunal has a passion for statistics and data; he has delivered training relating to Hypothesis Testing, Exploring Data, Machine Learning Algorithms, and the Theory of Visualisation.

Data Scientist at a credit management company; applied statistical analysis to distressed portfolios.

Business Data Analyst at an investment bank; project to overhaul the legacy reporting and analytics platform.

Statistician within the Government Statistical Service; quantitative analysis and publishing statistical findings of emerging levels of council tax data.

Structured Credit Product Control at an investment bank; developing, maintaining, and deploying a PnL platform for the CVA Hedging trading desk.