Read text file containing multiple instruments
The previous exercises work if each file contains only one instrument. Some software and data vendors may provide data for all instruments in one file. This exercise will teach you how to import a file that contains multiple instruments.
Once again, you can use read.zoo()
. This time you will be using its split
argument, which allows you to specify the name or number of the columns(s) that contain the variables that identify unique observations.
The two_symbols.csv
file in your working directory contains bid/ask data for two instruments, where each row has one bid or ask observation for one instrument. You will use the split
argument to import the data into an object that has both bid and ask prices for both instruments on one row.
This exercise is part of the course
Importing and Managing Financial Data in R
Exercise instructions
- Import the first 5 lines of
two_symbols.csv
usingread.csv()
. Assign the output totwo_symbols_data
. - Look at the structure of
two_symbols_data
and note the column names and locations. - Use
read.zoo()
to importtwo_symbols.csv
, specifyingsplit
as the names of the symbol and type columns. Assign the output totwo_symbols_zoo
. - Look at the first few rows of
two_symbols_zoo
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Read data with read.csv
___ <- read.csv("___", nrows = ___)
# Look at the structure of two_symbols_data
# Specify Symbol and Type index column names
two_symbols_zoo <- read.zoo("two_symbols.csv", split = c("___", "___"), sep = "___", header = ___)
# Look at first few rows of data