Hello everybody, I wonder if there is any swap function in R that does the following: x <- seq(1,10,12) x1 <- swap(x) x1 1 8 3 4 5 6 7 2 10 Thank you very much! Amor __________________________________________________ Schutz gegen Massenmails. [[alternative HTML version deleted]]
> I wonder if there is any swap function in R that does the following: > x <- seq(1,10,12)This just makes x equal to 1. I'm guessing you meant something like x <- 1:10> x1 <- swap(x) > x1 > 1 8 3 4 5 6 7 2 10It's not terribly clear what you want swap to do. You can reorder the elements of x using sample, e.g. sample(x) [1] 6 10 8 4 2 5 1 9 3 7 Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}
Hello Richie, I would like to do three (or k) swap steps in each step just 2 ID recursive swaping x <- 1:10 swap <- function(x){ a <- sample(x,2) x[x==a[1]] <- swap[2] x[x==a[2]] <- swap[1] return(x) } swap(swap(swap(x))) -> mix Is this possible? Thanks you in advance! Amor __________________________________________________ Schutz gegen Massenmails. [[alternative HTML version deleted]]
Is this what you want:> swap <- function(z){+ a <- sample(length(z), 2) + z[a] <- z[rev(a)] + z + }> swap(1:10)[1] 2 1 3 4 5 6 7 8 9 10> > swap(1:10)[1] 5 2 3 4 1 6 7 8 9 10> swap(1:10)[1] 8 2 3 4 5 6 7 1 9 10> swap(1:10)[1] 2 1 3 4 5 6 7 8 9 10> swap(1:10)[1] 4 2 3 1 5 6 7 8 9 10> swap(swap(swap(1:10)))[1] 8 10 3 5 4 6 7 1 9 2> swap(swap(swap(1:10)))[1] 8 2 7 4 5 6 3 10 9 1>On Fri, Aug 22, 2008 at 8:57 AM, amor Gandhi <amorigandhi at yahoo.de> wrote:> Hello Richie, > I would like to do three (or k) swap steps in each step just 2 ID recursive swaping > x <- 1:10 > swap <- function(x){ > a <- sample(x,2) > x[x==a[1]] <- swap[2] > x[x==a[2]] <- swap[1] > return(x) > } > swap(swap(swap(x))) -> mix > > Is this possible? > Thanks you in advance! > Amor > > __________________________________________________ > > > Schutz gegen Massenmails. > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help at 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. > >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
> Hello Richie, > I would like to do three (or k) swap steps in each step just 2 ID > recursive swaping > x <- 1:10 > swap <- function(x){ > a <- sample(x,2) > x[x==a[1]] <- swap[2] > x[x==a[2]] <- swap[1] > return(x) > } > swap(swap(swap(x))) -> mixI tried my best with a response before, but if you want a sensible answer you are going to have to try harder explaining what you really want. What do you mean by 'swap step'? If you want to swap the position of two elements in a vector (as I suspect you might) then which positions do you want swapping? Do you specify them yourself (as inputs to the swap function perhaps), or should they be randomly generated? If you provide more context (the rest of your code, what you are trying to achieve etc.) the help will be better. Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}