Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a vector by its sequences [duplicate]

Tags:

r

The following vector x contains the two sequences 1:4 and 6:7, among other non-sequential digits.

x <- c(7, 1:4, 6:7, 9)

I'd like to split x by its sequences, so that the result is a list like the following.

# [[1]]
# [1] 7
#
# [[2]]
# [1] 1 2 3 4
#
# [[3]]
# [1] 6 7
#
# [[4]]
# [1] 9

Is there a quick and simple way to do this?

I've tried

split(x, c(0, diff(x)))

which gets close, but I don't feel like appending 0 to the differenced vector is the right way to go. Using findInterval didn't work either.

like image 264
Rich Scriven Avatar asked Sep 11 '14 17:09

Rich Scriven


2 Answers

split(x, cumsum(c(TRUE, diff(x)!=1)))
#$`1`
#[1] 7
#
#$`2`
#[1] 1 2 3 4
#
#$`3`
#[1] 6 7
#
#$`4`
#[1] 9
like image 159
Roland Avatar answered Nov 04 '22 00:11

Roland


Just for fun, you can make use of Carl Witthoft's seqle function from his "cgwtools" package. (It's not going to be anywhere near as efficient as Roland's answer.)

library(cgwtools)

## Here's what seqle does...
## It's like rle, but for sequences
seqle(x)
# Run Length Encoding
#   lengths: int [1:4] 1 4 2 1
#   values : num [1:4] 7 1 6 9

y <- seqle(x)
split(x, rep(seq_along(y$lengths), y$lengths))
# $`1`
# [1] 7
# 
# $`2`
# [1] 1 2 3 4
# 
# $`3`
# [1] 6 7
# 
# $`4`
# [1] 9
like image 26
A5C1D2H2I1M1N2O1R2T1 Avatar answered Nov 03 '22 23:11

A5C1D2H2I1M1N2O1R2T1