【发布时间】: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 也没有用。
谁能帮帮我?
非常感谢!
【问题讨论】: