Skip to content

Inserting a subsequence between values of a vector

4 messages · Serguei Kaniovski, Chris Stubben, Gabor Grothendieck +1 more

#
You could use a combination of rle, cumsum and append.
[1] 3 2 5 1
[1]  3  5 10
[1] 1 1 1 0 0 0 2 2 0 0 0 3 3 3 3 3 0 0 0 4


Chris
Serguei Kaniovski-3 wrote:

  
    
#
Take the rle, fix up the result and take the invese.rle.  Our formula
adds 0's to the end too so remove those with head:

head(inverse.rle(with(rle(x), list(lengths = c(rbind(lengths, 3)),
values = c(rbind(values, 0))))), -3)
On Dec 4, 2007 10:49 AM, Serguei Kaniovski <Serguei.Kaniovski at wifo.ac.at> wrote:
#
library(R.utils)
pos=which(diff(x)==1)+1
insert(x,ats=pos,rep(list(rep(0,3)),length(pos)))

domenico vistocco
Serguei Kaniovski wrote: