I have two matrices in R, a square matrix, and a non-square matrix:
dataMatrix1, a 5x5 matrix:
1 2 3 4 3
2 1 3 5 7
3 3 1 1 8
4 5 1 1 5
1 7 8 5 1
dataMatrix2, a 5x8 matrix:
1 0.2 0.3 0.4 0.3 0.9 0.6 0.5
0.2 1 0.3 0.5 0.7 0.8 0.6 0.1
0.3 0.3 1 0.1 0.8 0.3 0.1 0.6
0.4 0.5 0.1 1 0.5 0.3 0.1 0.7
0.1 0.7 0.8 0.5 1 0.5 0.9 0.9
I want to replace the lower triangular part of the first 5x5 elements of dataMatrix2 with the lower triangular of dataMatrix1, so I would get the following matrix:
1 0.2 0.3 0.4 0.3 0.9 0.6 0.5
2 1 0.3 0.5 0.7 0.8 0.6 0.1
3 3 1 0.1 0.8 0.3 0.1 0.6
4 5 1 1 0.5 0.3 0.1 0.7
1 7 8 5 1 0.5 0.9 0.9
I just made up the values of these matrices to use as an example, I have to do this for a couple of matrices. Is there a systematic way to achieve this?
data
m1 <-"
1 2 3 4 3
2 1 3 5 7
3 3 1 1 8
4 5 1 1 5
1 7 8 5 1"
m1 <- as.matrix(read.table(text=m1,h=F))
m2 <-"
1 0.2 0.3 0.4 0.3 0.9 0.6 0.5
0.2 1 0.3 0.5 0.7 0.8 0.6 0.1
0.3 0.3 1 0.1 0.8 0.3 0.1 0.6
0.4 0.5 0.1 1 0.5 0.3 0.1 0.7
0.1 0.7 0.8 0.5 1 0.5 0.9 0.9"
m2 <- as.matrix(read.table(text=m2,h=F))