如何在R中一起执行vlookup和hlookup?

时间:2018-10-21 07:29:07

标签: r

我有一个这样的数据框:

interestRates <- c(0,0.005,0.015)
FXandGold <- c(0.01,0.05,0.075)
Equities <- c(0.06,0.08,0.1)
PreciousMetalsandGold <- c(0.07,0.07,0.08)
OtherCommodities <-c(0.1,0.12,0.15)
Timehorizon <- c("One year or less", "Over one year to five years", "Over five years")
Addondatatable<-data.frame(Timehorizon,interestRates,FXandGold,Equities,PreciousMetalsandGold,OtherCommodities)
Addondatatable
                  Timehorizon interestRates FXandGold Equities PreciousMetalsandGold OtherCommodities
1            One year or less         0.000     0.010     0.06                  0.07             0.10
2 Over one year to five years         0.005     0.050     0.08                  0.07             0.12
3             Over five years         0.015     0.075     0.10                  0.08             0.15

如何查找一年或以下且为股票的附加组件?因此该值应返回为0.06。

顺便说一句,有没有更好的方法来创建此映射表?

我是R的新手,需要有关这些的一些指导。

2 个答案:

答案 0 :(得分:0)

我建议您遵循p0bs的建议并阅读tidyverse。听起来您很在意:

library(tidyverse)
Addondatatable %>% filter(Timehorizon == "One year or less") %>% select(Equities)

答案 1 :(得分:0)

只需对表进行较小的修改,这很容易。不要让Timehorizo​​n成为一列,而应使其成为row.names。然后,您可以直接使用它来查询表。

Addondatatable<-data.frame(interestRates,FXandGold,Equities,
        PreciousMetalsandGold,OtherCommodities, row.names=Timehorizon)

Addondatatable["One year or less", "Equities"]
[1] 0.06