# Re: [R] Inserting a subsequence between values of a vector

From: Chris Stubben <stubben_at_lanl.gov>
Date: Tue, 4 Dec 2007 08:27:02 -0800 (PST)

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 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 inserted between the consecutive values, so I get
>
> 1,1,1,0,0,0,2,2,0,0,0,3,3,3,3,3,0,0,0,4
>
> thanks a lot,
> Serguei
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

```--
View this message in context: http://www.nabble.com/Inserting-a-subsequence-between-values-of-a-vector-tf4943930.html#a14154029
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help