【问题标题】:How can I get the rank percentile of every element in a numeric vector in r [duplicate]如何获得r中数字向量中每个元素的排名百分位数[重复]
【发布时间】:2021-01-04 22:34:32
【问题描述】:

我知道如何获得 25%、50% 和 75% 的百分位数。

set.seed(123)
a <- rnorm(100)
quantile(a)

如果我想知道a[13] 排名百分位数怎么办。

我见过类似的问题 [https://stackoverflow.com/questions/21219447/calculating-percentile-of-dataset-column] 但不是我想要的。

例如:

如果我想获得 13% 的百分位数,我可以使用这个:

quantile(a, prob = 0.13)

那我会得到

>      13% 
>-1.019541 

但这不是我想要的。我想得到我的向量a的百分位数。

比如我想得到a[13]的百分位数,也许函数应该是这样的:

get_percentile_value(a[13])

> 16.26%

那么我可以知道a[13](0.4007715) 总共排名16.26% a

有没有办法在 r 中做到这一点?

任何帮助将不胜感激!

【问题讨论】:

  • 您能解释一下 16.26% 的来源吗? rank() 或 dplyr 包装器(如 dplyr::percent_rank()dplyr::cume_dist())中的任何选项是否符合您的要求?
  • 请不要多次发布同一个问题,而且当它已经有答案时

标签: r


【解决方案1】:

您正在寻找的内容与empirical distribution function 模糊相关,尽管它并不完全符合要求,因为您不一定要查看概率分布本身(尽管您在您的示例中) .

无论如何,这里有一个简单的方法:



pctl = function(vector, value){
  
  out = sum(value >= vector)/ length(vector)
  
  return(out)
  
}

set.seed = 666

a = rnorm(100)
pctl(a, a[13])

>.85

它的作用是通过将逻辑向量强制转换为数字来总结测试值较大的值的数量,然后除以观察总数以获得百分比。

【讨论】:

    【解决方案2】:

    凌乱而低效的 Base R 解决方案(应该完全符合您的要求):

    get_percentile_value <- function(vec_w_idx){
      # Store argument as string: val => string scalar
      val <- deparse(substitute(vec_w_idx))
      # Extract the index from the argument: idx => integer scalar
      idx <- as.integer(gsub("(.*[[])(\\d+)[]]", "\\2", val))
      # Pull vector referenced in argument from Global Environment: 
      # vec => numeric vector
      vec <- eval(parse(text = gsub("(^\\w+)\\[.*", "\\1", val)))
      # Calculate the percentile rank of each value in the vector: 
      # pc_rnk => data.frame
      pc_rnk <- data.frame(srt_vec = sort(vec), pc_rnk = seq_along(vec)/length(vec))
      # Lookup the percentile rank and store it as a vector: res => data.frame
      res <- data.frame(vec = vec, pc_rnk = pc_rnk$pc_rnk[match(vec, pc_rnk$srt_vec)])
      # Return the percentile rank of the value at given index: 
      # double scalar => .GlobalEnv()
      return(res$pc_rnk[idx])
    }
    
    # Apply function: double scalar => stdout (console)
    get_percentile_value(a[14])
    

    或者如果输出必须完全符合您的要求:

    # Function to take a vector (with index provided), and return 
    # a percentile rank: get_percentile_value => function() 
    get_percentile_value <- function(vec_w_idx){
      # Store argument as string: val => string scalar
      val <- deparse(substitute(vec_w_idx))
      # Extract the index from the argument: idx => integer scalar
      idx <- as.integer(gsub("(.*[[])(\\d+)[]]", "\\2", val))
      # Pull vector referenced in argument from Global Environment: 
      # vec => numeric vector
      vec <- eval(parse(text = gsub("(^\\w+)\\[.*", "\\1", val)))
      # Calculate the percentile rank of each value in the vector: 
      # pc_rnk => data.frame
      pc_rnk <- data.frame(srt_vec = sort(vec), pc_rnk = seq_along(vec)/length(vec))
      # Lookup the percentile rank and store it as a vector: res => data.frame
      res <- data.frame(vec = vec, pc_rnk = pc_rnk$pc_rnk[match(vec, pc_rnk$srt_vec)])
      # Return the percentile rank of the value at given index: 
      # double scalar => .GlobalEnv()
      return(paste0(round(res$pc_rnk[idx] * 100, 4), "%"))
    }
    

    【讨论】:

      猜你喜欢
      • 2022-07-15
      • 2022-11-10
      • 2013-12-12
      • 2018-02-05
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 2019-10-04
      • 2011-12-01
      相关资源
      最近更新 更多