conditional selection: extracting row from matrix
3
0
Entering edit mode
@straubhaar-juerg-391
Last seen 10.3 years ago
I have an (n,m) matrix with rownames and colnames. The rownames are probe set ids. I know a value of one of the probe sets which is, say, in column 2 of the matrix. I would like to retrieve the rowname, or probe set id, of the row which contains this value. Neither of these work: matrix[,matrix[,2]==513.193057] (Error: (subscript) logical subscript too long) index <- which(matrix[,2] == 513.193057) matrix[index,] Of course, I could write.table() the matrix and search the file. But I would like to know how this is done in R/Bioconductor. Thank you very much. Juerg Straubhaar, Umass Med
probe probe • 922 views
ADD COMMENT
0
Entering edit mode
John Zhang ★ 2.9k
@john-zhang-6
Last seen 10.3 years ago
>I have an (n,m) matrix with rownames and colnames. The rownames are probe set ids. I know a value of one of the probe sets which is, say, in column 2 of the matrix. I would like to retrieve the rowname, or probe set id, of the row which contains this value. > >Neither of these work: > >matrix[,matrix[,2]==513.193057] >(Error: (subscript) logical subscript too long) Try this: > tt <- matrix(1:20, ncol = 4) > tt [,1] [,2] [,3] [,4] [1,] 1 6 11 16 [2,] 2 7 12 17 [3,] 3 8 13 18 [4,] 4 9 14 19 [5,] 5 10 15 20 > tt[tt[,2] == 8, ] [1] 3 8 13 18 > >index <- which(matrix[,2] == 513.193057) >matrix[index,] > >Of course, I could write.table() the matrix and search the file. But I would like to know how this is done in R/Bioconductor. > >Thank you very much. > >Juerg Straubhaar, >Umass Med > >_______________________________________________ >Bioconductor mailing list >Bioconductor@stat.math.ethz.ch >https://www.stat.math.ethz.ch/mailman/listinfo/bioconductor Jianhua Zhang Department of Biostatistics Dana-Farber Cancer Institute 44 Binney Street Boston, MA 02115-6084
ADD COMMENT
0
Entering edit mode
@wolfgang-huber-3550
Last seen 3 months ago
EMBL European Molecular Biology Laborat…
Hi, > matrix[,matrix[,2]==513.193057] > (Error: (subscript) logical subscript too long) matrix[,2]==513.193057 returns a logical vector the length of which is the number of rows of 'matrix'. Thus you would want to write matrix[matrix[,2]==513.193057, ] > index <- which(matrix[,2] == 513.193057) > matrix[index,] Note that all of the above may be problematic because of round-off errors, most likely this expression will not match for any row of 'matrix'. Please read the help pages on the == operator and the all.equal function (type: > help("==") > help(all.equal) Maybe matrix[abs(matrix[,2]-513.19 < 0.01), ] would be the simplest solution. Best wishes Wolfgang
ADD COMMENT
0
Entering edit mode
@wolfgang-huber-3550
Last seen 3 months ago
EMBL European Molecular Biology Laborat…
Hi, for the record: Laurent pointed out a typo in my code example from the previous post - Please see below. Best wishes Wolfgang On Tue, 6 Jan 2004, Laurent Gautier wrote: > > Maybe > > matrix[abs(matrix[,2]-513.19 < 0.01), ] > > would be the simplest solution. > > Did you mean > matrix[abs(matrix[,2]-513.19) < 0.01, ] > ?
ADD COMMENT

Login before adding your answer.

Traffic: 593 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6