[R] how to do something like " subset(mat, ("col1">4 & "col2">4)) "
Andy Bunn
abunn at whrc.org
Fri Sep 9 16:26:25 CEST 2005
Some thing like this?
mat <- matrix(1:9,3,3)
mat
mat[apply(mat[,2:3] > 4,1,all),]
# or less cryptically
foo <- mat[,2:3] > 4
bar <- apply(foo,1,all)
mat[bar,]
HTH, Andy
NB: No need to send this kind of message to r-devel
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Florence Combes
> Sent: Friday, September 09, 2005 10:09 AM
> To: r-help at stat.math.ethz.ch; r-devel-request at stat.math.ethz.ch
> Subject: [R] how to do something like " subset(mat, ("col1">4 &
> "col2">4)) "
>
>
> Dear all,
>
> I have a problem with the "subset()" function. I spent all day yesterday
> with a collegue to solve it and we did not find a satisfying
> solution (even
> in the archived mails), so I ask for your help.
> Let's say (for a simple example) a matrix mat:
>
> R> mat
> cola colb colc
> [1,] 1 4 7
> [2,] 2 5 8
> [3,] 3 6 9
>
> My goal is to select the lines of the matrix on the basis of the
> values of
> more than one column (let's say colb and colc).
> For example I want to select all the lines of the matrix for
> which values in
> colb and colc are more than 4.
>
> I tried several ways that did not work:
>
> R> mat2 <- subset(mat, ("colb">4 & "colc">4))
> R> mat2
> [1] 1 2 3 4 5 6 7 8 9
>
> it is a vector, not a matrix.
>
> > mat2 <- subset(mat, mat[,2:3]>4)
> > mat2
> [1] 2 3 4 5 6 8 9
>
> tha same: it is a vector; so I tried:
>
> > mat2 <- as.matrix(subset(mat, mat[,("colb">4 & "colc">4)]))
> > mat2
> [,1]
> [1,] 1
> [2,] 2
> [3,] 3
> [4,] 4
> [5,] 5
> [6,] 6
> [7,] 7
> [8,] 8
> [9,] 9
>
> not good :(
>
> Did someone have an idea of how to select the only the lines 2
> and 3 of mat
> by a selection on "colb" and "colc" >4 ?
>
> Thanks a lot,
>
> Florence.
>
> Version 2.0.1 (2004-11-15)
> (Linux Debian).
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
More information about the R-help
mailing list