Jannis
2010-Dec-18 17:21 UTC
[R] determine length of sequence of equal elements in a vector
Dear list members, I am seeking a function that returns me the length of a continous sequence of identical elements in a vector. Something like (or similar to): example = c(1,1,1,2,2,3,3,3,3) result = c(3,3,3,2,2,4,4,4,4) I am quite sure there already exists a function to do this, I just cant figure out its name. Otherwise I would start programming my own function. Best Jannis
Duncan Murdoch
2010-Dec-18 17:28 UTC
[R] determine length of sequence of equal elements in a vector
On 18/12/2010 12:21 PM, Jannis wrote:> Dear list members, > > > I am seeking a function that returns me the length of a continous > sequence of identical elements in a vector. Something like (or similar to): > > > example = c(1,1,1,2,2,3,3,3,3) > > > result = c(3,3,3,2,2,4,4,4,4) > > > I am quite sure there already exists a function to do this, I just cant > figure out its name. Otherwise I would start programming my own function.The rle() function produces the data in a different format. Together with rep() you can get what you want: x <- rle(example) rep(x$lengths, x$lengths) Duncan Murdoch
Jannis
2010-Dec-18 17:33 UTC
[R] determine length of sequence of equal elements in a vector
Thanks a lot, Duncan. Duncan Murdoch schrieb:> On 18/12/2010 12:21 PM, Jannis wrote: >> Dear list members, >> >> >> I am seeking a function that returns me the length of a continous >> sequence of identical elements in a vector. Something like (or >> similar to): >> >> >> example = c(1,1,1,2,2,3,3,3,3) >> >> >> result = c(3,3,3,2,2,4,4,4,4) >> >> >> I am quite sure there already exists a function to do this, I just cant >> figure out its name. Otherwise I would start programming my own >> function. > > The rle() function produces the data in a different format. Together > with rep() you can get what you want: > > x <- rle(example) > rep(x$lengths, x$lengths) > > Duncan Murdoch >