# [R] identify extremes positions of values into matrix

Francisco J. Zagmutt gerifalte28 at hotmail.com
Fri Nov 3 19:48:57 CET 2006

```Oi Milton,

Is this what you are after?

>x
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1  0  0  0  0  0  0  0  0  0   0
2  0  0  0  0  0  1  1  1  0   0
3  0  0  0  0  0  1  1  1  0   0
4  0  0  0  0  0  0  0  0  0   0
5  0  0  2  2  2  0  0  0  0   0
6  0  0  0  2  2  2  0  0  0   0
7  0  0  2  2  2  2  2  2  0   0
8  0  0  0  0  0  0  0  0  0   0

idx=which(x==2,arr.ind=T) #Index of observations with values == 2 .  Notice
the use of arr.ind=t to get the rows and columns of the observations

>idx
row col
5   5   3
7   7   3
5   5   4
6   6   4
7   7   4
5   5   5
6   6   5
7   7   5
6   6   6
7   7   6
7   7   7
7   7   8

>range(idx[,"row"]) #gives you the min and max of the rows with x==2
[1] 5 7
>range(idx[,"col"])#gives you the min and max of the columns with x==2
[1] 3 8

You can refine this and wrap it in a function to make reports in a more
suitable form

Regards

Francisco

Dr. Francisco J. Zagmutt
College of Veterinary Medicine and Biomedical Sciences

>From: Milton Cezar Ribeiro <milton_ruser at yahoo.com.br>
>To: r-help at stat.math.ethz.ch
>Subject: [R] identify extremes positions of values into matrix
>Date: Fri, 3 Nov 2006 11:25:32 -0300 (ART)
>
>Hi R-friends around world!
>   I have a matrix that looks like bellow one.
>Each cell are filled with values 0, 1 or 2.
>In fact I have a lot of 512x512 matrices, with
>values varying from 0 to 999.
>   I need to retrieve the left and right columns
>and the top and botton lines of the matrix
>where occours for example the value 2.
>Column and row labels are included at the example.
>
>     1234567890
>1 0000000000
>2 0000011100
>3 0000011100
>4 0000000000
>5 0022200000
>6 0002220000
>7 0022222200
>8 0000000000
>
>   At this example, the right answer are:
>   collumns 3 and 8
>   lines 5 and 7
>   Any idea of how can I solve this?
>   Kind regards,
>   Miltinho
>
>
>---------------------------------
>
>  Música para ver e ouvir: You're Beautiful, do James Blunt
>	[[alternative HTML version deleted]]
>

>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help