R Program to create a blank or empty matrix


March 10, 2022, Learn eTutorial
949

Here we are explaining how to write an R program to create a blank or empty matrix. For a better understanding of this R program example, we always recommend you to learn the basic topics of R programming listed below:

How to create a blank matrix

Here we are explaining how to write an R program to create a blank matrix. Here we are using a built-in function matrix() for this conversion. This method helps to creates a matrix from the given set of values. The syntax of this function is,

matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE,dimnames = NULL)

NA: An optional data vector.

nrow: The desired number of rows.

ncol: The desired number of columns.

byrow: If FALSE (the default) the matrix is filled by columns, otherwise filled by rows.

dimnames: NULL or a list of length 2 giving the row and column names respectively.

How to create a blank matrix in the R Program

Below are the steps used in the R program to create a blank matrix. In this R program, we directly give the values to built-in functions. And print the function result. Here we used variables Matx for assigning matrix.

ALGORITHM

STEP 1: Assign variable Matx with matrix values

STEP 2: Create a matrix of 6 rows with 5 columns

STEP 3: Create by calling like  matrix(, nrow = 6, ncol = 5)

STEP 4: print the result matrix

 

R Source Code

                                          Matx=matrix(, nrow = 6, ncol = 5)
print("Empty matrix of 6 rows and 5 columns:")
print(Matx)
                                      

OUTPUT

[1] "Empty matrix of 6 rows and 5 columns:"
      [,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
 [6,]   NA   NA   NA   NA   NA