Re: [R] Switching entries in vector in by groups of two

2008-06-27 Thread Peter Dalgaard
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, >>

Re: [R] Switching entries in vector in by groups of two

2008-06-27 Thread David Afshartous
Thanks Henrique, Marc, and Gabor! On 6/27/08 10:17 AM, "Henrique Dallazuanna" <[EMAIL PROTECTED]> wrote: > Try this: > > ave(X, rep(1:(length(X)/2), each = 2), FUN=rev) > > On Fri, Jun 27, 2008 at 11:11 AM, David Afshartous <[EMAIL PROTECTED]> > wrote: >> >> All, >> >> I have a long vector

Re: [R] Switching entries in vector in by groups of two

2008-06-27 Thread Marc Schwartz
on 06/27/2008 09:17 AM Marc Schwartz wrote: on 06/27/2008 09:11 AM David Afshartous 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,

Re: [R] Switching entries in vector in by groups of two

2008-06-27 Thread Henrique Dallazuanna
Try this: ave(X, rep(1:(length(X)/2), each = 2), FUN=rev) 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 writ

Re: [R] Switching entries in vector in by groups of two

2008-06-27 Thread Marc Schwartz
on 06/27/2008 09:11 AM David Afshartous 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

Re: [R] Switching entries in vector in by groups of two

2008-06-27 Thread Gabor Grothendieck
You can do it without and index like this: c(matrix(X, 2)[2:1,]) or if you need the index for some purpose apart from this: c(matrix(seq_along(X), 2)[2:1,]) On Fri, Jun 27, 2008 at 10:11 AM, David Afshartous <[EMAIL PROTECTED]> wrote: > > All, > > I have a long vector that contains an even numb

[R] Switching entries in vector in by groups of two

2008-06-27 Thread David Afshartous
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?