Home > Mobile >  Creating multiple rank columns dplyr
Creating multiple rank columns dplyr

Time:07-21

I have a df with several columns, and I'd like to rank them. I can do them one at a time like this:

iris.ranked <-
  iris %>% 
  arrange(Sepal.Length) %>% 
  mutate(Sepal.Length = rank(Sepal.Length))

But there are lots of columns...and this is clunky. I'd rather feed a list of columns and rank them all in one code chunk. I was thinking something like this but not working...

iris.ranked.all <-
  iris %>% 
  mutate_at(
    c('Sepal.Length',
      'Sepal.Width',
      'Petal.Width',
      'Petal.Length'),
    function(x) arrange(x) %>% rank()
  )

CodePudding user response:

Use mutate(across()) from dplyr:

library(dplyr)
iris  |>
    mutate(
        across(
            Sepal.Length:Petal.Width, 
            rank, 
            .names = "rank_{.col}")
    )
# # A tibble: 150 x 9
#    Sepal.Length Sepal.Width Petal.Length Petal.Width Species rank_Sepal.Length rank_Sepal.Width rank_Petal.Length rank_Petal.Width
#           <dbl>       <dbl>        <dbl>       <dbl> <fct>               <dbl>            <dbl>             <dbl>            <dbl>
#  1          5.1         3.5          1.4         0.2 setosa               37              128.               18                 20
#  2          4.9         3            1.4         0.2 setosa               19.5             70.5              18                 20
#  3          4.7         3.2          1.3         0.2 setosa               10.5            101                 8                 20
#  4          4.6         3.1          1.5         0.2 setosa                7.5             89                31                 20
#  5          5           3.6          1.4         0.2 setosa               27.5            134.               18                 20
#  6          5.4         3.9          1.7         0.4 setosa               49.5            146.               46.5               45
#  7          4.6         3.4          1.4         0.3 setosa                7.5            120.               18                 38
#  8          5           3.4          1.5         0.2 setosa               27.5            120.               31                 20
#  9          4.4         2.9          1.4         0.2 setosa                3               52.5              18                 20
# 10          4.9         3.1          1.5         0.1 setosa               19.5             89                31                  3
# # ... with 140 more rows

Or if in fact you want to overwrite the columns as your question suggests, omit the .names argument:

iris  |>
    mutate(
        across(
            Sepal.Length:Petal.Width, 
            rank)
    )
  • Related