banbh - 1 year ago 109
R Question

# 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) {
}
``````

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
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
``````

``````src <- matrix(1, nrow = 3, ncol = 4,