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.
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. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
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: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Akkineni,Vasundhara
Sent: Sunday, March 26, 2006 11:49 PM
To: r-help at stat.math.ethz.ch
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.
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
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: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Akkineni,Vasundhara
Sent: Sunday, March 26, 2006 11:49 PM
To: r-help at stat.math.ethz.ch
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.
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
Try this: which(xx[,1] > 0 & xx[,2] > 0) On 3/26/06, Akkineni,Vasundhara <vasu.akkineni at louisville.edu> 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. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
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 <vasu.akkineni at louisville.edu> 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. >> >>______________________________________________ >>R-help at stat.math.ethz.ch mailing list >>https://stat.ethz.ch/mailman/listinfo/r-help >>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >> > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html