You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

dplyr编程:动态变量与函数名实现升序/降序百分比排名

动态实现升序/降序的percent_rank计算

我希望通过动态命名变量与函数简化代码,实现与升序(ascending)、降序(descending)排序相关的percent_rank计算。目前可使用desc函数处理降序,但找不到升序的显式语句,需要将升序、降序的实现合并为统一的动态语句。

示例数据集

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))

动态传入变量实现升序百分比排名

current_var <- "a" # 动态变量名

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

动态传入变量实现降序百分比排名

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

合并为统一动态语句的解决方案

核心问题在于升序没有像desc那样的内置显式函数,我们可以自定义一个恒等函数作为升序的对应函数——因为升序排序本质就是直接使用变量本身:

# 自定义升序函数,直接返回输入值
asc <- function(x) x

这样就可以通过动态切换rank_function变量,实现升序或降序的percent_rank计算:

动态升序示例

rank_function <- asc
current_var <- "a"

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.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

动态降序示例

rank_function <- desc
current_var <- "a"

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

内容的提问来源于stack exchange,提问作者Sinh Nguyen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 22:57:21