Skip to contents

Rank the values of numeric variables, for example, in descending order, and then returns the result along with tidyverse code used to generate it. See row_number and percent_rank.

Usage

rank_vars(data, vars, rank_type = c("min", "dense", "percent"))

Arguments

data

a dataframe with the variables to rank

vars

a character vector of numeric variables in data to rank

rank_type

either "min", "dense" or "percent", see row_number, percent_rank

Value

the original dataframe containing new columns with the ranks of the variables in vars with tidyverse code attached

See also

Author

Zhaoming Su

Examples

ranked <- rank_vars(iris, vars = c("Sepal.Length", "Petal.Length"))
cat(code(ranked))
#> iris |> dplyr::mutate(Sepal.Length.min_rank = dplyr::min_rank(Sepal.Length), .after = Sepal.Length) |> dplyr::mutate(Petal.Length.min_rank = dplyr::min_rank(   Petal.Length), .after = Petal.Length)
head(ranked)
#>   Sepal.Length Sepal.Length.min_rank Sepal.Width Petal.Length
#> 1          5.1                    33         3.5          1.4
#> 2          4.9                    17         3.0          1.4
#> 3          4.7                    10         3.2          1.3
#> 4          4.6                     6         3.1          1.5
#> 5          5.0                    23         3.6          1.4
#> 6          5.4                    47         3.9          1.7
#>   Petal.Length.min_rank Petal.Width Species
#> 1                    12         0.2  setosa
#> 2                    12         0.2  setosa
#> 3                     5         0.2  setosa
#> 4                    25         0.2  setosa
#> 5                    12         0.2  setosa
#> 6                    45         0.4  setosa