在R中的VLookup类型方法

8

我有一个包含数千个期货合约代号的数据框。它们有缩写名称(稍后会出现)和全名(我希望在其他数据框中使用全名)。

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 脚本了。
5个回答

16

合并它们:

> merge(full_list, replace, by.x="Ticker", by.y="Symbol")
  Ticker                           Long_Name Type Location Month
1     AC                     Ethanol -- CBOT    F       US   U13
2    AIC      DJ UBS Commodity Index -- CBOT    F       US   U13
3    BBS     South American Soybeans -- CBOT    F       US   U13
4     BO                  Soybean Oil -- CBT    F       US   V13
5      C                         Corn -- CBT    F       US   U13
6     DF Dow Jones Industrial Average -- CBT    F       US   U13

如果有重复的“股票代码”,这个方法还能正常工作吗?它会多次使用“long_name”吗? - Tim
@Tim:是的:merge(full_list, rbind(replace,transform(replace,Month="Z13")), by.x="Ticker", by.y="Symbol") - Joshua Ulrich

10
你可以使用match方法,它会返回第一个参数在第二个参数中的位置索引。例如:

You could use match - which gives the index of where the first argument falls in the second argument. For example:

arg1 <- c("red","blue")
arg2 <- c("blue","red")

> match(arg1,arg2)
[1] 2 1

那么在您的替换数据框中创建一个新列(注意 - 您应该为其命名其他内容,因为replace实际上是r中的一个函数),使用匹配符号的full_list数据框即可。

replace$Long_Name <- full_list$Long_Name[match(replace$Symbol,full_list$Ticker)]

> replace
  Type Location Symbol Month                           Long_Name
1    F       US     BO   V13                  Soybean Oil -- CBT
2    F       US      C   U13                         Corn -- CBT
3    F       US     DF   U13 Dow Jones Industrial Average -- CBT
4    F       US    AIC   U13      DJ UBS Commodity Index -- CBOT
5    F       US     AC   U13                     Ethanol -- CBOT
6    F       US    BBS   U13     South American Soybeans -- CBOT

能否根据两个条件进行匹配?比如,如果“Long_Name”随着年份的变化而变化,是否可以在“match”中添加一个“$year”参数? - Rafael
1
@RafaelMartins 我会推荐使用 data.table 包。你可以轻松高效地在多列键上进行合并。你也可以使用 merge 并提供一个列名向量给 'by' 参数。参见:https://dev59.com/pGw15IYBdhLWcg3wSJzQ - dayne

6

如果是一个大数据集,您可能会从环境查找中受益:

library(qdap)
replace$Long_Name <- lookup(replace$Symbol, full_list)

## > replace
##   Type Location Symbol Month                           Long_Name
## 1    F       US     BO   V13                  Soybean Oil -- CBT
## 2    F       US      C   U13                         Corn -- CBT
## 3    F       US     DF   U13 Dow Jones Industrial Average -- CBT
## 4    F       US    AIC   U13      DJ UBS Commodity Index -- CBOT
## 5    F       US     AC   U13                     Ethanol -- CBOT
## 6    F       US    BBS   U13     South American Soybeans -- CBOT

6

必备的 data.table 答案

library(data.table)
full_list <- data.table(full_list, key='Symbol')
replace <- data.table(replace, key='Ticker')

replace[full_list]

对于超过1e5行的数据集,使用有键值的data.table将比其他列出的方法(除了qdap版本,我没有尝试过)更快。 这里可以找到合并时间


1

如果您正在使用大型数据集,可能会遇到一些时间/内存问题,如果是这种情况,请尝试以下操作:

require(plyr)

colnames(replace)<-c("Type", "Location", "Ticker", "Month")

Full<-join(full_list, replace, by = "Ticker", type = "left", match = "all")

> Full
  Ticker                           Long_Name Type Location Month
1     AC                     Ethanol -- CBOT    F       US   U13
2    AIC      DJ UBS Commodity Index -- CBOT    F       US   U13
3    BBS     South American Soybeans -- CBOT    F       US   U13
4     BO                  Soybean Oil -- CBT    F       US   V13
5      C                         Corn -- CBT    F       US   U13
6     DF Dow Jones Industrial Average -- CBT    F       US   U13
< p>尽管合并不仅仅是一行解决方案,但处理大型数据框可能需要一些时间。此外,plyr包可能是您最好的朋友。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接