[R] How do I get the row indices?
Hi, I was wondering if it's possible to get the row numbers from a filtering. Here's an example: # give me the rows with sepal.length == 6.2 iris[(iris[,1]==6.2),] # output Sepal.Length Sepal.Width Petal.Length Petal.Width Species 69 6.2 2.2 4.5 1.5 versicolor 98 6.2 2.9 4.3 1.3 versicolor 127 6.2 2.8 4.8 1.8 virginica 149 6.2 3.4 5.4 2.3 virginica What I really want is that it return the row numbers: 69, 98, 127, 149. Thanks in advance, Martin __ R-help@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
Re: [R] How do I get the row indices?
have a look at which() hih __ R-help@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
Re: [R] How do I get the row indices?
On Fri, 16 Sep 2005 10:34:57 -0700 (PDT) Martin Lam wrote: Hi, I was wondering if it's possible to get the row numbers from a filtering. Here's an example: # give me the rows with sepal.length == 6.2 iris[(iris[,1]==6.2),] # output Sepal.Length Sepal.Width Petal.Length Petal.Width Species 69 6.2 2.2 4.5 1.5 versicolor 98 6.2 2.9 4.3 1.3 versicolor 127 6.2 2.8 4.8 1.8 virginica 149 6.2 3.4 5.4 2.3 virginica What I really want is that it return the row numbers: 69, 98, 127, 149. R which(iris[,1]==6.2) [1] 69 98 127 149 hth, Z Thanks in advance, Martin __ R-help@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 __ R-help@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
Re: [R] How do I get the row indices?
On Fri, 2005-09-16 at 10:34 -0700, Martin Lam wrote: Hi, I was wondering if it's possible to get the row numbers from a filtering. Here's an example: # give me the rows with sepal.length == 6.2 iris[(iris[,1]==6.2),] # output Sepal.Length Sepal.Width Petal.Length Petal.Width Species 69 6.2 2.2 4.5 1.5 versicolor 98 6.2 2.9 4.3 1.3 versicolor 127 6.2 2.8 4.8 1.8 virginica 149 6.2 3.4 5.4 2.3 virginica What I really want is that it return the row numbers: 69, 98, 127, 149. Thanks in advance, Martin ?which, as in: which(iris[ , 1] == 6.2) [1] 69 98 127 149 HTH G __ R-help@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 -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [T] +44 (0)20 7679 5522 ENSIS Research Fellow [F] +44 (0)20 7679 7565 ENSIS Ltd. ECRC [E] gavin.simpsonATNOSPAMucl.ac.uk UCL Department of Geography [W] http://www.ucl.ac.uk/~ucfagls/cv/ 26 Bedford Way[W] http://www.ucl.ac.uk/~ucfagls/ London. WC1H 0AP. %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ R-help@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
Re: [R] How do I get the row indices?
?row row(iris)[iris[,1]==6.2] ##or better yet ?which which(iris[,1]==6.2) Note also that both may fail because the test may not be numerically exact -- you may need to add fuzz. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA The business of the statistician is to catalyze the scientific learning process. - George E. P. Box -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Martin Lam Sent: Friday, September 16, 2005 10:35 AM To: R Subject: [R] How do I get the row indices? Hi, I was wondering if it's possible to get the row numbers from a filtering. Here's an example: # give me the rows with sepal.length == 6.2 iris[(iris[,1]==6.2),] # output Sepal.Length Sepal.Width Petal.Length Petal.Width Species 69 6.2 2.2 4.5 1.5 versicolor 98 6.2 2.9 4.3 1.3 versicolor 127 6.2 2.8 4.8 1.8 virginica 149 6.2 3.4 5.4 2.3 virginica What I really want is that it return the row numbers: 69, 98, 127, 149. Thanks in advance, Martin __ R-help@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 __ R-help@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
Re: [R] How do I get the row indices?
You can use which on your subscript vector. which(iris[,1] == 6.2) [1] 69 98 127 149 Reid Huntsinger -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Martin Lam Sent: Friday, September 16, 2005 1:35 PM To: R Subject: [R] How do I get the row indices? Hi, I was wondering if it's possible to get the row numbers from a filtering. Here's an example: # give me the rows with sepal.length == 6.2 iris[(iris[,1]==6.2),] # output Sepal.Length Sepal.Width Petal.Length Petal.Width Species 69 6.2 2.2 4.5 1.5 versicolor 98 6.2 2.9 4.3 1.3 versicolor 127 6.2 2.8 4.8 1.8 virginica 149 6.2 3.4 5.4 2.3 virginica What I really want is that it return the row numbers: 69, 98, 127, 149. Thanks in advance, Martin __ R-help@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 __ R-help@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
Re: [R] How do I get the row indices?
On Fri, 2005-09-16 at 10:34 -0700, Martin Lam wrote: Hi, I was wondering if it's possible to get the row numbers from a filtering. Here's an example: # give me the rows with sepal.length == 6.2 iris[(iris[,1]==6.2),] # output Sepal.Length Sepal.Width Petal.Length Petal.Width Species 69 6.2 2.2 4.5 1.5 versicolor 98 6.2 2.9 4.3 1.3 versicolor 127 6.2 2.8 4.8 1.8 virginica 149 6.2 3.4 5.4 2.3 virginica What I really want is that it return the row numbers: 69, 98, 127, 149. Thanks in advance, Martin Martin, First: Be very, very careful when performing exact equalities on floating point numbers. They won't always result in the answer you expect. For more information see R FAQ 7.31: Why doesn't R think these numbers are equal? Second: See ?all.equal, ?sapply and ?which. Here is a possible vectorized solution: which(sapply(iris[, 1], function(x) isTRUE(all.equal(x, 6.2 [1] 69 98 127 149 The above applies isTRUE(all.equal(x, 6.2)) for each element 'x' in iris[, 1], returning the indices of the TRUE results for the near equality comparison, based upon the tolerance argument in all.equal(). HTH, Marc Schwartz __ R-help@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