[R] swap
Patrick Burns
pburns at pburns.seanet.com
Fri Aug 22 17:49:24 CEST 2008
I'm guessing that the following (untested)
does what is wanted:
function(x) {
pos <- sample(length(x), 2, replace=FALSE)
x[pos] <- x[ pos[2:1] ]
x
}
Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")
Richard.Cotton at hsl.gov.uk 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
>>
>
> I 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}}
>
> ______________________________________________
> 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.
>
>
>
More information about the R-help
mailing list