R: How to combine / attach 2 matrices of different sizes (nrow / ncol)?

I want to combine / join two matrices, a small one with values ​​should correspond to a larger one by name / colnames. I find this answer. However, I cannot match the locations, since in my case the code line is frn <- as.matrix(bigMatrix[[1]])not working. The answers of internal, external ... join did not help here , since I want to combine / combine many different columns (and not, for example, CostumerID for X and CustomerID for y).

As matrices, I use 126x104 and 193x193 matrices. I prepared data examples: 1. a large matrix, into which a smaller one should be added (letters are indicated in the names of the source data):

a = c("A", "B", "C", "D", "E", "F")
full_matrix = matrix(nrow = length(a), ncol=length(a))
dimnames(full_matrix) <- list(levels(as.factor(a)), levels(as.factor(a)))

full_matrix
   A  B  C  D  E  F
A NA NA NA NA NA NA
B NA NA NA NA NA NA
C NA NA NA NA NA NA
D NA NA NA NA NA NA
E NA NA NA NA NA NA
F NA NA NA NA NA NA

And a smaller matrix:

matrix = matrix(c(2, 4, 3, 1, 5, 7, 3, 1, 6), nrow=3, ncol=3)
dimnames(matrix) <- list(c("B","C","E"), c("A","B","F"))

matrix
  A B F
B 2 1 3
C 4 5 1
E 3 7 6

The result should look like this:

   A  B  C  D  E  F
A NA NA NA NA NA NA
B  2  1 NA NA NA  3
C  4  5 NA NA NA  1
D NA NA NA NA NA NA
E  3  7 NA NA NA  6
F NA NA NA NA NA NA
+4
2

. , R ( ):

a = c("A", "B", "C", "D", "E", "F")
full_matrix = matrix(nrow = length(a), ncol=length(a))
dimnames(full_matrix) <- list(levels(as.factor(a)), levels(as.factor(a)))

small_matrix = matrix(c(2, 4, 3, 1, 5, 7, 3, 1, 6), nrow=3, ncol=3)
dimnames(small_matrix) <- list(as.factor(c("B","C","E")), as.factor(c("A","B","F")))

:

rowmatch <- match(rownames(small_matrix), rownames(full_matrix))
colmatch <- match(colnames(small_matrix), colnames(full_matrix))
full_matrix[rowmatch, colmatch] <- small_matrix

:

   A  B  C  D  E  F
A NA NA NA NA NA NA
B  2  1 NA NA NA  3
C  4  5 NA NA NA  1
D NA NA NA NA NA NA
E  3  7 NA NA NA  6
F NA NA NA NA NA NA

, , %in% :

full_matrix[rownames(full_matrix) %in% rownames(small_matrix), 
            colnames(full_matrix) %in% colnames(small_matrix)] <- small_matrix
+3

1) as.data.frame.table L Var1, Var2 Freq, subscript :

L <- as.data.frame.table(matrix)
full_matrix[as.matrix(L[1:2])] <- L$Freq

:

> full_matrix
   A  B  C  D  E  F
A NA NA NA NA NA NA
B  2  1 NA NA NA  3
C  4  5 NA NA NA  1
D NA NA NA NA NA NA
E  3  7 NA NA NA  6
F NA NA NA NA NA NA

2)

full_matrix[rownames(matrix), colnames(matrix)] <- matrix

.. :

a = c("A", "B", "C", "D", "E", "F")
full_matrix = matrix(nrow = length(a), ncol=length(a))
dimnames(full_matrix) <- list(levels(as.factor(a)), levels(as.factor(a)))
matrix = matrix(c(2, 4, 3, 1, 5, 7, 3, 1, 6), nrow=3, ncol=3)
dimnames(matrix) <- list(c("B","C","E"), c("A","B","F"))

fm1 <- full_matrix
L <- as.data.frame.table(matrix)
fm1[as.matrix(L[1:2])] <- L$Freq

fm2 <- full_matrix
fm2[rownames(matrix), colnames(matrix)] <- matrix

identical(fm1, fm2)
## [1] TRUE
+6

All Articles