I am trying to compare two 5x5 matrices based on row and column names. Note that in matrix b the names for row 2 and row 4 have been swapped.
a <- matrix(1:25, nrow = 5)
colnames(a) = c("col1", "col2", "col3", "col4", "col5")
rownames(a) = c("row1", "row2", "row3", "row4", "row5")
a
b <- matrix(1:25, nrow = 5)
colnames(b) = c("col1", "col2", "col3", "col4", "col5")
rownames(b) = c("row1", "row4", "row3", "row2", "row5")
b
c <- b-a
c
col1 col2 col3 col4 col5
row1 0 0 0 0 0
row4 0 0 0 0 0
row3 0 0 0 0 0
row2 0 0 0 0 0
row5 0 0 0 0 0
When calculating the difference c = b - a, the code returns a matrix of zeros as if it was looking for the position of the elements in the matrix rather that the row and column index. I'd expect the result for row 4 to be:
d <- a["row4",] - b["row4",]
col1 col2 col3 col4 col5
2 2 2 2 2
Any idea how to do this?