Finding GICS Sector using Rblpapi in R

266 views Asked by At

I am trying to replace a column in my data with the output of function: bdp(column + "equity", "GICS_SECTOR NAME")

Require(Rblpapi)
#Create raw data example
ticker <- c(2,3,4,5,6)
sector <- c(NA,NA,NA,NA,NA)
dataraw <- data.frame(ticker, random)

dataraw$sector <- bdp("dataraw$ticker Equity", "GICS_SECTOR_NAME")

This does not work due to "" making it text only and I have to add the word "Equity" e.g. IBM Equity. An example of it working perfectly would be bdp("IBM Equity", "GICS_SECTOR_NAME")

1

There are 1 answers

2
tester On

You can add the "Equity" part using paste and use the resulting ticker as an argument to bdp:

#Create raw data example
ticker <- c("IBM", "AAPL", "MSFT", "FB")
sector <- c(NA,NA,NA,NA)
df <- data.frame(ticker, sector)
df$ticker_full <- paste(df$ticker, "US Equity", sep = " ")

conn <- Rblpapi::blpConnect()

sectors <- bdp(securities = df$ticker_full,
               fields = "GICS_SECTOR_NAME")

> print(sectors)
                     GICS_SECTOR_NAME
IBM US Equity  Information Technology
AAPL US Equity Information Technology
MSFT US Equity Information Technology
FB US Equity   Communication Services

df$sector <- sectors$GICS_SECTOR_NAME

> print(df)
  ticker                 sector    ticker_full
1    IBM Information Technology  IBM US Equity
2   AAPL Information Technology AAPL US Equity
3   MSFT Information Technology MSFT US Equity
4     FB Communication Services   FB US Equity