Hi,
I was wondering if it is possible to get the
rowindices without using the function "which" because
I don't have a restriction criteria. Here's an example
of what I mean:
# take 10 randomly selected instances
iris[sample(1:nrow(iris), 10),]
# output
Sepal.Length Sepal.Width Petal.Length Petal.Width
Species
76 6.6 3.0 4.4 1.4
versicolor
105 6.5 3.0 5.8 2.2
virginica
131 7.4 2.8 6.1 1.9
virginica
79 6.0 2.9 4.5 1.5
versicolor
69 6.2 2.2 4.5 1.5
versicolor
42 4.5 2.3 1.3 0.3
setosa
25 4.8 3.4 1.9 0.2
setosa
129 6.4 2.8 5.6 2.1
virginica
60 5.2 2.7 3.9 1.4
versicolor
80 5.7 2.6 3.5 1.0
versicolor
What I want to get are their rownumbers: 76, 105, 131,
79, 69, 42, 25, 129, 60, 80.
Thanks in advance,
Martin
Dimitris Rizopoulos
2005-Sep-26 08:51 UTC
[R] How to get the rowindices without using which?
try this:
dat <- iris[sample(1:nrow(iris), 10), ]
dat
match(rownames(dat), rownames(iris))
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/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Martin Lam" <tmlammail at yahoo.com>
To: "R" <r-help at stat.math.ethz.ch>
Sent: Monday, September 26, 2005 10:37 AM
Subject: [R] How to get the rowindices without using which?
> Hi,
>
> I was wondering if it is possible to get the
> rowindices without using the function "which" because
> I don't have a restriction criteria. Here's an example
> of what I mean:
> # take 10 randomly selected instances
> iris[sample(1:nrow(iris), 10),]
>
> # output
> Sepal.Length Sepal.Width Petal.Length Petal.Width
> Species
> 76 6.6 3.0 4.4 1.4
> versicolor
> 105 6.5 3.0 5.8 2.2
> virginica
> 131 7.4 2.8 6.1 1.9
> virginica
> 79 6.0 2.9 4.5 1.5
> versicolor
> 69 6.2 2.2 4.5 1.5
> versicolor
> 42 4.5 2.3 1.3 0.3
> setosa
> 25 4.8 3.4 1.9 0.2
> setosa
> 129 6.4 2.8 5.6 2.1
> virginica
> 60 5.2 2.7 3.9 1.4
> versicolor
> 80 5.7 2.6 3.5 1.0
> versicolor
>
> What I want to get are their rownumbers: 76, 105, 131,
> 79, 69, 42, 25, 129, 60, 80.
>
> 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
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Martin Lam <tmlammail at yahoo.com> writes:> Hi, > > I was wondering if it is possible to get the > rowindices without using the function "which" because > I don't have a restriction criteria. Here's an example > of what I mean: > # take 10 randomly selected instances > iris[sample(1:nrow(iris), 10),] > > # output > Sepal.Length Sepal.Width Petal.Length Petal.Width > Species > 76 6.6 3.0 4.4 1.4 > versicolor > 105 6.5 3.0 5.8 2.2 > virginica > 131 7.4 2.8 6.1 1.9 > virginica > 79 6.0 2.9 4.5 1.5 > versicolor > 69 6.2 2.2 4.5 1.5 > versicolor > 42 4.5 2.3 1.3 0.3 > setosa > 25 4.8 3.4 1.9 0.2 > setosa > 129 6.4 2.8 5.6 2.1 > virginica > 60 5.2 2.7 3.9 1.4 > versicolor > 80 5.7 2.6 3.5 1.0 > versicolor > > What I want to get are their rownumbers: 76, 105, 131, > 79, 69, 42, 25, 129, 60, 80.Er, they are either the rownames of the result or the value that you used to index with: iris[nums <- sample(1:nrow(iris), 10),] leaves the numbers sitting in nums. (If the original data frame has rownames that are not 1:nrow(yourframe), then you have to decide whether it is names or numbers that you want.) -- 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
I'm not certain what you are asking. Consider the following: set.seed(1) (irows <- sample(1:nrow(iris), 10)) iris[irows,] If you want more than this, PLEASE do read the posting guide! "www.R-project.org/posting-guide.html". I believe that people who follow the posting guide generally get quicker and better answers than those who don't. spencer graves Martin Lam wrote:> Hi, > > I was wondering if it is possible to get the > rowindices without using the function "which" because > I don't have a restriction criteria. Here's an example > of what I mean: > # take 10 randomly selected instances > iris[sample(1:nrow(iris), 10),] > > # output > Sepal.Length Sepal.Width Petal.Length Petal.Width > Species > 76 6.6 3.0 4.4 1.4 > versicolor > 105 6.5 3.0 5.8 2.2 > virginica > 131 7.4 2.8 6.1 1.9 > virginica > 79 6.0 2.9 4.5 1.5 > versicolor > 69 6.2 2.2 4.5 1.5 > versicolor > 42 4.5 2.3 1.3 0.3 > setosa > 25 4.8 3.4 1.9 0.2 > setosa > 129 6.4 2.8 5.6 2.1 > virginica > 60 5.2 2.7 3.9 1.4 > versicolor > 80 5.7 2.6 3.5 1.0 > versicolor > > What I want to get are their rownumbers: 76, 105, 131, > 79, 69, 42, 25, 129, 60, 80. > > 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-- Spencer Graves, PhD Senior Development Engineer PDF Solutions, Inc. 333 West San Carlos Street Suite 700 San Jose, CA 95110, USA spencer.graves at pdf.com www.pdf.com <http://www.pdf.com> Tel: 408-938-4420 Fax: 408-280-7915