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:
> Hallo,
>
> suppose I have a vector:
>
> x <- c(1,1,1,2,2,3,3,3,3,3,4)
>
> How can I generate a vector/sequence in which a fixed number of zeroes (say
> 3) is ins
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 <[EMAIL PROTECTED]> wrote:
>
You could use a combination of rle, cumsum and append.
> x <- c(1,1,1,2,2,3,3,3,3,3,4)
> y<-rle(x)$lengths
> y
[1] 3 2 5 1
> z<-cumsum(y)[y>1]
> z
[1] 3 5 10
>
> for(i in rev(z)) x <- append(x, c(0,0,0), after = i)
> x
[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
3 matches
Mail list logo