[R] shuffling a vector
Marc Schwartz
MSchwartz at medanalytics.com
Mon Nov 10 17:57:17 CET 2003
On Mon, 2003-11-10 at 10:51, Marc Schwartz wrote:
> On Mon, 2003-11-10 at 10:28, Rajarshi Guha wrote:
> > Hi,
> > I'me trying to write a function that will shuffle a vector. At the
> > moment I'm baically making a vector of randomized indices and then
> > making a new vector from the original one using these random indices.
> >
> > However, is there an alternative (more elegant) method to do this? I
> > tried help.search('shuffle') but it does'nt return anything relevant.
> >
> > Thanks,
>
>
> You might want to look at the permute() function in the 'gregmisc'
> package.
>
> Example:
>
> > x <- 1:10
> > permute(x)
> [1] 1 3 2 6 5 7 10 8 9 4
>
> HTH,
>
> Marc Schwartz
A quick add:
Also the sample() function in the base package:
> sample(x)
[1] 4 10 5 3 7 2 1 6 9 8
See ?sample for more information. sample() is also more flexible in
terms of options if you want more than just a permutation of the
original vector.
HTH,
Marc
More information about the R-help
mailing list