Creating Empty Matrix in R

Many times, we need an empty matrix and want to fill it later as per our requirement. Below are a few examples to achieve the same.In this article, we will be looking into creating an empty matrix in R and in the final section we will see how to verify if an object is a matrix or not.

  1. Create an empty matrix in R [NA filled].
  2. Create an empty matrix in R [Zero Rows].
  3. Create an empty matrix in R [Zero Columns].
  4. Verify if the object is a matrix or not.

 

Create an empty matrix in R [ NA filled ]

Here, we are creating a matrix of 5 columns and 5 rows filled with NA.  ‘NA’ in R means missing values. It is a logical constant of length 1.
We will be using the matrix() function.

Syntax of matrix() function :-
matrix(x, nrow, ncol, byrow, dimnames)
Arguments:-
x – is a vector (including a list or expression vector) input representing the elements in the matrix.
nrow – is the number specifying the number of rows in the matrix to be created.
ncol -is the number specifying the number of columns in the matrix to be created.
byrow – is a logical value if TRUE: the matrix is filled row-wise; else will be column-wise. By default, its value is FALSE.
dimnames– is the argument through which specifies the names of rows and columns.

So instead of adding any input as a vector, we will just be supplying ‘NA’ as the first argument and will specify the value of nrows and ncol arguments as 5.

Example:-

empty_matrix<-matrix(NA,5,5) 
print(empty_matrix)

Output:-

 [,1] [,2] [,3] [,4] [,5]
[1,]   NA   NA   NA   NA   NA
[2,]   NA   NA   NA   NA   NA
[3,]   NA   NA   NA   NA   NA
[4,]   NA   NA   NA   NA   NA
[5,]   NA   NA   NA   NA   NA

Create an empty matrix in R [ Zero Rows ]

In this section we will see how to create an empty matrix with zero rows and we will also be naming the rows for clarity.
Again, we will be using the matrix() function but specifying with arguments that the number of rows is zero (nrow=0).

Example:-

matrix_zero_row<-matrix(ncol=3,nrow=0) 
colnames(matrix_zero_row) <- c("Name","Age","Department")
print(matrix_zero_row)

Output:-

Name Age Department

The above output clarifies three columns created Name, Age, and Department, respectively, and there are no rows.

Create an empty matrix in R [ Zero columns ]

Similar to the above section, we created an empty matrix with zero rows; in this section, we will see how to create an empty matrix with zero columns, and again for clarity, we will give names to rows for clarity.
matrix() function is used but specifying with arguments that the number of columns is zero (ncol=0).

Example:-

matrix_zero_col<-matrix(ncol=0,nrow=3) 
rownames(matrix_zero_col) <- c("Student1","Student2","Student3")
print(matrix_zero_col)

Output:-

Student1
Student2
Student3

The above output clarifies three rows created Student1, Student2, and Student3, respectively, and there are no columns.

Verify if an object is a matrix or not.

To verify if the object is a matrix or not, R has provided a function is.matrix(x).

Here the object passed in the argument(x) is tested if it is a (strict) matrix.

The function would return TRUE if the object passed in the argument is a matrix.
The function would return FALSE if the object passed in the argument is not a matrix.

Example:-
In the below example code, we will be observing the output on testing the above three matrices created when passed in is.matrix() function.

is.matrix(empty_matrix)
is.matrix(matrix_zero_row)
is.matrix(matrix_zero_col)

Output:-

> is.matrix(empty_matrix)
[1] TRUE
> is.matrix(matrix_zero_row)
[1] TRUE
> is.matrix(matrix_zero_col)
[1] TRUE

Note that since all the three arguments passed are matrices; therefore, TRUE is printed three times on the console.

Know more about creating matrices and giving names to rows and columns.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top