[R] How do you test for "consecutivity"?
jmburgos at u.washington.edu
Tue Apr 29 19:07:19 CEST 2008
There must be many ways to do this. This is one of them:
#First, define a function to calculate the proportion of consecutive
numbers in a vector.
#Note that I am counting both numbers in a consecutive pair. For
example, the vector c(1,2,6,9,10) will contain 4 consecutive numbers. I
think this is what you wanted do do, right?
#Next, generate a matrix with 1000 columns (one for each experiment) and
5 rows (the five numbers in each experiment). Note the use of the
'replicate' function to generate multiple sets of random numbers
#Third, use the apply function to apply the function we defined above to
each column of the matrix
# This will give you a vector with the 1000 proportions of consecutive
> I need to use R to model a large number of experiments (say, 1000). Each
> experiment involves the random selection of 5 numbers (without replacement)
> from a pool of numbers ranging between 1 and 30.
> What I need to know is what *proportion* of those experiments contains two
> or more numbers that are consecutive. So, for instance, an experiment that
> yielded the numbers 2, 28, 31, 4, 27 would be considered a "consecutive =
> true" experiment since 28 and 27 are two consecutive numbers, even though
> they are not side-by-side.
> I am quite new to R, so really am puzzled as to how to go about this. I've
> tried sorting each experiment, and then subtracting adjacent pairs of
> numbers to see if the difference is plus or minus 1. I'm also unsure about
> whether to use an array to store all the data first.
> Any assistance would be much appreciated.
More information about the R-help