1. Home
  2. Training Library
  3. Big Data
  4. Courses
  5. 4. Intermediate Data Structures in R

Arrays in R

Developed with


Fundamentals of R
Objects in R
Integers in R

The course is part of this learning path

Fundamentals of R
Start course

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. 


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 


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


- [Teacher] Arrays in R. In the same way that matrices generalize vectors, arrays generalize matrices. What is an array? An array in an n-dimensional homogenous structure. In the same way a vector can hold scalars, a matrix can hold vectors, an array can hold matrices, specifically a 3-D array. To scale this up, a 4-D holds several 3-D arrays, and so on. Let me create a 3-D array on the screen, using the array constructor, I input in the vector that I'd like to see, and then I use the dim argument for the dimensions, and here I'm asking for a two by three matrix one time. So there is one of them. What if I wanted to change that to being a one by three matrix here, but two times. I would see two matrices, and each matrix is of size one by three. I can create a two by two matrix three times. So long as the product of my dimensions is the same as the length of the data inside the array, I will see three matrices on the screen, and each matrix is of size two by two. I can ask for the dimensions of a array that I have created, and this will return a vector of two-two-three because I know that I created this. So for index sub setting, if I just bring D-3 up onto the screen again, I can ask for a subset of this array by grabbing in this case I would like to select the first row and the first column from every matrix. So that would be asking for the first row, first column of the first matrix, the one there at the top of the screen, and then asking for the first row, first column of the second matrix, asking for the five in the middle of the screen, and the third one would be the first row, and the first column of the third matrix, and hence when I ran this command here D-3 one one blank, I return one, five, nine. Arrays support complex sub setting including matrices and vectors, so if I was to enter in D-3, two, C, two, comma one, comma two this would return eight six. Now we can explain that by building this up slowly. So let me just clear the screen, and show you D-3, and let me show you what the. This shows a matrix by bringing in the number two. So we're asking for the two, four, six, eight, ten, twelve, column wise fills. Next I'm asking for in a specific order, a vector two comma one that returns the second one first and then the first one second. And the last section is just restricting us to being the second, okay. It's a bit hard to explain on a two dimensional screen, but I'll do my best to talk through what a four dimensional array looks like. We can start by using a vector called X. Let's bring X up onto the screen so we can see it. How do we element X into that? We can grab any element as such. We can ask for the length of this to make sure that it is a number, that is, we can create a four dimensional shape, so we need dimensions that multiply through to create this four dimensional array that we want. I'll reshape this X into a two by two by two by three. The product of these four numbers is 24. And now if I bring X up onto the screen, I have a four dimensional array, and I'll just scroll through this, and just show you that the two, three, four, five, at the beginning is in my first matrix, which is stored in the point, or the part of the four dimensional array with subscripts blank, blank, meaning the matrix that we're dealing with, and one, one. The next matrix , and the next dimension takes us over to the next four. And so on, and then it loops around to one because we only have two dimensions in our third dimension. What is the class of X? It is an array. Can I access, can I use element access to access this? If I was to clear the screen, I can take the first element of X which we know is four, I can also grab which we know is two, and I can also grab the last element of this which is 25. So if I bring X back up onto the screen, the last element was 25, and the first element was two. If I was to leave out the first two parameters, I can ask for the matrix at the position one, one. Or if I was to ask for two one, I would get back the matrix at the two one position.

About the Author
Kunal Haria
Data Science Trainer
Learning Paths

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.