Home > OS >  dplyr - programming dynamic variable & function name - ascending & descending
dplyr - programming dynamic variable & function name - ascending & descending

Time:08-17

I am trying to find way to shorten my code using dynamic naming variables & functions related with ascending & descending order. Though I can do desc but couldn't find anything for ascending. Below is the reproducible example to demonstrate my problem.

Here is the sample dataset

library(dplyr)

set.seed(100)
data <- tibble(a = runif(20, min = 0, max = 100),
  b = runif(20, min = 0, max = 100),
  c = runif(20, min = 0, max = 100))

Dynamically passing variable with percent rank in ascending order

current_var <- "a" # dynamic variable name

data %>%
  mutate("percent_rank_{current_var}" := percent_rank(!!sym(current_var)))
#> # A tibble: 20 × 4
#>        a     b     c percent_rank_a
#>    <dbl> <dbl> <dbl>          <dbl>
#>  1 30.8   53.6  33.1         0.263 
#>  2 25.8   71.1  86.5         0.158 
#>  3 55.2   53.8  77.8         0.684 
#>  4  5.64  74.9  82.7         0     
#>  5 46.9   42.0  60.3         0.526 
#>  6 48.4   17.1  49.1         0.579 
#>  7 81.2   77.0  78.0         0.947 
#>  8 37.0   88.2  88.4         0.421 
#>  9 54.7   54.9  20.8         0.632 
#> 10 17.0   27.8  30.7         0.0526
#> 11 62.5   48.8  33.1         0.737 
#> 12 88.2   92.9  19.9         1     
#> 13 28.0   34.9  23.6         0.211 
#> 14 39.8   95.4  27.5         0.474 
#> 15 76.3   69.5  59.1         0.895 
#> 16 66.9   88.9  25.3         0.789 
#> 17 20.5   18.0  12.3         0.105 
#> 18 35.8   62.9  23.0         0.316 
#> 19 35.9   99.0  59.8         0.368 
#> 20 69.0   13.0  21.1         0.842

Dynamically passing variable with percent rank in descending order

data %>%
  mutate("percent_rank_{current_var}" := percent_rank(desc(!!sym(current_var))))
#> # A tibble: 20 × 4
#>        a     b     c percent_rank_a
#>    <dbl> <dbl> <dbl>          <dbl>
#>  1 30.8   53.6  33.1         0.737 
#>  2 25.8   71.1  86.5         0.842 
#>  3 55.2   53.8  77.8         0.316 
#>  4  5.64  74.9  82.7         1     
#>  5 46.9   42.0  60.3         0.474 
#>  6 48.4   17.1  49.1         0.421 
#>  7 81.2   77.0  78.0         0.0526
#>  8 37.0   88.2  88.4         0.579 
#>  9 54.7   54.9  20.8         0.368 
#> 10 17.0   27.8  30.7         0.947 
#> 11 62.5   48.8  33.1         0.263 
#> 12 88.2   92.9  19.9         0     
#> 13 28.0   34.9  23.6         0.789 
#> 14 39.8   95.4  27.5         0.526 
#> 15 76.3   69.5  59.1         0.105 
#> 16 66.9   88.9  25.3         0.211 
#> 17 20.5   18.0  12.3         0.895 
#> 18 35.8   62.9  23.0         0.684 
#> 19 35.9   99.0  59.8         0.632 
#> 20 69.0   13.0  21.1         0.158

How to combine both into one statement? - I can do for desc but couldn't find any explicit statement for ascending order

rank_function <- desc # dynamic function for ranking
data %>%
  mutate("percent_rank_{current_var}" := percent_rank(rank_function(!!sym(current_var))))
#> # A tibble: 20 × 4
#>        a     b     c percent_rank_a
#>    <dbl> <dbl> <dbl>          <dbl>
#>  1 30.8   53.6  33.1         0.737 
#>  2 25.8   71.1  86.5         0.842 
#>  3 55.2   53.8  77.8         0.316 
#>  4  5.64  74.9  82.7         1     
#>  5 46.9   42.0  60.3         0.474 
#>  6 48.4   17.1  49.1         0.421 
#>  7 81.2   77.0  78.0         0.0526
#>  8 37.0   88.2  88.4         0.579 
#>  9 54.7   54.9  20.8         0.368 
#> 10 17.0   27.8  30.7         0.947 
#> 11 62.5   48.8  33.1         0.263 
#> 12 88.2   92.9  19.9         0     
#> 13 28.0   34.9  23.6         0.789 
#> 14 39.8   95.4  27.5         0.526 
#> 15 76.3   69.5  59.1         0.105 
#> 16 66.9   88.9  25.3         0.211 
#> 17 20.5   18.0  12.3         0.895 
#> 18 35.8   62.9  23.0         0.684 
#> 19 35.9   99.0  59.8         0.632 
#> 20 69.0   13.0  21.1         0.158

Created on 2022-08-17 by the reprex package (v2.0.1)

CodePudding user response:

You could compose a function to return its input:

rank_function <- function(x) x

Actually this function has been defined in base, i.e. identity.

rank_function <- identity

Also, you can explore the source code of desc:

desc

function (x) -xtfrm(x)

Apparently desc is just the opposite number of xtfrm. So you can use it for ascending ordering.

rank_function <- xtfrm

In the help document of xtfrm(x):

A generic auxiliary function that produces a numeric vector which will sort in the same order as x.

  • Related