gpt4 book ai didi

r - 在 R 中,如何根据包含我想要匹配的值的相邻列中的间隔查找一列中的值?

转载 作者:行者123 更新时间:2023-12-02 20:00:29 24 4
gpt4 key购买 nike

在 R 中,我有一个包含三列的引用表(数据框)。下面是一个例子:

reftable <- data.frame(
X_lower = c(0, 101, 181, 231, 280, 300, 340, 390, 500),
X_upper = c(100, 180, 230, 279, 299, 339, 389, 499, 600),
Percentile = c(2, 3, 4, 6, 8, 11, 15, 20, 25))

# X_lower X_upper Percentile
# 0 100 2
# 101 180 3
# 181 230 4
# etc.

我有一个单独的数据框,scores,其中包含 X 的特定值,并且我想使用引用表来查找与每个值关联的百分位数排名。

scores <- data.frame(
X = c(58, 127, 175, 245, 300, 90, 405, 284, 330),
PercRank = NA))

# X PercRank
# 58 ?
# 127 ?
# 175 ?
# 245 ?
# etc.

我尝试过使用 match 或 findInterval 但找不到解决方案。我已经搜索了现有的问题。如果以前有人问过这个问题,那么我肯定没有找到正确的搜索词。

最佳答案

您可以尝试:

scores$PercRank=sapply(scores$X,function(x){
i = which(reftable$X_upper>x)[1]
reftable$Percentile[i]
})

> scores
X PercRank
1 58 2
2 127 3
3 175 3
4 245 6
5 300 11
6 90 2
7 405 20
8 284 8
9 330 11

因为reftable是有序的,所以你只需要检查第一个比你的X大的上限值。

关于r - 在 R 中,如何根据包含我想要匹配的值的相邻列中的间隔查找一列中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56026522/

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