[R] In which column and in which row a number is in a matrix

Charles Novaes de Santana charles.santana at gmail.com
Wed Jan 2 12:12:02 CET 2013


Dear all, Happy New Year for all of you! I hope we have an year of
essential freedom for everyone!

I am trying to manipulate a matrix in order to know in which column
and in which row a number is allocated. But, when we use the function
"which" it returns the position of the number in the "vector
representation of the matrix". For example:

> mata<-matrix(2:7,nrow=2,ncol=3)
> mata
     [,1] [,2] [,3]
[1,]    2    4    6
[2,]    3    5    7
> matb<-matrix(2:7,nrow=3,ncol=2)
> matb
     [,1] [,2]
[1,]    2    5
[2,]    3    6
[3,]    4    7
> which(mata==4)
[1] 3
> which(matb==4)
[1] 3

The function "which" returns the position "3" for both, mata and matb,
but If I didn't know that mata is a 2x3 matrix and matb is a 3x2, I
wouldn't know that the number 4 is at the column 2 and row 1 of the
mata and at the column 1 and row 3 of the matb. Do you know any way to
know the column and the row of a number in a matrix automatically?

Thanks, best regards,

Charles


-- 
Um axé! :)

--
Charles Novaes de Santana
http://www.imedea.uib-csic.es/~charles
PhD student - Global Change
Laboratorio Internacional de Cambio Global
Department of Global Change Research
Instituto Mediterráneo de Estudios Avanzados(CSIC/UIB)
Calle Miquel Marques 21, 07190
Esporles - Islas Baleares - España

Office phone - +34 971 610 896
Cell phone - +34 660 207 940




More information about the R-help mailing list