An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20050909/be555dcf/attachment.pl
how to do something like " subset(mat, ("col1">4 & "col2">4)) "
5 messages · Florence Combes, Peter Dalgaard, Andy Bunn +1 more
Florence Combes <fcombes at gmail.com> writes:
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 ?
Well, subset has methods for vectors and data frames, so what happens for matrices is basically that they get converted to vectors. I don't know what gave you the idea of quoting the names, but "colb">4 is TRUE because numbers sort before letters! Try something like as.matrix(subset(as.data.frame(mat),colb>4 & colc>4))
O__ ---- Peter Dalgaard ??ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
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
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
An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20050909/8bd65c52/attachment.pl