swap | R Documentation |
Exchange object x
with object y
.
swap(x, y)
x |
an R object |
y |
an R object |
none.
J.R. Lobry
citation("seqinr")
move
#
# Example in a new empty environment:
#
local({
x <- 0:9
y <- 10:19
print(x)
print(y)
swap(x[1], y[2])
print(x)
print(y)
})
#
# Sanity check with a bubble sort:
#
bubble.sort <- function(tab, n = length(tab)){
i <- 1
while(i < n){
if(tab[i + 1] < tab[i]){
swap(tab[i], tab[i+1])
i <- 1
} else {
i <- i+1
}
}
return(tab)
}
set.seed(1)
x <- rnorm(10)
stopifnot(identical(sort(x), bubble.sort(x)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.