Takes a numeric vector and sort its values so that they would be increasing from left to right. It is different from sort in that the function will only "sort" the values levels, and not the vector itself.

This function is useful for cutree - making the sort_cluster_numbers parameter possible. Using that parameter with TRUE makes the clusters id's from cutree to be ordered from left to right. e.g: the left most cluster in the tree will be numbered "1", the one after it will be "2" etc...).

sort_levels_values(
  x,
  MARGIN = 2,
  decreasing = FALSE,
  force_integer = FALSE,
  warn = dendextend_options("warn"),
  ...
)

Arguments

x

a numeric vector.

MARGIN

passed to apply. It is a vector giving the subscripts which the function will be applied over. E.g., for a matrix 1 indicates rows, 2 indicates columns, c(1, 2) indicates rows and columns. Where X has named dimnames, it can be a character vector selecting dimension names.

decreasing

logical (FALSE). Should the sort be increasing or decreasing?

force_integer

logical (FALSE). Should the values returned be integers?

warn

logical (default from dendextend_options("warn") is FALSE). Set if warning are to be issued, it is safer to keep this at TRUE, but for keeping the noise down, the default is FALSE. (for example when x had NA values in it)

...

ignored.

Value

if x is an object - it returns logical - is the object of class dendrogram.

See also

Examples


x <- 1:4
sort_levels_values(x) # 1 2 3 4
#> [1] 1 2 3 4

x <- c(4:1)
names(x) <- letters[x]
attr(x, "keep_me") <- "a cat"
sort_levels_values(x) # 1 2 3 4
#> d c b a 
#> 1 2 3 4 
#> attr(,"keep_me")
#> [1] "a cat"

x <- c(4:1, 4, 2)
sort_levels_values(x) # 1 2 3 4 1 3
#> [1] 1 2 3 4 1 3

x <- c(2, 2, 3, 2, 1)
sort_levels_values(x) # 1 1 2 1 3
#> [1] 1 1 2 1 3

x <- matrix(16:1, 4, 4)
rownames(x) <- letters[1:4]
x
#>   [,1] [,2] [,3] [,4]
#> a   16   12    8    4
#> b   15   11    7    3
#> c   14   10    6    2
#> d   13    9    5    1
apply(x, 2, sort_levels_values)
#>   [,1] [,2] [,3] [,4]
#> a   13    9    5    1
#> b   14   10    6    2
#> c   15   11    7    3
#> d   16   12    8    4