From: Marc Schwartz <marc_schwartz_at_comcast.net>

Date: Thu, 20 Dec 2007 16:33:54 -0600

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 Thu 20 Dec 2007 - 22:36:31 GMT

Date: Thu, 20 Dec 2007 16:33:54 -0600

On Thu, 2007-12-20 at 22:43 +0100, Johannes Graumann wrote:

> Hi all,

*>
**> Does anybody have a magic trick handy to isolate directly consecutive
**> integers from something like this:
**> c(1,2,3,4,7,8,9,10,12,13)
**>
**> The result should be, that groups 1-4, 7-10 and 12-13 are consecutive
**> integers ...
**>
**> Thanks for any hints, Joh
*

Not fully tested, but here is one possible approach:

*> Vec
*

[1] 1 2 3 4 7 8 9 10 12 13

Breaks <- c(0, which(diff(Vec) != 1), length(Vec))

> Breaks

[1] 0 4 8 10

> sapply(seq(length(Breaks) - 1),

function(i) Vec[(Breaks[i] + 1):Breaks[i+1]])
[[1]]

[1] 1 2 3 4

[[2]]

[1] 7 8 9 10

[[3]]

[1] 12 13

For a quick test, I tried it on another vector:

set.seed(1)

Vec <- sort(sample(20, 15))

*> Vec
*

[1] 1 2 3 4 5 6 8 9 10 11 14 15 16 19 20

Breaks <- c(0, which(diff(Vec) != 1), length(Vec))

> Breaks

[1] 0 6 10 13 15

> sapply(seq(length(Breaks) - 1),

function(i) Vec[(Breaks[i] + 1):Breaks[i+1]])
[[1]]

[1] 1 2 3 4 5 6

[[2]]

[1] 8 9 10 11

[[3]]

[1] 14 15 16

[[4]]

[1] 19 20

**HTH,
**
Marc Schwartz

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 Thu 20 Dec 2007 - 22:36:31 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 Fri 21 Dec 2007 - 15:30:20 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.
*