[R] Sampling from a Data Frame

Kenneth Cabrera krcabrer at perseus.unalmed.edu.co
Mon Apr 7 14:11:21 CEST 2003


On Sat, 5 Apr 2003 09:01:13 +1200 (NZST), Ko-Kang Kevin Wang 
<kwan022 at stat.auckland.ac.nz> wrote:

> Hi,
>
> I've been looking through the documentation for sample(), but can only 
> get it to work with vectors.  Is it possible to sample from a dataframe?
>
I think somebody already answer, but, just in case:

suppose dfrme is a "data frame" and "n" is the sample size, then using
indexing you try this:

samp1<-dfrme[sample(1:dim(dfrme)[1],n),]

If you want to preserve the original order in the sample:

samp1<-dfrme[sort(sample(1:dim(dfrme)[1],n)),]


--



More information about the R-help mailing list