banbh
banbh

Reputation: 1525

How to pad a matrix with named rows and columns?

Suppose you have a matrix (X) and a second matrix (src) such that rownames of X are a subset of the rownames of src and the colnames of X are a subset of the colnames of src. What is the best way to create a matrix with the rows and columns of src, the data of X, and with missing data filled in with some default value (such as zero or NA)?

Below is my attempt, but I feel there must be a standard way of doing this in R.

# Assume row and columns are named
PadColumns <- function(x, src, fill = NA) {
  # pad columns with default values
  result <- matrix(fill, nrow = nrow(x), ncol = ncol(src))
  colnames(result) <- colnames(src)
  rownames(result) <- rownames(x)
  result[,match(colnames(x), colnames(src))] <- x
  result
}

PadRows <- function(x, src, fill = NA) {
  # pad rows with default values
  result <- matrix(fill, nrow = nrow(src), ncol = ncol(x))
  colnames(result) <- colnames(x)
  rownames(result) <- rownames(src)
  result[match(rownames(x), rownames(src)),] <- x
  result
}

PadRowsColumns <- function(x, src, fill = NA) {
  PadColumns(PadRows(x, src, fill = fill), src, fill = fill)
}

For example, given

X <- matrix(1:6, nrow = 2, dimnames = list(letters[2 * 1:2], LETTERS[2 * 1:3]))
src <- matrix(0, nrow = 4, ncol = 6, dimnames = list(letters[1:4], LETTERS[1:6]))

then we should get this:

> X
  B D F
b 1 3 5
d 2 4 6
> PadRowsColumns(X, src)
   A  B  C  D  E  F
a NA NA NA NA NA NA
b NA  1 NA  3 NA  5
c NA NA NA NA NA NA
d NA  2 NA  4 NA  6

Upvotes: 1

Views: 1200

Answers (1)

Kota Mori
Kota Mori

Reputation: 6750

You can subset matrices by row and column names.

src <- matrix(1, nrow = 3, ncol = 4, 
              dimnames= list(c("a", "b", "c"), c("A", "B", "C", "D")))
X <- matrix(1:6, nrow = 2, ncol = 3,
            dimnames = list(c("b", "c"), c("A", "C", "D")))
Y <- src
Y[] <- NA
Y[rownames(X), colnames(X)] <- X

Upvotes: 3

Related Questions