# [R] repeating values in an index two by two

Carl Witthoft carl at witthoft.com
Mon Nov 11 17:16:06 CET 2013

```Here's a rather extreme solution:

foo<-rep(1:6,each=2)
Rgames> foo
[1] 1 1 2 2 3 3 4 4 5 5 6 6

Rgames> foo[rep(c(1,3,2,4),3)+rep(c(0,4,8),each=4)]
[1] 1 2 1 2 3 4 3 4 5 6 5 6

In the general case, then, it would be something like

foo<- rep(1:N, each = 2)  # foo is of length(2*N)

foo[rep(c(1,3,2,4),2*N/4 + rep( seq(0, 3*N/4,by=4),each=4)]

Note that the refolding requires the sequence to have length a multiple of
4.

Patrick Burns wrote
>> f1
> function(x) {
>      one <- matrix(1:x, nrow=2)
>      as.vector(rbind(one, one))
> }
> <environment: 0x000000000daaf1c0>
>  > f1(8)
>   [1] 1 2 1 2 3 4 3 4 5 6 5 6 7 8 7 8
>
> Pat
>
>
> On 11/11/2013 12:11, Federico Calboli wrote:
>> Hi All,
>>
>> I am trying to create an index that returns something like
>>
>> 1,2,1,2,3,4,3,4,5,6,5,6,7,8,7,8
>>
>> and so on and so forth until a predetermined value (which is obviously
>> even).  I am trying very hard to avoid for loops or for loops front ends.
>>
>> I'd be obliged if anybody could offer a suggestion.
>>
>> BW
>>
>> F
>>
>>
>>
>> ______________________________________________
>>

> R-help@

>  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.
>>
>
> --
> Patrick Burns

> pburns at .seanet

> http://www.portfolioprobe.com/blog
> http://www.burns-stat.com
> (home of:
>   'Impatient R'
>   'The R Inferno'
>   'Tao Te Programming')
>
> ______________________________________________

> R-help@

>  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://r.789695.n4.nabble.com/repeating-values-in-an-index-two-by-two-tp4680210p4680234.html
Sent from the R help mailing list archive at Nabble.com.

```