banbh - 1 year ago 87

R Question

Suppose you have a matrix (

`X`

`src`

`X`

`src`

`X`

`src`

`src`

`X`

`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

Answer Source

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