kinda_sort: Kinda sort

kinda_sortR Documentation

Kinda sort

Description

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.

Usage

kinda_sort(x, n, decreasing = FALSE, indices = NULL, index.return = FALSE)

Arguments

x

a numeric, complex, character, or logical vector

n

number of elements of x to remain unsorted (the default is approximately 10% of x), ignored if indices is given

decreasing

logical; if FALSE (default), x is sorted in increasing order

indices

a vector of indices specifying which elements of x should not be sorted

index.return

logical; if TRUE, the ordering index vector is returned

Value

x sorted approximately (length(x) - n)/length(x)*100 percent.

See Also

sort2; sym_sort

Examples

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))


raredd/rawr documentation built on May 19, 2024, 1:02 p.m.