[R] Randomly extract rows from a data frame
David Scott
d.scott at auckland.ac.nz
Mon Feb 19 05:01:22 CET 2007
On Mon, 19 Feb 2007, Amy Whitehead wrote:
> Hi,
>
> I am looking for a way to randomly extract a specified number of rows from a
> data frame. I was planning on binding a column of random numbers to the
> data frame and then sorting the data frame using this bound column. But I
> can't figure out how to use this column to sort the entire data frame so
> that the content of the rows remains together. Does anyone know how I can
> do this? Hints for other ways to approach this problem would also be
> appreciated.
>
> Cheers
> Amy
>
It is a bit easier than that.
Here is one way:
> df <- airquality
> rNames <- row.names(df)
> sampRows <- sample(rNames,10)
> sampRows
[1] "137" "56" "1" "135" "62" "43" "12" "128" "86" "54"
> subset(df,rNames%in%sampRows)
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
12 16 256 9.7 69 5 12
43 NA 250 9.2 92 6 12
54 NA 91 4.6 76 6 23
56 NA 135 8.0 75 6 25
62 135 269 4.1 84 7 1
86 108 223 8.0 85 7 25
128 47 95 7.4 87 9 5
135 21 259 15.5 76 9 12
137 9 24 10.9 71 9 14
David Scott
_________________________________________________________________
David Scott Department of Statistics, Tamaki Campus
The University of Auckland, PB 92019
Auckland 1142, NEW ZEALAND
Phone: +64 9 373 7599 ext 86830 Fax: +64 9 373 7000
Email: d.scott at auckland.ac.nz
Graduate Officer, Department of Statistics
More information about the R-help
mailing list