【问题标题】:Find next smaller and next larger values查找下一个较小和下一个较大的值
【发布时间】:2020-03-23 16:35:54
【问题描述】:

我有两个向量:

smaller_array <- c(50, 60, 70, 75, 80, 85, 90, 95, 100, 105)

moneyness_cert <- c(105.8138,   105.7155,   105.4637,   104.5942,   105.0757,   105.316,    104.641,     
                    105.0637,   105.461,    104.971,    105.2471,   105.1348,   105.638,    105.8024,                                                               
                    105.592,    104.9338,   105.0133,   104.613,    104.9407,   105.0136,   107.2144,    
                    107.0112,   105.7793,   106.4742,   105.5703,   106.0615,   106.3446,   105.7296,    
                    105.1307,   104.6472,   103.6721,   104.607,    105.1265,   105.2077,   104.363,     
                    104.5036,   104.2205,   104.9135,   103.8404,   105.1506,   105.8887,   105.0894,    
                    104.3529,   103.0007,   103.0904,   103.334,    103.2959,   103.4819,   103.504,     
                    102.7641,   102.5911,   102.5386,   102.843,    103.8211,   102.3814,   105.265,     
                    104.3255,   104.1589,   105.6462,   107.0716,   106.5527,   104.655,    103.1285,    
                    102.3955,   102.8577) #length of vector is 65

我想为每个moneyness_cert 值找到在smaller_array 中最接近它的值。 找到的值应保存在向量中(例如“result_vector”)

moneyness_cert 中第 64 个元素的示例:
moneyness_cert = 102.3955

然后在较小的数组中返回值“100”
并将其保存在 64 位的 result_vector 中

我试过了(它返回了无用的结果);来自 MALDIquant-Package 的 match.closest-function:

>     match.closest(x = moneyness_cert, table = sort(smaller_array, decreasing = F), tolerance = Inf, nomatch = NA)
 [1] 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
[26] 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
[51] 10 10 10 10  9 10 10 10 10 10 10 10 10  9 10

另一个尝试是:

> apply(smaller_array, 1 , function(x) moneyness_cert - x)
Error in apply(smaller_array, 1, function(x) moneyness_cert - x) : 
  dim(X) must have a positive length

通过 lapply 也没有用。

谁能帮帮我?

非常感谢!

【问题讨论】:

    标签: r numbers next closest


    【解决方案1】:

    试试:

    sapply(moneyness_cert, function(x) smaller_array[which.min(abs(smaller_array - x))])
    

    输出:

    [1] 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105
    [34] 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 100 105 105 105 105 105 105 105 105 100 105
    

    或者直接为第64个元素:

    sapply(moneyness_cert, function(x) smaller_array[which.min(abs(smaller_array - x))])[64]
    
    # [1] 100
    

    【讨论】:

    • 嗨 arg0naut91,如果我尝试运行你的函数,它会返回错误:``` > money_closest_smaller -.default(smaller_array, x) 中:较长的对象长度不是较短对象长度的倍数。```
    • 嗯,奇怪 - 它适用于您的示例 - 您可以重新检查您发布的示例是否与您正在使用的示例相对应吗?
    • moneyness_cert 被格式化为 xts 元素。我将它转换为数字,现在它可以工作了:)。感谢您的帮助!巴伐利亚最佳。
    【解决方案2】:

    使用相同的purrr

    library(purrr)
    map_dbl(moneyness_cert, ~ smaller_array[which.min(abs(smaller_array - .x))])
    

    【讨论】:

      猜你喜欢
      • 2020-07-30
      • 2018-11-08
      • 2012-09-23
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 2011-10-13
      • 2019-10-11
      相关资源
      最近更新 更多