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

Charles Annis, P.E. Charles.Annis at StatisticalEngineering.com
Tue Apr 29 18:14:25 CEST 2008

This will work:

my.list <- c(2, 28, 31, 4, 27)
any(diff(sort(my.list)) == 1)

the middle two lines are only to illustrate what's going on.

Best wishes!

Charles Annis, P.E.

Charles.Annis at StatisticalEngineering.com
phone: 561-352-9699
eFax:  614-455-3265

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Anthony28
Sent: Tuesday, April 29, 2008 8:52 AM
To: r-help at r-project.org
Subject: [R] How do you test for "consecutivity"?

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.
View this message in context:
Sent from the R help mailing list archive at Nabble.com.

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list