[R] Prevent sequential repeated values in data frame column

Jon Zadra jrz9f at virginia.edu
Tue Aug 11 21:25:32 CEST 2009


I'm trying to randomize a sequence of trials for an experimental 
design.  The trials consist of values for each of two factors.  As is 
there are 30 combinations of the two factors, and I want them to be 
ordered randomly but with the requirement that for one of the factors, 
the value can never be the same as the previous value.

I'm currently randomizing my dataframe by using:

  x[sample(1:nrow(x), nrow(x),]

Output example, on rows 2 and 3 the angle value is the same (the 
situation I wish to prevent):
    Distance Angle
         9    90
         9    45
        10   45
        11   30
         8    60
         7     0
         8    30
        10     0
        10    60
         ...    ...

Can anyone recommend a simple way to do this?  (Bonus if it could be 
implemented for more than a single column!)

Thanks in advance,

Jon Zadra
Department of Psychology
University of Virginia
P.O. Box 400400
Charlottesville VA 22904
(434) 982-4744
email: zadra at virginia.edu

More information about the R-help mailing list