VLookup type method in R [duplicate]

百般思念 提交于 2019-11-28 07:03:20

merge them:

> 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

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

Then just create a new column in your replace data frame (note - you should call it something else, because replace is actually a function in r) using the full_list data frame with the matched symbols.

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

If it's a big data set you may benefit from an environment lookup:

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
Justin

Obligatory data.table answer

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

replace[full_list]

FWIW on a data set above about 1e5 rows a keyed data.table will be significantly faster than the other approaches listed (except for the qdap version, I haven't tried that). merge timings can be found here

If you're using a large data set, you might run into some time/memory issues, if that's the case, try this:

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

Although its more than just a one line solution, merge can take some time to process with larger dataframes. Also, the plyr package can be your best friend.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!