查找下一个更小和下一个更大的值
Find next smaller and next larger values
我有两个向量:
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
然后 return 在 smaller_array 中值“100”
并将其保存在 result_vector 位置 64
我试过了(return得到了无用的结果); match.closest-来自 MALDIquant-Package 的函数:
> 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 它也没有用。
谁能帮帮我?
非常感谢!
尝试:
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
使用相同的 purrr
library(purrr)
map_dbl(moneyness_cert, ~ smaller_array[which.min(abs(smaller_array - .x))])
我有两个向量:
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
然后 return 在 smaller_array 中值“100”
并将其保存在 result_vector 位置 64
我试过了(return得到了无用的结果); match.closest-来自 MALDIquant-Package 的函数:
> 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 它也没有用。
谁能帮帮我?
非常感谢!
尝试:
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
使用相同的 purrr
library(purrr)
map_dbl(moneyness_cert, ~ smaller_array[which.min(abs(smaller_array - .x))])