Introduction
R is a programming language primarily used for statistical analysis and computing. With R programming, you can perform complex mathematical operations and statistical analysis and create high-quality plots and graphs. It is not just a programming language but an environment to implement statistical techniques.
Matrix in R programming is a rectangular arrangement of numbers. You can think of a matrix as a table with rows and columns or as a two-dimensional array that has rows and columns.
A matrix in R Programming Language is a two-dimensional data structure. It is a homogeneous data structure i.e., you cannot store two different data types like - numbers and strings together in one matrix but you can store them in two different matrices.

In this article, we will discuss how to create, access and modify a matrix in R-programming language.
Also see, Must Do Coding Questions
Creating a Matrix
There are two methods of creating a matrix in R programming; you can use the matrix() function to create a matrix and you can also create a matrix from a vector.
Note: In r-programming, the indexing starts from 1. Consider the following example matrix -
1 |
2 |
3 |
|
1 |
10 |
50 |
90 |
2 |
20 |
60 |
100 |
3 |
30 |
70 |
110 |
4 |
40 |
80 |
120 |
Using matrix() function
You can use the matrix() function to create a matrix in R programming. By default, a matrix is filled column-wise however, this can be changed by using the arguments provided by the matrix() function.
Syntax
matrix(data, nrow, ncol, byrow, dimnames)
This function has the following arguments:
data - Enter the values to be filled in the matrix.
nrow - the number of rows the matrix should contain
ncol - the number of columns the matrix should contain
byrow - by default it is set as FALSE. The matrix in R programming is filled column-wise, to fill the matrix row-wise set it to TRUE.
dimnames - you can pass a 2-element list to the argument to set names for the rows and columns.
Example: Let’s create a 3x3 matrix with data - 1,2,3,4,5,6,7,8,9
Code
matrix(c(1,2,3,4,5,6,7,8,9),nrow=3, ncol=3)
Note: c() is a concatenate function, that is popularly used to create a one-dimensional vector.
✅ Output:
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
Note: It is not necessary to provide both dimensions of the matrix. If one of the dimensions is present, the other dimension will be calculated from the length of the data.
In the above example matrix, notice that the data has been filled column-wise. We will also discuss how to fill data row-wise later in this article.
Matrices in R are homogeneous data structures. If you try to add data of different data types R will automatically convert the data into the simplest data structure out of all the different data types available i.e. if you store values 1, 2.0 and ninja in a matrix R, will convert the data into a character data type.
The rough order of data types is - logical < integer < numeric < complex < character < list.
Also See, 8085 Microprocessor Pin Diagram
Using Vectors
In R programming, a vector is a sequence of the same type of data elements. We can use a vector as a matrix by setting the dimensions for the vector.
Use the dim() function to set the dimensions for the vector to convert the vector into a matrix.
Syntax
dim() <- c(nrow, ncol)
dim() is the dimensions function.
c() is the concatenate function.
nrow and ncol are number of rows and columns respectively.
Example
Code
Let’s create a 2x3 matrix with data 1,2,3,4,5,6
1️⃣ Start by creating a one-dimensional vector.
Code
x <- c(1,2,3,4,5,6)
x
Note: x is a variable that we have created to store the vector.
✅ Output
[1] 1 2 3 4 5 6
2️⃣ Using dim() set the dimensions for the vector. For our example, the dimensions will be 2,3 i.e. nrow will be 2 and ncol will be 3.
Code
dim(x) <- c(2,3)
✅ Output
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
Create a row-wise matrix
By default, the matrix in R programming is filled column-wise.
We can fill the data row-wise by setting the byrow argument of the matrix() function to True.
Syntax
matrix(data, nrow, ncol, byrow = TRUE, dimnames)
The explanation for the arguments has already been given in the section - using matrix() function.
Example
Let’s create a 3x3 matrix with values - 1,2,3,4,5,6,7,8,9
Code
matrix(c(1,2,3,4,5,6,7,8,9), nrow=3, ncol=3, byrow=TRUE)
✅ Output
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Recommended Topic, Cognizant Eligibility Criteria



