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

From: Doran, Harold <HDoran_at_air.org>
Date: Tue, 29 Apr 2008 09:16:43 -0400


How about this

result <- numeric(10)
for(i in 1:10){
 x <- sample(1:30, 5, replace = FALSE)
 x <- sort(x)
 result[i] <- any(diff(x) == 1)
}  

> -----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:
> 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
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



R-help_at_r-project.org 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. Received on Tue 29 Apr 2008 - 13:43:33 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 Apr 2008 - 14:30:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive