# How to multiply two matrices in R if they contain missing values?

If we want to multiply two matrices if they contain missing values then we first need to convert the missing values to zeros and then the multiplication can be easily done. If we do not do so then the Output of the multiplication will have NAs at all positions.

Check out the Examples given below to understand the correct of multiplication if NAs are present in the matrices.

## Example 1

Following snippet creates a sample matrix −

M1<-matrix(sample(c(NA,rpois(2,5)),25,replace=TRUE),ncol=5)
M1

The following matrix is created −

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

To multiply two matrices if they contain missing values, on the above created matrix, add the following code to the above snippet −

M1<-matrix(sample(c(NA,rpois(2,5)),25,replace=TRUE),ncol=5)
M2<-matrix(sample(c(NA,rpois(2,5)),25,replace=TRUE),ncol=5)
M2

If you execute all the above given snippets as a single program, it generates the following Output −

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

To multiply two matrices if they contain missing values, on the above created matrix, add the following code to the above snippet −

M1<-matrix(sample(c(NA,rpois(2,5)),25,replace=TRUE),ncol=5)
M2<-matrix(sample(c(NA,rpois(2,5)),25,replace=TRUE),ncol=5)
M1[is.na(M1)]<-0
M1

If you execute all the above given snippets as a single program, it generates the following Output −

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

To multiply two matrices if they contain missing values, on the above created matrix, add the following code to the above snippet −

M1<-matrix(sample(c(NA,rpois(2,5)),25,replace=TRUE),ncol=5)
M2<-matrix(sample(c(NA,rpois(2,5)),25,replace=TRUE),ncol=5)
M1[is.na(M1)]<-0
M2[is.na(M2)]<-0
M2

If you execute all the above given snippets as a single program, it generates the following Output −

   [,1] [,2] [,3] [,4] [,5]
[1,] 7   7     7    0    3
[2,] 7   7     3    0    3
[3,] 7   3     0    0    0
[4,] 0   7     7    0    7
[5,] 0   3     7    3    7

To multiply two matrices if they contain missing values, on the above created matrix, add the following code to the above snippet −

M1<-matrix(sample(c(NA,rpois(2,5)),25,replace=TRUE),ncol=5)
M2<-matrix(sample(c(NA,rpois(2,5)),25,replace=TRUE),ncol=5)
M1[is.na(M1)]<-0
M2[is.na(M2)]<-0
M1 %*% M2

If you execute all the above given snippets as a single program, it generates the following Output −

    [,1] [,2] [,3] [,4] [,5]
[1,] 112 184  184    30 160
[2,] 112 172  182    30 158
[3,] 140 160  114    18 114
[4,] 210 212  142     0 102
[5,] 182 160  130    18  90

## Example 2

Following snippet creates a sample matrix −

M3<-matrix(sample(c(NA,round(rnorm(5),1)),25,replace=TRUE),ncol=5)
M3

The following matrix is created −

    [,1]  [,2] [,3] [,4] [,5]
[1,] -0.2 -0.1   NA -0.1 -0.1
[2,]  1.3 -0.1 -0.6   NA  1.3
[3,] -0.1 -0.1  0.4  1.3   NA
[4,]  NA   1.3   NA -0.2 -0.2
[5,]  NA    NA -0.1  0.4 -0.6

To multiply two matrices if they contain missing values, on the above created matrix, add the following code to the above snippet −

M3<-matrix(sample(c(NA,round(rnorm(5),1)),25,replace=TRUE),ncol=5)
M4<-matrix(sample(c(NA,round(rnorm(5),1)),25,replace=TRUE),ncol=5)
M4

If you execute all the above given snippets as a single program, it generates the following Output −

     [,1] [,2] [,3] [,4] [,5]
[1,] -0.1 -0.1 -0.9  0.7   NA
[2,]  0.7  0.7   NA  0.7 -0.1
[3,]   NA  1.1  0.7   NA -0.9
[4,]  0.7 -0.9   NA  0.7   NA
[5,] -0.7 -0.7  1.1 -0.9 -0.9

To multiply two matrices if they contain missing values, on the above created matrix, add the following code to the above snippet −

M3<-matrix(sample(c(NA,round(rnorm(5),1)),25,replace=TRUE),ncol=5)
M4<-matrix(sample(c(NA,round(rnorm(5),1)),25,replace=TRUE),ncol=5)
M3[is.na(M3)]<-0
M3

If you execute all the above given snippets as a single program, it generates the following Output −

     [,1] [,2] [,3] [,4] [,5]
[1,] -0.2 -0.1  0.0 -0.1 -0.1
[2,]  1.3 -0.1 -0.6  0.0  1.3
[3,] -0.1 -0.1  0.4  1.3  0.0
[4,]  0.0  1.3  0.0 -0.2 -0.2
[5,]  0.0  0.0 -0.1  0.4 -0.6

To multiply two matrices if they contain missing values, on the above created matrix, add the following code to the above snippet −

M3<-matrix(sample(c(NA,round(rnorm(5),1)),25,replace=TRUE),ncol=5)
M4<-matrix(sample(c(NA,round(rnorm(5),1)),25,replace=TRUE),ncol=5)
M3[is.na(M3)]<-0
M4[is.na(M4)]<-0
M4

If you execute all the above given snippets as a single program, it generates the following Output −

     [,1]  [,2] [,3] [,4] [,5]
[1,] -0.1 -0.1 -0.9  0.7  0.0
[2,]  0.7  0.7  0.0  0.7 -0.1
[3,]  0.0  1.1  0.7  0.0 -0.9
[4,]  0.7 -0.9  0.0  0.7  0.0
[5,] -0.7 -0.7 1.1  -0.9 -0.9

To multiply two matrices if they contain missing values, on the above created matrix, add the following code to the above snippet −

M3<-matrix(sample(c(NA,round(rnorm(5),1)),25,replace=TRUE),ncol=5)
M4<-matrix(sample(c(NA,round(rnorm(5),1)),25,replace=TRUE),ncol=5)
M3[is.na(M3)]<-0
M4[is.na(M4)]<-0
M3 %*% M4

If you execute all the above given snippets as a single program, it generates the following Output −

      [,1]  [,2]  [,3]  [,4]   [,5]
[1,] -0.05  0.11  0.07 -0.19  0.10
[2,] -1.11 -1.77 -0.16 -0.33 -0.62
[3,]  0.85 -0.79  0.37  0.77 -0.35
[4,]  0.91  1.23 -0.22  0.95  0.05
[5,]  0.70 -0.05 -0.73  0.82  0.63

Updated on: 05-Nov-2021

602 Views