[R] How to get the rowindices without using which?
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Mon Sep 26 11:01:07 CEST 2005
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
More information about the R-help
mailing list