Matrix elements

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Matrix elements

Akkineni,Vasundhara
Hello all,

I have a matrix object, xx given as below:

             [,1]         [,2]
 [1,]  0.02237883  0.601572660
 [2,] -0.39880918  0.126498958
 [3,]  0.20269214  0.567953402
 [4,] -0.45750812 -0.031193600
 [5,] -0.30666134 -0.084819484
 [6,] -0.37718928  0.078675868
 [7,] -0.25432685 -0.005200316
 [8,] -0.22146224  0.168196438
 [9,] -0.32049919 -0.013216047
[10,]  0.94325749  1.341664313

How can i check for every row in the matrix that both the column elements are greater than zero, and if they are greater than zero then to print the row number. I tried using for loops, but it kept returning warnings. Please let me know how this can be done.

Thanks,
Vasu.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Matrix elements

Liaw, Andy
Something like this should work:

which(rowSums(xx > 0) == ncol(xx))

Andy

From: Akkineni,Vasundhara

>
> Hello all,
>
> I have a matrix object, xx given as below:
>
>              [,1]         [,2]
>  [1,]  0.02237883  0.601572660
>  [2,] -0.39880918  0.126498958
>  [3,]  0.20269214  0.567953402
>  [4,] -0.45750812 -0.031193600
>  [5,] -0.30666134 -0.084819484
>  [6,] -0.37718928  0.078675868
>  [7,] -0.25432685 -0.005200316
>  [8,] -0.22146224  0.168196438
>  [9,] -0.32049919 -0.013216047
> [10,]  0.94325749  1.341664313
>
> How can i check for every row in the matrix that both the
> column elements are greater than zero, and if they are
> greater than zero then to print the row number. I tried using
> for loops, but it kept returning warnings. Please let me know
> how this can be done.
>
> Thanks,
> Vasu.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Matrix elements

Christos Hatzis-2
In reply to this post by Akkineni,Vasundhara
Since both columns being positive implies that the row wise sum and row wise
product are both positive, you can try the following:

x <- matrix(rnorm(20),ncol=2)

x[apply(x, 1, FUN=function(x) ifelse(sum(x)>0 & prod(x)>0,TRUE,FALSE)),]

-Christos Hatzis

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Akkineni,Vasundhara
Sent: Sunday, March 26, 2006 11:49 PM
To: [hidden email]
Subject: [R] Matrix elements

Hello all,

I have a matrix object, xx given as below:

             [,1]         [,2]
 [1,]  0.02237883  0.601572660
 [2,] -0.39880918  0.126498958
 [3,]  0.20269214  0.567953402
 [4,] -0.45750812 -0.031193600
 [5,] -0.30666134 -0.084819484
 [6,] -0.37718928  0.078675868
 [7,] -0.25432685 -0.005200316
 [8,] -0.22146224  0.168196438
 [9,] -0.32049919 -0.013216047
[10,]  0.94325749  1.341664313

How can i check for every row in the matrix that both the column elements
are greater than zero, and if they are greater than zero then to print the
row number. I tried using for loops, but it kept returning warnings. Please
let me know how this can be done.

Thanks,
Vasu.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Matrix elements

Christos Hatzis-2
In reply to this post by Akkineni,Vasundhara
To follow up on my previous posting, if you just need the row numbers try
this instead:

row(x)[apply(x, 1, FUN=function(x) ifelse(sum(x)>0 &
prod(x)>0,TRUE,FALSE)),1]

-Christos

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Akkineni,Vasundhara
Sent: Sunday, March 26, 2006 11:49 PM
To: [hidden email]
Subject: [R] Matrix elements

Hello all,

I have a matrix object, xx given as below:

             [,1]         [,2]
 [1,]  0.02237883  0.601572660
 [2,] -0.39880918  0.126498958
 [3,]  0.20269214  0.567953402
 [4,] -0.45750812 -0.031193600
 [5,] -0.30666134 -0.084819484
 [6,] -0.37718928  0.078675868
 [7,] -0.25432685 -0.005200316
 [8,] -0.22146224  0.168196438
 [9,] -0.32049919 -0.013216047
[10,]  0.94325749  1.341664313

How can i check for every row in the matrix that both the column elements
are greater than zero, and if they are greater than zero then to print the
row number. I tried using for loops, but it kept returning warnings. Please
let me know how this can be done.

Thanks,
Vasu.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Matrix elements

Gabor Grothendieck
In reply to this post by Akkineni,Vasundhara
Try this:

which(xx[,1] > 0 & xx[,2] > 0)


On 3/26/06, Akkineni,Vasundhara <[hidden email]> wrote:

> Hello all,
>
> I have a matrix object, xx given as below:
>
>             [,1]         [,2]
>  [1,]  0.02237883  0.601572660
>  [2,] -0.39880918  0.126498958
>  [3,]  0.20269214  0.567953402
>  [4,] -0.45750812 -0.031193600
>  [5,] -0.30666134 -0.084819484
>  [6,] -0.37718928  0.078675868
>  [7,] -0.25432685 -0.005200316
>  [8,] -0.22146224  0.168196438
>  [9,] -0.32049919 -0.013216047
> [10,]  0.94325749  1.341664313
>
> How can i check for every row in the matrix that both the column elements are greater than zero, and if they are greater than zero then to print the row number. I tried using for loops, but it kept returning warnings. Please let me know how this can be done.
>
> Thanks,
> Vasu.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Matrix elements

Uwe Ligges
Gabor Grothendieck wrote:

> Try this:
>
> which(xx[,1] > 0 & xx[,2] > 0)
>


... or more generally for an arbitrary number of columns:

   which(apply(xx > 0, 1, all))

Uwe Ligges



> On 3/26/06, Akkineni,Vasundhara <[hidden email]> wrote:
>
>>Hello all,
>>
>>I have a matrix object, xx given as below:
>>
>>            [,1]         [,2]
>> [1,]  0.02237883  0.601572660
>> [2,] -0.39880918  0.126498958
>> [3,]  0.20269214  0.567953402
>> [4,] -0.45750812 -0.031193600
>> [5,] -0.30666134 -0.084819484
>> [6,] -0.37718928  0.078675868
>> [7,] -0.25432685 -0.005200316
>> [8,] -0.22146224  0.168196438
>> [9,] -0.32049919 -0.013216047
>>[10,]  0.94325749  1.341664313
>>
>>How can i check for every row in the matrix that both the column elements are greater than zero, and if they are greater than zero then to print the row number. I tried using for loops, but it kept returning warnings. Please let me know how this can be done.
>>
>>Thanks,
>>Vasu.
>>
>>______________________________________________
>>[hidden email] mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>>
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html