[R] how to do something like " subset(mat, ("col1">4 & "col2">4)) "

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Fri Sep 9 16:31:12 CEST 2005


maybe something like this:

subset(mat, mat[, 2] > 4 & mat[, 3] > 4)


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Florence Combes" <fcombes at gmail.com>
To: <r-help at stat.math.ethz.ch>; <r-devel-request at stat.math.ethz.ch>
Sent: Friday, September 09, 2005 4:09 PM
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
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm




More information about the R-help mailing list