Skip to contents

This method sorts an object of class freqlist.

Usage

# S3 method for freqlist
sort(
  x,
  decreasing = FALSE,
  sort_crit = c("ranks", "names", "orig_ranks", "freqs"),
  na_last = TRUE,
  ...
)

Arguments

x

Object of class freqlist.

decreasing

Logical. If TRUE items are sorted from large to small; if FALSE, from small to large.

Note, however, that ranking in frequency lists is such that lower ranks correspond to higher frequencies. Therefore, sorting by rank (either "ranks" or "orig_ranks") with decreasing set to its default value FALSE results in the highest frequencies ending up at the beginning of the sorted list.

sort_crit

Character string determining the sorting criterion.

If sort_crit is "ranks", then the items in the frequency list are sorted by their current frequency rank.

If sort_crit is "names", then the items in the frequency list are sorted alphabetically their name.

If sort_crit is "orig_ranks", then the items in the frequency list are sorted by their original ranks (if those are present), or by their current frequency ranks (if no original ranks are present).

Finally, sorting with sort_crit set to "freqs" is identical to sorting by frequency ranks, but with the meaning of the argument decreasing being reversed. In other words, sorting by frequencies ("freqs") with decreasing set to its default value FALSE results in the lowest frequencies ending up at the beginning of the sorted list.

na_last

Logical defining the behavior of NA elements.

This argument is only relevant when sort_crit is "orig_ranks" because currently names and frequencies are not allowed to be NA in frequency lists.

If na_last is TRUE, then items with a sorting criterion of NA end up at the end of the sorted frequency list. If na_last is FALSE, then items with a sorting criterion of NA end up at the start of the sorted frequency list. If na_last is NA, then items with a sorting criterion of NA are removed from the sorted frequency list.

...

Additional arguments.

Value

Object of class freqlist.

Details

Because of the way ranks are calculated for ties (with lower ranks being assigned to ties earlier in the list), sorting the list may affect the ranks of ties. More specifically, ranks among ties may differ depending on the criterion that is used to sort the frequency list.

Examples

(flist <- freqlist(tokenize("the old story of the old man and the sea.")))
#> Frequency list (types in list: 7, tokens in list: 10)
#> rank  type abs_freq nrm_freq
#> ---- ----- -------- --------
#>    1   the        3     3000
#>    2   old        2     2000
#>    3   and        1     1000
#>    4   man        1     1000
#>    5    of        1     1000
#>    6   sea        1     1000
#>    7 story        1     1000
sort(flist)
#> Frequency list (types in list: 7, tokens in list: 10)
#> rank orig_rank  type abs_freq nrm_freq
#> ---- --------- ----- -------- --------
#>    1         1   the        3     3000
#>    2         2   old        2     2000
#>    3         3   and        1     1000
#>    4         4   man        1     1000
#>    5         5    of        1     1000
#>    6         6   sea        1     1000
#>    7         7 story        1     1000
sort(flist, decreasing = TRUE)
#> Frequency list (types in list: 7, tokens in list: 10)
#> rank orig_rank  type abs_freq nrm_freq
#> ---- --------- ----- -------- --------
#>    7         7 story        1     1000
#>    6         6   sea        1     1000
#>    5         5    of        1     1000
#>    4         4   man        1     1000
#>    3         3   and        1     1000
#>    2         2   old        2     2000
#>    1         1   the        3     3000