# [R] How do you test for "consecutivity"?

Charles C. Berry cberry at tajo.ucsd.edu
Tue Apr 29 18:34:53 CEST 2008

```On Tue, 29 Apr 2008, Anthony28 wrote:

>
> 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.

Are the numbers 1:30 equiprobable??

If so, you can find the probability by direct enumeration.

> mat <- combn(30,5) # each column happens to be in order
> tab <- table( mat[2:5,]-mat[1:4,]==1, col(mat[1:4,]) )
> table(tab[2,])

0     1     2     3     4
65780 59800 15600  1300    26
> prop.table( table(tab[2,] != 0 ) )

FALSE      TRUE
0.4615946 0.5384054
>

If the numbers are not equiprobable, you will need to weight the values of
tab[2,] according to the probability of each column of mat.

HTH,

Chuck

> --
> View this message in context: http://www.nabble.com/How-do-you-test-for-%22consecutivity%22--tp16959748p16959748.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help