根据已知机构(标准普尔、穆迪和惠誉)的评级信息,我如何设置债券的最低评级?

我有一个带有相应数值的评分表:

#define rating scale table
RTG_table <- data.frame(SP_FITCH = c("AAA", "AA+", "AA", "AA-", "A+", "A", "A-", "BBB+", "BBB", "BBB-",
                                  "BB+", "BB", "BB-", "B+", "B", "B-", "CCC+", "CCC", "CCC-", "CC", "C"),
                        MOODY = c("Aaa", "Aa1", "Aa2", "Aa3", "A1", "A2", "A3", "Baa1", "Baa2", "Baa3",
                              "Ba1", "Ba2", "Ba3", "B1", "B2", "B3", "Caa1", "Caa2", "Caa3", "Ca", "C"),
                        VALUE = c(seq(1, 20), 22), stringsAsFactors = FALSE)

我的数据框将是这样的:
Corp <- data.frame(SECURITY_NAME = LETTERS[seq(1, 6)],
               RTG_SP_NO_WATCH = c("AA+", "A-", "BBB-", "AAA", "B-", "AA"),
               RTG_MOODY_NO_WATCH = c("Aa2", "A2", "Baa3", "Aaa", "B2", "Aa2"),
               RTG_FITCH_NO_WATCH = c("AA+", "BBB+", "BBB-", "AA+", "B-", "AA-"))

结果应该是:
Corp_MIN_RTG <- data.frame(SECURITY_NAME = LETTERS[seq(1, 6)],
               RTG_SP_NO_WATCH = c("AA+", "A-", "BBB-", "AAA", "B-", "AA"),
               RTG_MOODY_NO_WATCH = c("Aa2", "A2", "Baa3", "Aaa", "B2", "Aa2"),
               RTG_FITCH_NO_WATCH = c("AA+", "BBB+", "BBB-", "AA+", "B-", "AA-"),
               MIN_RTG = c("Aa2", "BBB+", "BBB-", "AA+", "B-", "AA-"))

> Corp_MIN_RTG
 SECURITY_NAME RTG_SP_NO_WATCH RTG_MOODY_NO_WATCH RTG_FITCH_NO_WATCH MIN_RTG
1             A             AA+                Aa2                AA+     Aa2
2             B              A-                 A2               BBB+    BBB+
3             C            BBB-               Baa3               BBB-    BBB-
4             D             AAA                Aaa                AA+     AA+
5             E              B-                 B2                 B-      B-
6             F              AA                Aa2                AA-     AA-
>

为了到达那里,我需要提取所有 3 个评分的最高数值,并用 RTG_table 中的相应评分替换该值。

希望我的例子很清楚。

任何帮助深表感谢!

最佳答案

您可以按以下方式处理此问题:

library(data.table)
# convert the dataframes into datatables and melt them into long format
corp2 <- melt(setDT(Corp), id="SECURITY_NAME")
rtg2 <- melt(setDT(RTG_table), id="VALUE", value.name = "rtgval")

# join them together and calculate the minimum rating for each 'SECURITY_NAME'
# and set those values in a new look-up data.table
DT <- corp2[rtg2, on=c("value"="rtgval"), nomatch=0
            ][, .(min_rtg = value[which.max(VALUE)]), by = SECURITY_NAME]

# join the original 'Corp' data.table with the look-up data.table
Corp <- Corp[DT, on=c("SECURITY_NAME")][order(SECURITY_NAME)]

这使:
> Corp
   SECURITY_NAME RTG_SP_NO_WATCH RTG_MOODY_NO_WATCH RTG_FITCH_NO_WATCH min_rtg
1:             A             AA+                Aa2                AA+     Aa2
2:             B              A-                 A2               BBB+    BBB+
3:             C            BBB-               Baa3               BBB-    BBB-
4:             D             AAA                Aaa                AA+     AA+
5:             E              B-                 B2                 B-      B-
6:             F              AA                Aa2                AA-     AA-

关于r - 在 R 中设置债券的最低评级,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34419738/

10-12 17:10