Rank provides a customizable alternative to the built-in rank()
function. The package offers the following features:
Frequency-based ranking of categorical variables: choose whether to rank based on alphabetic order or element frequency.
Control over sorting order: Use
desc=TRUE
to rank based on descending or ascending order.
Installation
To install rank from CRAN run:
install.packages("rank")
You can install the development version of rank like so:
# install.packages('remotes')
remotes::install_github("selkamand/rank")
Usage
library(rank)
fruits <- c("Apple", "Orange", "Apple", "Pear", "Orange")
## CATEGORICAL INPUT -----------------------
# rank alphabetically
smartrank(fruits)
#> [1] 1.5 3.5 1.5 5.0 3.5
# rank based on frequency
smartrank(fruits, sort_by = "frequency")
#> smartrank: Sorting a categorical variable by frequency: ignoring ties.method
#> [1] 2 3 2 1 3
# rank based on descending order of frequency
smartrank(fruits,sort_by = "frequency", desc = TRUE)
#> smartrank: Sorting a categorical variable by frequency: ignoring ties.method
#> [1] 1 2 1 3 2