gpt4 book ai didi

舍入数字并保留足够的有效数字以将它们与邻居区分开来

转载 作者:行者123 更新时间:2023-12-04 12:20:06 25 4
gpt4 key购买 nike

我有一个数字递增的向量,例如以下一个:

set.seed(1)
numbers <- cumsum(abs(rnorm(10,100,100)))
# [1] 37.35462 155.71895 172.15609 431.68417 564.63495 582.58811 731.33101 905.16348 1062.74162 1132.20278

我想选择最少数量的有效数字,然后对这些数字进行四舍五入,确保我始终保留足够的数字,这样连续的数字就不会四舍五入为相同的值。

请参阅以下示例(预期输出):
magic(numbers, n = 1)
# [1] 40 160 170 400 560 580 700 900 1060 1130
  • 37.35462舍入为 40因为我在可能的情况下只要求一位数字 ( n = 1 )
  • 我不能圆 155.71895200因为 172.15609将四舍五入为 200同样的规则,所以我舍入 155.71895160 , 和 172.15609170
  • 我可以安全地绕过 431.68417400因为它离 172.15609 足够远了和 564.63495

  • 等等...

    对于 n = 2 或 3,我们将得到:
    magic(numbers, n = 2)
    # [1] 37 160 170 430 560 580 730 910 1060 1130

    magic(numbers, n = 3)
    # [1] 37.4 156 172 432 565 583 731 905 1060 1130

    我的目标是获得非线性分布的分位数的可读值。

    最佳答案

    #' Minimum preferred significant digits
    #'
    #' @details
    #' Facilitate reducing numbers to their least *distinguishable*
    #' significant digits, where "distinguishable" means
    #' "between neighbors". This means that if reducing more digits would
    #' cause two neighbors to reduce to the same number, then the
    #' reduction cannot take place.
    #'
    #' References:
    #'
    #' - [Original question on StackOverflow](https://stackoverflow.com/q/51616332/3358272) (and [my answer](https://stackoverflow.com/a/51617325/3358272))
    #'
    #' @param numbers numeric, length 2 or more
    #' @param n integer, number of preferred remaining significant digits
    #' @return numeric vector
    #' @export
    #' @md
    #' @examples
    #' \dontrun{
    #' set.seed(1)
    #' numbers <- cumsum(abs(rnorm(10,100,100)))
    #' # [1] 37.35462 155.71895 172.15609 431.68417 564.63495 582.58811 731.33101 905.16348 1062.74162 1132.20278
    #' magic(numbers, 1)
    #' # [1] 40 160 170 400 560 580 700 900 1060 1130
    #' magic(numbers, 2)
    #' # [1] 37 160 170 430 560 580 730 910 1060 1130
    #' magic(numbers, 3)
    #' # [1] 37.4 156.0 172.0 432.0 565.0 583.0 731.0 905.0 1060.0 1130.0
    #' magic(c(1,2.4,2.6,4),1)
    #' # [1] 1 2 3 4
    #' }
    magic <- function(numbers, n=1L) {
    stopifnot(length(numbers) > 1L)
    logscale <- ceiling(log10(abs(numbers)))
    logdiff <- log10(diff(numbers))
    keepoom <- floor(pmin(c(Inf, logdiff), c(logdiff, Inf)))
    roundpoints <- 5*(10^keepoom)
    out <- signif(numbers, pmax(n, logscale - (1+keepoom)))
    dupes <- duplicated(out)
    if (any(dupes)) {
    dupes <- dupes | c(dupes[-1], FALSE)
    out2 <- signif(numbers, pmax(n, logscale - keepoom))
    out[dupes] <- out2[dupes]
    }
    out
    }

    示例用法:
    magic(numbers, 1)
    # [1] 40 160 170 400 560 580 700 900 1060 1130
    ## [1] 40 160 170 400 560 580 700 900 1060 1130 # yours
    magic(numbers, 2)
    # [1] 37 160 170 430 560 580 730 910 1060 1130
    ## [1] 37 160 170 430 560 580 730 910 1060 1130 # yours
    magic(numbers, 3)
    # [1] 37.4 156.0 172.0 432.0 565.0 583.0 731.0 905.0 1060.0 1130.0
    ## [1] 37.4 156 172 432 565 583 731 905 1060 1130 # yours
    magic(c(1,2.4,2.6,4),1)
    # [1] 1 2 3 4
    ## [1] 1:4 # yours, from comments

    关于舍入数字并保留足够的有效数字以将它们与邻居区分开来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51616332/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com