[R] Boundaries of consecutive integers

arun smartpink111 at yahoo.com
Tue May 14 06:25:32 CEST 2013

 indx<- c(0,cumsum(diff(test)!=1))
aggregate(test,list(indx),function(x) c(min(x),max(x)))[,-1]
#     [,1] [,2]
#[1,]    1    5
#[2,]   22   29
#[3,]   33   40

dat1<- data.frame(test,indx)
 ddply(dat1,.(indx),summarize, Min=min(test),Max=max(test))[,-1]
#  Min Max
#1   1   5
#2  22  29
#3  33  40


----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: Lizzy Wilbanks <egwilbanks at ucdavis.edu>
Cc: R help <r-help at r-project.org>
Sent: Tuesday, May 14, 2013 12:11 AM
Subject: Re: [R] Boundaries of consecutive integers

May be:
#     [,1] [,2]
#[1,]    1    5
#[2,]   22   29
#[3,]   33   40

----- Original Message -----
From: Lizzy Wilbanks <egwilbanks at ucdavis.edu>
To: r-help at r-project.org
Sent: Monday, May 13, 2013 9:18 PM
Subject: [R] Boundaries of consecutive integers

Hi folks,

I'm trying to accomplish something that seems like it should be
straightforward, but I've gotten tied in knots trying to figure it
out.  A toy example of my issue is below.  I've played with diff and
can't seem to figure out a systematic solution that will give me the
two column output independent of the number of breakpoints in the

test<-c(1:5, 22:29,33:40)

Any ideas?


"The obvious goal of any bacterium is to become bacteria."

Lizzy Wilbanks
Graduate Student, Eisen and Facciotti Labs
UC Davis, Microbiology Graduate Group

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