Henrique Dallazuanna wrote:
> Try this:
>
> ave(X, rep(1:(length(X)/2), each = 2), FUN=rev)
>
>   
Also,

ix <- 2*rep(1:(length(X)/2-1), each = 2) + 2:1
X[ix]

or

ix <- seq_along(X) + c(1,-1)
X[ix]
> On Fri, Jun 27, 2008 at 11:11 AM, David Afshartous <
> [EMAIL PROTECTED]> wrote:
>
>   
>> All,
>>
>> I have a long vector that contains an even number of entries. I'd like to
>> switch the 1st and 2nd entry, the 3rd and 4th, and so on, without writing a
>> loop.
>>
>> This code works:
>>
>> X = c(8, 10, 6, 3, 20, 1)
>> index = c(2,1,4,3,6,5)
>> X[index]
>>
>> But for a long list is there a way to generate the index?  I can get the
>> parts to the index as:
>>
>> index.odd = seq(1,length(X), by  = 2)
>> index.even = index.odd + 1
>>
>> Is there a simple way to interweave them to produce the desired index?  Or
>> is there a better way?
>>
>> Cheers,
>> David
>>
>> ______________________________________________
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>     
>
>
>
>   
> ------------------------------------------------------------------------
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>   


-- 
   O__  ---- Peter Dalgaard             Ă˜ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - ([EMAIL PROTECTED])              FAX: (+45) 35327907

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to