I have a matrix of elements with numeric values, e.g.:
> mtx <- as.matrix(read.csv("matrix.csv", header = TRUE, row.names = 1,sep = ","))
> head(mtx)
BE BG EE ES FI
BE 0.00 0.75 0.17 0.28 0.48
BG 0.75 0.00 0.92 1.03 1.24
EE 0.17 0.92 0.00 0.11 0.31
ES 0.28 1.03 0.11 0.00 0.20
I want to replace values in certain elements with zero based on names of columns and rows. Namely, I want to have zero when column "BE" intersects with row "ES" and vice versa, i.e. when column "ES" intersects with row "BE", so I want to get:
BE BG EE ES FI
BE 0.00 0.75 0.17 0.00 0.00
BG 0.75 0.00 0.92 1.03 1.24
EE 0.17 0.92 0.00 0.11 0.31
ES 0.00 1.03 0.11 0.00 0.00
I have to perform this operation for many matrices, which are larger than this example, so there are >150 pairs for which replacement is needed. Here's data structure. Replacement needed for:
c('BE', 'FI', 'FR', 'DE', 'IE', 'NL', 'NO', 'SE', 'CH', 'GB', 'DK','PT','ES')
I have found function replace
, but it works apparently only based on criteria applied to the values, not to the names of rows and columns of elements that contain them...
mtx[c("BE", "ES"), c("ES", "BE")] <- 0
– akrunmtx[rbind(c("BE", "ES"), c("ES", "BE"))] <- 0
. Take a look at?"["
, it's worth reading two or three times. – lmodput
on mtx and paste the results into your question. Second, you should include the additional information either by mentioning it in the text of your question and perhaps by adding a second pair in your example. Look into?ncomb
. This may be helpful. Alsoexpand.grid
. It is not clear what your data structure for your hundreds of pairs is, so you should also provide this in your question. – lmo