通过在分数表中查找,向表列添加值

时间:2017-01-03 10:44:01

标签: r lookup

我有一张平均成本为“costmel”的桌子 我有一张表,其平均成本的高低范围之间的风险评分为1-6。

aggProb_avCostMeld table:

          costmel
1         19.39
2         0.00
3         4889.71
4         4612.82
5         0.00
6         3812.52

RisicoKostSchaal table:

  Schaal low     high
1      1   0 1.75e+02
2      2 175 3.50e+02
3      3 350 5.25e+02
4      4 525 7.00e+02
5      5 700 8.75e+02
6      6 875 1.00e+08

我想做的是对于aggProb_avCostMeld $ costmel中的每一个, 检查它是否落在RisicoKostSchaal高低范围的y内并返回相应数量的RisicoKostSchaal $ Schaal并将其放入aggProb_avCostMeld表中的新列“costrisk”。

我尝试的代码是:

for(i in aggProb_avCostMeld$costmel) {
  for (y in RisicoKostSchaal ){
    if(aggProb_avCostMeld$costmel[i] >= RisicoKostSchaal$low[y] & aggProb_avCostMeld$costmel[i] < RisicoKostSchaal$high[y]){
  aggProb_avCostMeld$costrisk[i] <- RisicoKostSchaal$Schaal[y]
    }

  }
}

但那没用。

dput(aggProb_avCostMeld)
c(19.3984518231819, 0, 4889.71644908616, 4612.82921161826, 0, 
3812.52550091075, 0, 0, 0, 1484.24951267057, 0, 789.423206278027, 
2529.65298303229, 3600.89832689833)

dput(RisicoKostSchaal)
structure(list(Schaal = c(1, 2, 3, 4, 5, 6), low = c(0, 175, 
350, 525, 700, 875), high = c(175, 350, 525, 700, 875, 1e+08)), .Names = c("Schaal", 
"low", "high"), row.names = c(NA, -6L), class = "data.frame")

1 个答案:

答案 0 :(得分:3)

你可以:

<强>第一即可。定义一个函数,以便在特定数字落在Schaal范围内时获得RisicoKostSchaal c(low, high)

> checkInRange <- function(num) {
 RisicoKostSchaal$Schaal[which(RisicoKostSchaal$low <= num & RisicoKostSchaal$high >= num)]
 }

<强>第二即可。 此函数应用于aggProb_avCostMeld中的每个元素:

> aggProb_avCostMeld$costrisk  <- sapply(aggProb_avCostMeld$costmel, checkInRange)


> aggProb_avCostMeld
 #      costmel costrisk
 #1:   19.39845        1
 #2:    0.00000        1
 #3: 4889.71645        6
 #4: 4612.82921        6
 #5:    0.00000        1
 #6: 3812.52550        6
 #7:    0.00000        1
 #8:    0.00000        1
 #9:    0.00000        1
#10: 1484.24951        6
#11:    0.00000        1
#12:  789.42321        5
#13: 2529.65298        6
#14: 3600.89833        6