I still remember my public spanking from Ben Bolker on the unnecessary use of "which" in this instance.

> MM <- matrix(c(1:10,sample(-10:10,10)),nrow=10)
> MM
      [,1] [,2]
 [1,]    1   -1
 [2,]    2    5
 [3,]    3   -2
 [4,]    4   -3
 [5,]    5    0
 [6,]    6    7
 [7,]    7   -9
 [8,]    8    1
 [9,]    9    6
[10,]   10    4

> MM[MM[,2]<0, ]
     [,1] [,2]
[1,]    1   -1
[2,]    3   -2
[3,]    4   -3
[4,]    7   -9

> MM[MM[,2]>0, ]
     [,1] [,2]
[1,]    2    5
[2,]    6    7
[3,]    8    1
[4,]    9    6
[5,]   10    4

--
David Winsemius
On Feb 9, 2009, at 9:46 AM, Ian Fiske wrote:


If your matrix is called mat, how about

mat[which(mat[,2] > 0), ]


mat[which(mat[,2] < 0), ]


-Ian


mentor_ wrote:

Hi,

I have a matrix with negative and positiv values.
How can I get either the negative or positive values from the matrix?

Matrix:
    [,1] [,2]
[1,]    1   -3
[2,]    2   -2
[3,]    3    5
[4,]    4   -2
[5,]    5    9
[6,]    6    8
[7,]    7   -2

What I want to have is:

    [,1] [,2]
[1,]    1   -3
[2,]    2   -2
[3,]    4   -2
[4,]    7   -2

and:

    [,1] [,2]
[1,]    3    5
[2,]    5    9
[3,]    6    8


Regards


--
View this message in context: 
http://www.nabble.com/Return-values-%3C-0-from-Matrix-tp21913440p21914262.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@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.

______________________________________________
R-help@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.

Reply via email to