我有一个包含数千个期货合约代号的数据框。它们有缩写名称(稍后会出现)和全名(我希望在其他数据框中使用全名)。
full_list <- structure(
list(
Ticker = c("AC", "AIC", "BBS", "BO", "C", "DF"),
Long_Name = c("Ethanol -- CBOT", "DJ UBS Commodity Index -- CBOT", "South American Soybeans -- CBOT", "Soybean Oil -- CBT", "Corn -- CBT", "Dow Jones Industrial Average -- CBT")
),
.Names = c("Ticker", "Long_Name"),
row.names = c(NA, 6L),
class = "data.frame"
)
这个数据框包含我每天收到的列表。我需要去查找缩写名称并将其与全名匹配。
replace <- structure(
list(
Type = c("F", "F", "F", "F", "F", "F"),
Location = c("US", "US", "US", "US", "US", "US"),
Symbol = c("BO", "C", "DF", "AIC", "AC", "BBS"),
Month = c("V13", "U13", "U13", "U13", "U13", "U13")
),
.Names = c("Type", "Location", "Symbol", "Month"),
row.names = c(NA, 6L),
class = "data.frame"
)
我希望 R 能够替换 $Symbol 列,并在 full_list$Ticker 列中查找这些值,然后添加一列 replace$Long_Name,其中相应的 full_list$Long_Name 将被复制。希望这样说得清楚。我知道列名很难理解。
这在 Excel 中很容易实现 VLookup,但我已经快完成一个几乎每天都要用到的 R 脚本了。
merge(full_list, rbind(replace,transform(replace,Month="Z13")), by.x="Ticker", by.y="Symbol")
。 - Joshua Ulrich