我有一个参考表,显示奖金额,因为完成百分位数小于或等于百分位数X.我想加入一个列出实际百分位数的表来获得奖品,但我不知道如何通过排序数据加入或合并。我想要的例子:
Percentile <- c(.01, .1, .2, .3, .4, .5, 1)
Prize <- c(1000, 100, 25, 6, 3, 2, 0)
refDF <- data.frame(Percentile, Prize)
Person <- c(1, 2, 3, 4)
Percentile <- c(.005, .385, .72, .20)
resultDF <- data.frame(Person, Percentile)
MADEUPLEFTJOIN(resultdf, refDF, by = c('Percentile'), sorted = T)
Person Percentile Prize
1 1 0.005 1000
2 2 0.385 3
3 3 0.720 0
4 4 0.200 25
我以前习惯使用dplyr::left_join
,但我查看了文档,无法找到我正在寻找的内容。
答案 0 :(得分:0)
使用sqldf
的一个解决方案可以是:
library(sqldf)
sqldf("SELECT resultDF.*, refDF.Prize
FROM resultDF, refDF
WHERE refDF.Percentile = (SELECT min(refDF.Percentile) FROM refDF
WHERE refDF.Percentile >= resultDF.Percentile)")
Person Percentile Prize
1 1 0.005 1000
2 2 0.385 3
3 3 0.720 0
4 4 0.200 25
答案 1 :(得分:0)
您也可以使用baser R:
resultDF$Prize=refDF$Prize[sapply(resultDF$Percentile,function(x) which(x<=refDF$Percentile)[1])]
> resultDF
Person Percentile Prize
1 1 0.005 1000
2 2 0.385 3
3 3 0.720 0
4 4 0.200 25