R - min, max and mean of off-diagonal elements in a matrix

amhemad ahmad picture amhemad ahmad · Oct 24, 2012 · Viewed 19.3k times · Source

I have like a matrix in R and I want to get:

Max off - diagonal elements
Min off – diagonal elements
Mean off –diagonal elements

With diagonal I used max(diag(A)) , min(diag(A)) , mean(diag(A)) and worked just fine

But for off-diagonal I tried

dataD <- subset(A, V1!=V2)

Error in subset.matrix(A, V1 != V2) : object 'V1' not found

to use:

colMeans(dataD) # get the mean for columns

but I cannot get dataD b/c it says object 'V1' not found

Thanks!

Answer

Gavin Simpson picture Gavin Simpson · Oct 24, 2012

Here the row() and col() helper functions are useful. Using @James A, we can get the upper off-diagonal using this little trick:

> A[row(A) == (col(A) - 1)]
[1]  5 10 15

and the lower off diagonal via this:

> A[row(A) == (col(A) + 1)]
[1]  2  7 12

These can be generalised to give whatever diagonals you want:

> A[row(A) == (col(A) - 2)]
[1]  9 14

and don't require any subsetting.

Then it is a simple matter of calling whatever function you want on these values. E.g.:

> mean(A[row(A) == (col(A) - 1)])
[1] 10

If as per my comment you mean everything but the diagonal, then use

> diag(A) <- NA
> mean(A, na.rm = TRUE)
[1] 8.5
> max(A, na.rm = TRUE)
[1] 15
> # etc. using sum(A, na.rm = TRUE), min(A, na.rm = TRUE), etc..

So this doesn't get lost, Ben Bolker suggests (in the comments) that the above code block can be done more neatly using the row() and col() functions I mentioned above:

mean(A[row(A)!=col(A)])
min(A[row(A)!=col(A)])
max(A[row(A)!=col(A)])
sum(A[row(A)!=col(A)])

which is a nicer solution all round.