在 r 中返回具有匹配条件的值 [英] Return values with matching conditions in r
问题描述
我想根据切分标准在另一列中返回具有匹配条件的值.如果变量中的切分分数不可用,我想获取最接近的较大值.这是数据集的快照:
I would like to return values with matching conditions in another column based on a cut score criterion. If the cut scores are not available in the variable, I would like to grab closest larger value. Here is a snapshot of dataset:
ids <- c(1,2,3,4,5,6,7,8,9,10)
scores.a <- c(512,531,541,555,562,565,570,572,573,588)
scores.b <- c(12,13,14,15,16,17,18,19,20,21)
data <- data.frame(ids, scores.a, scores.b)
> data
ids scores.a scores.b
1 1 512 12
2 2 531 13
3 3 541 14
4 4 555 15
5 5 562 16
6 6 565 17
7 7 570 18
8 8 572 19
9 9 573 20
10 10 588 21
cuts <- c(531, 560, 571)
我想获取与第一个cut score对应的score.b
值,即13
.然后,抓取第二个cut(560
)分数对应的score.b值,但它不在score.a中,所以我想得到score.a值562
(最接近 560
),对应的值为 16
.最后,对于第三个切割分数 (571
),我想得到 19,这是与第三个切割分数最接近的值 (572
) 的对应值.
I would like to grab score.b
value corresponding to the first cut score, which is 13
. Then, grab score.b value corresponding to the second cut (560
) score but it is not in the score.a, so I would like to get the score.a value 562
(closest to 560
), and the corresponding value would be 16
. Lastly, for the third cut score (571
), I would like to get 19 which is the corresponding value of the closest value (572
) to the third cut score.
这是我想要的.
scores.b
cut.1 13
cut.2 16
cut.3 19
有什么想法吗?谢谢
推荐答案
我们可以使用滚动连接
library(data.table)
setDT(data)[data.table(cuts = cuts), .(ids = ids, cuts, scores.b),
on = .(scores.a = cuts), roll = -Inf]
# ids cuts scores.b
#1: 2 531 13
#2: 5 560 16
#3: 8 571 19
<小时>
或者另一个选项是 findInterval
从 base R
改变符号并取 rev
erse
Or another option is findInterval
from base R
after changing the sign and taking the rev
erse
with(data, scores.b[rev(nrow(data) + 1 - findInterval(rev(-cuts), rev(-scores.a)))])
#[1] 13 16 19
这篇关于在 r 中返回具有匹配条件的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!