Making heavy use of some R idioms:
> split(v, cumsum(c(1, diff(v) != 1)))
$`1`
[1] 1
$`2`
[1] 3 4 5
$`3`
[1] 9 10
$`4`
[1] 17
$`5`
[1] 29 30
More Related Contents:
- Insert blanks into a vector for, e.g., minor tick labels in R
- Split a vector into chunks
- How to find common elements from multiple vectors?
- How to convert a list consisting of vector of different lengths to a usable data frame in R?
- How to cbind or rbind different lengths vectors without repeating the elements of the shorter vectors?
- Fixing the order of facets in ggplot
- generate sequence within group in R [duplicate]
- How to tell what is in one vector and not another?
- rep() with each equals a vector
- Create integer sequences defined by ‘from’ and ‘to’ vectors
- else if(){} VS ifelse()
- Finding All Positions for Multiple Elements in a Vector
- Convert data.frame column to a vector?
- Select every other element from a vector
- Converting two columns of a data frame to a named vector
- calculating mean for every n values from a vector
- Find start and end positions/indices of runs/consecutive values
- Create counter of consecutive runs of a certain value
- Unlist a data frame by rows, not columns
- Split delimited single value character vector
- Count values separated by a comma in a character string
- Creating a unique sequence of dates
- R split numeric vector at position
- Extract a column from a data.table as a vector, by position
- How to return 5 topmost values from vector in R?
- How to reverse order a vector?
- propagating data within a vector
- R reshape a vector into multiple columns
- Deleting every n-th row in a dataframe
- Create a Vector of All Days Between Two Dates