drmariod - 1 year ago 88

R Question

I have a maybe simple problem but do not find an easy and comprehensive way to do it.

I have a substitution matrix and I want to check if a specific (or almost equal) value is in my substation matrix. If I would search for the exact value, I could use

`==`

`all.equal`

`a <- matrix(c(0.00000, -85.063997, -43.005813,`

85.06400, 0.000000, 42.058184,

43.00581, -42.058184, 0.000000),3,byrow=TRUE)

85.06400 == a

all.equal(85, a)

I would love to just get the column and row index or names back.

Thanks for any help or hint.

Answer Source

There are some details (like whether you want to measure absolute or relative error), but `abs(x-y)<eps`

is a start: then you can use `which(...,arr.ind=TRUE)`

to get row/column indices.

```
a <- matrix(c(0.00000, -85.063997, -43.005813,
85.06400, 0.000000, 42.058184,
43.00581, -42.058184, 0.000000),3,byrow=TRUE)
target <- 85.06400
which(abs(a-target)<.Machine$double.eps,arr.ind=TRUE)
## row col
## [1,] 2 1
```