[R] search through a matrix

Kushantha Perera kushanthap at ambaresearch.com
Tue Apr 21 14:53:00 CEST 2009


row() gives the row number of each element of the matrix, similarly
col() gives the column number. By definition, diagonal elements are the
elements correspond to same row and column numbers. That's how it
works...

x <- matrix(1:12, 3, 4)
> x
     [,1] [,2] [,3] [,4]
[1,]   12    9    6    3
[2,]   11    8    5    2
[3,]   10    7    4    1
> row(x)
     [,1] [,2] [,3] [,4]
[1,]    1    1    1    1
[2,]    2    2    2    2
[3,]    3    3    3    3
> col(x)
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    1    2    3    4
[3,]    1    2    3    4


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of onyourmark
Sent: Tuesday, April 21, 2009 6:03 PM
To: r-help at r-project.org
Subject: Re: [R] search through a matrix


Thanks very much.

I don't really understand the row() function. I looked in the reference
but
I don't really get it. It says:
Description
Returns a matrix of integers indicating their row number in a
matrix-like
object, or a factor indicating the row labels. 

Usage
row(x, as.factor = FALSE)

Arguments
x a matrix-like object, that is one with a two-dimensional dim. 

I don't understand what row() does. 

And in the example in the documentation it says:
x <- matrix(1:12, 3, 4)
# extract the diagonal of a matrix
dx <- x[row(x) == col(x)]
dx
[1] 1 5 9

I thought the single square bracket notation accepts a pair separated by
a
comma but I don't see how 
row(x)==col(x) produces that?

Thanks again.


onyourmark wrote:
> 
> Hi. I have a 925 by 925 correlation matrix corM. I want to identify
all
> variables that have correlation greater than 0.9.  Can anyone suggest
an
> "R way" of doing this?
> 
> Thank you.
> 

-- 
View this message in context:
http://www.nabble.com/search-through-a-matrix-tp23153538p23155104.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
This e-mail may contain confidential and/or privileged i...{{dropped:10}}




More information about the R-help mailing list