Exercise

Use getPrice to extract other columns

The extractor functions you learned in the previous two exercises do not cover all use cases. Sometimes you might have one object that contains the same price column for multiple instruments. Other times, you might have an object with price data (e.g. bid, ask, trade) that do not have an explicit extractor function.

The getPrice() function in the quantmod package can extract any column by name by using the prefer argument. It can also extract columns for a specific instrument by using the symbol argument, which is useful when an object contains several instruments with the same price type.

You can use regular expressions for both the prefer and symbol arguments, because they are passed to the base::grep() function internally.

Instructions

100 XP
  • Finish the Quandl() call to import CME futures data for CL and BZ into an xts object, oil_data.
  • Use colnames() to view the column names of the oil_data object.
  • Finish the getPrice() call to extract the Open price for CL into cl_open. Set prefer = "Open$" to match "Open" and not "Open Interest".
  • Use a ISO-8601 string to subset cl_open for the month of January, 2016 (don't use a range, and you don't need to specify any days).