Skip to content
Prev 342772 / 398506 Next

How to randomly extract a number of rows in a data frame

Do you know how to extract some rows of a data.frame?  A short answer
is with subscripts, either integer,
   first10 <- 1:10
   dFirst10 <- d[first10, ] # I assume your data.frame is called 'd'
or logical
   plus4 <- d[, "Col_4"] == "+"
   dPlus4 <- d[ plus4, ]
If you are not familiar with that sort of thing, read the introduction
to R document that comes with R.

So you can solve your problem if you can generate a vector containing
1 million integers in the range 1:10^7.  Use the sample function for
that.  You must decide if you want to allow duplicate rows or not
(i.e., sampling with or without replacement). Type
  ?sample
to see the details.


Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Fri, Aug 1, 2014 at 11:58 AM, Stephen HK Wong <honkit at stanford.edu> wrote: