Thanks in advance. I have a vector of numbers which contain sections that are sequences which increase by a value of 1 followed by a gap in the data and then another sequence occurs, etc: x<-c(1:3, 6: 7, 10:13) From the vector I need to extract 2 items of information A) the first number in the sequence (e.g., 1, 6, 10) and B) how many observations were in each sequence section (e.g., 3, 2, 4). v1 v2 1 3 6 2 10 4 It seems simple, but my R skills are still in their infancy so I very much appreciate the help. Eric -- Eric Vander Wal Ph.D. Candidate University of Saskatchewan, Department of Biology, 112 Science Place, Saskatoon, SK., S7N 5E2 "Pluralitas non est ponenda sine neccesitate"
try this:> x[1] 1 2 3 6 7 10 11 12 13> # find breaks > breaks <- c(FALSE, diff(x) != 1) > # now create matrix with groupings (just for visual) > z <- data.frame(x, cumsum(breaks)) > # create list with first element of each seq and the length > t(sapply(split(z, z[,2]), function(b) c(b[1,1], nrow(b))))[,1] [,2] 0 1 3 1 6 2 2 10 4 On Wed, Jun 10, 2009 at 1:28 PM, Eric Vander Wal <eric.vanderwal@usask.ca>wrote:> Thanks in advance. > I have a vector of numbers which contain sections that are sequences which > increase by a value of 1 followed by a gap in the data and then another > sequence occurs, etc: > > x<-c(1:3, 6: 7, 10:13) > > From the vector I need to extract 2 items of information A) the first > number in the sequence (e.g., 1, 6, 10) and B) how many observations were in > each sequence section (e.g., 3, 2, 4). > > v1 v2 > 1 3 > 6 2 > 10 4 > > It seems simple, but my R skills are still in their infancy so I very much > appreciate the help. > Eric > > -- > Eric Vander Wal > Ph.D. Candidate > University of Saskatchewan, Department of Biology, > 112 Science Place, Saskatoon, SK., S7N 5E2 > > "Pluralitas non est ponenda sine neccesitate" > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
jim holtman wrote:> try this: > >Oh well, i spent the time writing this so i might as well post my (almost identical) solution, x<-c(1:3, 6: 7, 10:13) breaks = c(TRUE, diff(x) != 1) data.frame(start = x[breaks], length = tabulate(cumsum(breaks))) Hoping this works, baptiste>> x >> > [1] 1 2 3 6 7 10 11 12 13 > >> # find breaks >> breaks <- c(FALSE, diff(x) != 1) >> # now create matrix with groupings (just for visual) >> z <- data.frame(x, cumsum(breaks)) >> # create list with first element of each seq and the length >> t(sapply(split(z, z[,2]), function(b) c(b[1,1], nrow(b)))) >> > [,1] [,2] > 0 1 3 > 1 6 2 > 2 10 4 > > > On Wed, Jun 10, 2009 at 1:28 PM, Eric Vander Wal <eric.vanderwal at usask.ca>wrote: > > >> Thanks in advance. >> I have a vector of numbers which contain sections that are sequences which >> increase by a value of 1 followed by a gap in the data and then another >> sequence occurs, etc: >> >> x<-c(1:3, 6: 7, 10:13) >> >> From the vector I need to extract 2 items of information A) the first >> number in the sequence (e.g., 1, 6, 10) and B) how many observations were in >> each sequence section (e.g., 3, 2, 4). >> >> v1 v2 >> 1 3 >> 6 2 >> 10 4 >> >> It seems simple, but my R skills are still in their infancy so I very much >> appreciate the help. >> Eric >> >> -- >> Eric Vander Wal >> Ph.D. Candidate >> University of Saskatchewan, Department of Biology, >> 112 Science Place, Saskatoon, SK., S7N 5E2 >> >> "Pluralitas non est ponenda sine neccesitate" >> >> ______________________________________________ >> 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<http://www.r-project.org/posting-guide.html> >> and provide commented, minimal, self-contained, reproducible code. >> >> > > > >
On Jun 10, 2009, at 1:28 PM, Eric Vander Wal wrote:> Thanks in advance. > I have a vector of numbers which contain sections that are sequences > which increase by a value of 1 followed by a gap in the data and > then another sequence occurs, etc: > > x<-c(1:3, 6: 7, 10:13) > > From the vector I need to extract 2 items of information A) the > first number in the sequence (e.g., 1, 6, 10) and B) how many > observations were in each sequence section (e.g., 3, 2, 4). > > v1 v2 > 1 3 > 6 2 > 10 4 > > It seems simple, but my R skills are still in their infancy so I > very much appreciate the help. > Eric >Yet another solution: > x[which(c(-Inf, diff(x)) != 1)] [1] 1 6 10 > diff(c(which(c(-Inf, diff(x)) != 1), length(x)+1) ) [1] 3 2 4 -- David Winsemius, MD Heritage Laboratories West Hartford, CT