Вы можете использовать rownames
и colnames
для подмножества matrix2
и обновлять только его часть.
matrix2[rownames(matrix1), colnames(matrix1)] <- (matrix1 + matrix2[rownames(matrix1), colnames(matrix1)])/2
matrix2
# A B C D
#x 3.5 4.0 2.0 2
#y 2.0 3.0 1.0 3
#z 3.0 3.0 2.5 4
#k 4.0 3.5 3.0 1
данные
matrix1 <- structure(c(1L, 5L, 1L, 4L, 2L, 2L, 3L, 4L, 3L), .Dim = c(3L,
3L), .Dimnames = list(c("x", "z", "k"), c("A", "B", "C")))
matrix2 <- structure(c(6L, 2L, 1L, 7L, 4L, 3L, 4L, 5L, 1L, 1L, 1L, 3L, 2L,
3L, 4L, 1L), .Dim = c(4L, 4L), .Dimnames = list(c("x", "y", "z",
"k"), c("A", "B", "C", "D")))