kinda_sort | R Documentation |
sort
a vector but not very well.
For a vector, x
, n
elements will be randomly selected, and
their positions will remain unchanged as all other elements are sorted.
Alternatively, a vector of indices
of x
can be given and
will remain unsorted.
kinda_sort(x, n, decreasing = FALSE, indices = NULL, index.return = FALSE)
x |
a numeric, complex, character, or logical vector |
n |
number of elements of x to remain unsorted (the default is
approximately 10% of |
decreasing |
logical; if |
indices |
a vector of indices specifying which elements of |
index.return |
logical; if |
x
sorted approximately (length(x) - n)/length(x)*100
percent.
sort2
; sym_sort
set.seed(1)
x <- sample(1:12)
rbind(
unsorted = x,
'50% sort' = kinda_sort(x, n = 5),
'fix 2:5' = kinda_sort(x, indices = 2:5)
)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
# unsorted 9 4 7 1 2 5 3 8 6 11 12 10
# 50% sort 9 1 4 6 2 5 3 7 8 11 10 12
# fix 2:5 3 4 7 1 2 5 6 8 9 10 11 12
## use index.return = TRUE for indices instead of values
set.seed(1)
x <- runif(100)
o1 <- kinda_sort(x, n = 50, index.return = TRUE)
set.seed(1)
x <- runif(100)
o2 <- kinda_sort(x, n = 50)
stopifnot(identical(x[o1], o2))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.