Search code examples
rmatrixdata-manipulationmissing-data

Pad the missing data with NA for two matrices


I have two square matrices where both of them have some missing data. I want to pad the missing data with NA in the two matrices

Data below:

#first matrix
t1 = matrix(
  c(1, 0, 1, 0, 0, 1, 1, 0, 1),
  nrow = 3,  
  ncol = 3,        
  byrow = TRUE         
)
rownames(t1) <- c("a","b", "c")
colnames(t1) <- c("a","b", "c")

#second matrix
t2 = matrix(
  c(1, 1, 0, 0, 0, 1, 0, 0, 1),
  nrow = 3,  
  ncol = 3,        
  byrow = TRUE         
)
rownames(t2) <- c("a","c", "d") 
colnames(t2) <- c("a","c", "d")
#Expected outcome for the two matrices:
#first matrix
  a  b  c  d
a 1  0  1  NA
b 0  0  1  NA
c 1  0  1  NA
d NA NA NA NA

#second matrix
  a  b  c  d
a 1  NA 1  0
b NA NA NA NA
c 0  NA 0  1
d 0  NA 0  1

How do I achieve this? Preferrably the outcome is a list that contains these two NA-padded matrices


Solution

  • If row and column names will always be the same in each of the matrices (no hardcoded row/column names here) :

    nam <- union(rownames(t1), rownames(t2))
    m <- array(dim=rep(length(nam), 2),
               dimnames=list(nam, nam))
    lapply(list(t1, t2), function(x) {
      m[rownames(x), colnames(x)] <- x
      m
    })
    
    # [[1]]
    #    a  b  c  d
    # a  1  0  1 NA
    # b  0  0  1 NA
    # c  1  0  1 NA
    # d NA NA NA NA
    # 
    # [[2]]
    #    a  b  c  d
    # a  1 NA  1  0
    # b NA NA NA NA
    # c  0 NA  0  1
    # d  0 NA  0  1