[R] random sequence

David Winsemius dwinsemius at comcast.net
Tue Jun 22 15:49:52 CEST 2010


On Jun 22, 2010, at 7:15 AM, status at quo.org wrote:

> Hi Barbara,
>
> You could use:
>
> samples = matrix(rep(0,(5000*40)),ncol=40)
> for(i in 1:5000) {
>    samples[i,] = sample(1:40,40)
> }

Might also be a good time to use replicate():

samples = matrix(replicate(5000, sample(1:40) ), ncol=40, byrow=TRUE)

>
> This will give you a matrix of samples, one per row. The second term  
> in
> the "sample" function is the length of your sequence. By default  
> "sample"
> samples without replacement, so there won't be any duplicates.

And by default the number of sampled is length of the sampled vector,  
so it returns a permutation.

>
> The "for" loop is generally slower than using "lapply",

Can you cite any evidence for that assertion?


> but for just 5000
> samples it won't make much difference.
>
> Cheers,
>
> Matt Asher
> StatisticsBlog.com
>
>
>
>
>> I have a sequence of number from 1 to 40 and I have to extract a  
>> random
>> sequence, for example:
>> 1) 1, 2 3, 4, ...40
>> 2) 2, 10, 9 , 25....
>>
>> e so on for 5000 times. How I can do????
>> Thanks
-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list