[R] How do I get the row indices?

Huntsinger, Reid reid_huntsinger at merck.com
Fri Sep 16 19:50:26 CEST 2005


You can use "which" on your subscript vector.

> which(iris[,1] == 6.2)
[1]  69  98 127 149

Reid Huntsinger

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] 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 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