Skip to content
Related Articles

Related Articles

Improve Article

Create matrix of zeros in R

  • Last Updated : 01 Apr, 2021

R programming language offers us a variety of ways to create a matrix and fill it in such a way that all the element values are equivalent to 0. Let’s see those ways –

Using matrix() method

The in-built matrix() method in R can be used to create a matrix with a given set of values, that is, n x m dimensions, and initialize it with a specified value. All the elements are initialized with the same value. If either of the m or n parameters is not specified, an attempt is made to infer the missing value from the length of data and the other parameter(s) that are given. If neither of them is given, then a one-column matrix is returned as an output. This matrix can then be stored in a variable and then its elements can be accessed and manipulated. 

Syntax: matrix(0, n, m)

Parameters:

  • 0 – value to initialise the matrix with
  • n – no. of rows
  • m – no. of columns

Return type : a matrix or scalar of zeros



Example: 

R




# initializing a matrix of 0s of 2*3 dimensions
mat = matrix(0, 2, 3)
  
# print the matrix
print (mat)

Output

     [,1] [,2] [,3]

[1,]    0    0    0

[2,]    0    0    0

 Using replicate() method

The replicate() method is used to create a replica of the second argument of the method vec, by appending it n times. It applies the same specified vector repeatedly to form a 2D matrix. The method belongs to the apply set of functions used in R and uses it as its parent or base class. The second argument is specified by enclosing within numeric(int) value. Also, the numeric method creates a real vector of the specified length. The elements of the vector are all equal to 0 on numeric application.

Syntax: replicate ( n , numeric (m) )



Parameter :

  • n – no. of rows
  • numeric(m) – no. of columns in the matrix, specified as a numeric parameter

Return type : a matrix or scalar of zeros

Example:

R




# initializing a matrix of 0s of 2*3 dimensions
mat = replicate( 6, numeric(3) )
  
# print the matrix
print ("Matrix : ")
print (mat)

Output

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

[1,]    0    0    0    0    0    0

[2,]    0    0    0    0    0    0

[3,]    0    0    0    0    0    0

 Using rep() method 

rep() method in R can be used to create a one row matrix, which creates the number of columns equivalent to the value in the second argument of the method. The first argument, specifies the vector to repeat and stack together y times, which in this case is 0. We can specify 0L instead of 0. 



Syntax: rep (0 , y)

Arguments : y – number of columns in matrix

Return type : Single row matrix of zeros

Example:

R




print ("Matrix : ")
  
# create a matrix of single 0 and 10 columns
rep(0, 10)

Output

[1] “Matrix : “

[1] 0 0 0 0 0 0 0 0 0 0

Using Numeric() and integer()

There are several other methods, like numeric() or integer() which can be used to create a vector of zeros. All of these methods takes an argument the length, specifying the number of zeros to combine.  The behavior of integer() and numeric() methods is almost same. 

Syntax:



numeric(size)

integer(size)

Example:

R




print ("Matrix using numeric() method:")
# create a matrix of single 0 and 8 columns
numeric (8)
  
print ("Matrix using integer() method:")
# create a matrix of single 0 and 5 columns
integer (5)

Output

[1] “Matrix using numeric() method:”

[1] 0 0 0 0 0 0 0 0

[1] “Matrix using integer() method:”

[1] 0 0 0 0 0




My Personal Notes arrow_drop_up
Recommended Articles
Page :