[R] Splitting Numerical Vector Into Chunks

Ista Zahn istazahn at gmail.com
Wed Apr 20 21:49:23 CEST 2016


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
> 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