[R] need help with ramdomly sampling some data

Peter Langfelder peter.langfelder at gmail.com
Wed Sep 29 03:28:11 CEST 2010


On Tue, Sep 28, 2010 at 6:22 PM, Michael Larkin <mlarkin at rsmas.miami.edu> wrote:
> I am trying to get R to randomly select values from my dataset (i.e.
> bootstrapping) with replacement.  However, my attempts at this have been
> unsuccessful.  Here is a basic example of what I am doing:
>
> I have a data vector of 8 values (i.e. data= 2,5,9,4,5,6,7,8).  I used the
> sample function and it worked.  However, it only repeated my values in the
> exact same order as the dataset.  It did not randomly sample them.  Here the
> code for what I did:
>
> sample(data, replace=TRUE)

You're doing the right thing. Perhaps your random seed was set in a
particular way, or perhaps you made some mistake in the execution, but
I get the following:

> data= c(2,5,9,4,5,6,7,8)
> sample(data, replace=TRUE)
[1] 5 5 5 4 9 8 2 7

which is what you likely want.

Peter



More information about the R-help mailing list