我想根据列名和行名合并两个矩阵。
两个矩阵中的值都是数字,并且合并是对同时出现的两个矩阵求平均值。
矩阵1:
A B C
x 1 4 3
z 5 2 4
k 1 2 3
和matrix2:
A B C D
x 6 4 1 2
y 2 3 1 3
z 1 4 1 4
k 7 5 3 1
所以输出将是:
A B C D
x 3.5 4 2 2
y 2 3 1 3
z 3 3 2.5 4
k 4 3.5 3 1
My idea is to use for loop or apply
function, but if the matrices are big, then this program will run for a long time. Any advice? Thank you!
You can use
rownames
andcolnames
to subsetmatrix2
and update only part of it.数据