This function takes a vector with cluster labels and sorts it by frequency such that the most frequent cluster is the first one and so on.

sort_clusters(clusters, map_subset = NULL)

Arguments

clusters

A vector with cluster labels.

map_subset

A logical vector of length equal to clusters specifying which elements of clusters to use to determine the ranking of the clusters.

Value

A factor of length equal to clusters where the levels are the new ordered clusters and the names of the factor are the original values from clusters.

Examples


## Build an initial set of cluster labels
clus <- letters[unlist(lapply(4:1, function(x) rep(x, x)))]

## In this case, it's a character vector
class(clus)
#> [1] "character"

## Sort them and obtain a factor
sort_clusters(clus)
#> d d d d c c c b b a 
#> 1 1 1 1 2 2 2 3 3 4 
#> Levels: 1 2 3 4