Emmanuel Levy
2007-Aug-21 20:49 UTC
[R] (Most efficient) way to make random sequences of random sequences
Hi, I was wondering the what would be the (most efficient) way to generate a sequence of sequences, i mean: if I have 1,2 and 3. I'd like to generate a sequence of length N*3 (N ~ 1,000,000 or more) Where random permutations of the sequence 1,2,3 follow each other. i.e 1,2,3,1,3,2,3,2,1 /!\ The thing is that there should never be twice the same number of in the same sub-sequence, meaning that this is different from generating a vector with the numbers 1,2 and 3 randomly distributed. Any suggestion very welcome! Thanks, Emmanuel
roger koenker
2007-Aug-21 21:24 UTC
[R] (Most efficient) way to make random sequences of random sequences
One way: N <- 10 s <- c(apply(matrix(rep(1:3,N),3,N),2,sample)) url: www.econ.uiuc.edu/~roger Roger Koenker email rkoenker at uiuc.edu Department of Economics vox: 217-333-4558 University of Illinois fax: 217-244-6678 Champaign, IL 61820 On Aug 21, 2007, at 3:49 PM, Emmanuel Levy wrote:> Hi, > > I was wondering the what would be the (most efficient) way to generate > a sequence > of sequences, i mean: > > if I have 1,2 and 3. > > I'd like to generate a sequence of length N*3 (N ~ 1,000,000 or more) > > Where random permutations of the sequence 1,2,3 follow each other. > > i.e 1,2,3,1,3,2,3,2,1 > > /!\ The thing is that there should never be twice the same number of > in the same sub-sequence, meaning that this is different from > generating a vector with the numbers 1,2 and 3 randomly distributed. > > Any suggestion very welcome! Thanks, > > Emmanuel > > ______________________________________________ > 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.
Rolf Turner
2007-Aug-21 21:28 UTC
[R] (Most efficient) way to make random sequences of random sequences
On 22/08/2007, at 8:49 AM, Emmanuel Levy wrote:> Hi, > > I was wondering the what would be the (most efficient) way to generate > a sequence > of sequences, i mean: > > if I have 1,2 and 3. > > I'd like to generate a sequence of length N*3 (N ~ 1,000,000 or more) > > Where random permutations of the sequence 1,2,3 follow each other. > > i.e 1,2,3,1,3,2,3,2,1 > > /!\ The thing is that there should never be twice the same number of > in the same sub-sequence, meaning that this is different from > generating a vector with the numbers 1,2 and 3 randomly distributed.require(gtools) m <- permutations(3,3) M <- m[sample(1:6,1000000,TRUE),] x <- as.vector(t(M)) ###################################################################### Attention:\ This e-mail message is privileged and confidenti...{{dropped}}
Apparently Analagous Threads
- Best HMM package to generate random (protein) sequences?
- Finding (swapped) repetitions of numbers pairs across two columns
- Random sampling while keeping distribution of nearest neighbor distances constant.
- group bunch of lines in a data.frame, an additional requirement
- A particular shuffling on a vector