# [R] Splitting Numerical Vector Into Chunks

William Dunlap wdunlap at tibco.com
Wed Apr 20 21:55:48 CEST 2016

```> i <- seq_len(length(x)-1)
> split(x, cumsum(c(TRUE, (x[i]==0) != (x[i+1]==0))))
\$`1`
[1] 0.144872972504 0.850797178400

\$`2`
[1] 0 0 0

\$`3`
[1] 0.199304859380 2.063609410700 0.939393760782 0.838781367540

\$`4`
[1] 0 0 0 0 0

\$`5`
[1] 0.374688091264 0.488423999452 0.783034615362 0.626990428900
0.138188255307 2.324635712186

\$`6`
[1] 0 0 0 0 0 0 0

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, Apr 20, 2016 at 12:49 PM, Ista Zahn <istazahn at gmail.com> wrote:

> Perhaps
>
> x <- split(x, x == 0)
>
> Best,
> Ista
>
> On Wed, Apr 20, 2016 at 9:40 AM, Sidoti, Salvatore A.
> <sidoti.23 at buckeyemail.osu.edu> wrote:
> > Greetings!
> >
> > I have several large data sets of animal movements. Their pauses (zero
> magnitude vectors) are of particular interest in addition to the speed
> distributions that precede the periods of rest. Here is an example of the
> kind of data I am interested in analyzing:
> >
> > x <-
> abs(c(rnorm(2),replicate(3,0),rnorm(4),replicate(5,0),rnorm(6),replicate(7,0)))
> > length(x)
> >
> > This example has 27 elements with strings of zeroes (pauses) situated
> among the speed values.
> > Is there a way to split the vector into zero and nonzero chunks and
> store them in a form where they can be analyzed? I have tried various forms
> of split() to no avail.
> >
> > Thank you!
> > Salvatore A. Sidoti
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help