[R] Randomly extract rows from a data frame

Wensui Liu liuwensui at gmail.com
Mon Feb 19 05:48:34 CET 2007


amy,
here is a piece of code copied from my blog, which might answer part
of your question.

library(MASS);
data(Boston);

# DIVIDE DATA INTO TESTING AND TRAINING SETS
set.seed(2005);
test.rows <- sample(1:nrow(Boston), 100);
test.set <- Boston[test.rows, ];
train.set <- Boston[-test.rows, ];


On 2/18/07, Amy Whitehead <alw76 at student.canterbury.ac.nz> 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
>
>
> Amy Whitehead
> School of Biological Sciences
> University of Canterbury
> Private Bag 4800
> Christchurch
> Ph 03 364 2987 ext 7033
> Cellphone 021 2020525
> Email alw76 at student.canterbury.ac.nz
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
WenSui Liu
A lousy statistician who happens to know a little programming
(http://spaces.msn.com/statcompute/blog)



More information about the R-help mailing list