Hello All,
I would like to combine two data matrices. One is the reference matrix, and the second is the subset of the reference matrix. I want the reference matrix to contain all values of the subset matrix only and the rest of the matrix to be zero.
For example, I have seen a few solutions in the link below.
https://stackoverflow.com/questions/55735530/join-two-adjacency-matrices-and-retain-values
However, I need some one to suggest to me if there is another elegant way to do it.
The codes are below.
best,
ADR
ref = structure(c(0L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L,0L,0L,0L, 0L, 0L, 1L,0L,0L,1L,0L,0L,
1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L), .Dim = c(6L, 6L),
.Dimnames = list(c("Mike", "Roy", "Addy", "Sam", "Dan","Tommy"),
c("Mike", "Roy", "Addy", "Sam", "Dan","Tommy")))
mat = ref[1:3,1:3]
complete_matrix <- function(mat, ref) {
dif <- setdiff(rownames(ref), rownames(mat))
mat <- rbind(mat, matrix(0, length(dif), ncol(mat), dimnames = list(dif, NULL)))
mat <- cbind(mat, matrix(0, nrow(mat), length(dif), dimnames = list(NULL, dif)))
return(mat)
}
complete_matrix(mat, ref)