Reputation: 965
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
Upvotes: 2
Views: 193
Reputation: 4725
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
Upvotes: 2
Reputation: 41245
You can use match
to find the matching columns and rows otherwise it will add a column or row with NA's. Using lapply
to perform the operations on each matrix to generate the outcome as a list. You can use the following code:
l <- list(t1, t2)
lapply(l, \(x) {
# Your required columns and rows names
colrown=c("a","b","c","d")
# columns part
coln=colnames(x)
x=x[,match(colrown,coln)]
colnames(x)=colrown
# Rows part
rown=rownames(x)
x=x[match(colrown,rown),]
rownames(x)=colrown
return(x)
})
#> [[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
Created on 2022-09-24 with reprex v2.0.2
Data
#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")
t1
#> a b c
#> a 1 0 1
#> b 0 0 1
#> c 1 0 1
t2
#> a c d
#> a 1 1 0
#> c 0 0 1
#> d 0 0 1
Upvotes: 1