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.
Cet exercice fait partie du cours
Importing and Managing Financial Data in R
Instructions
- Import the first 5 lines of
two_symbols.csvusingread.csv(). Assign the output totwo_symbols_data. - Look at the structure of
two_symbols_dataand note the column names and locations. - Use
read.zoo()to importtwo_symbols.csv, specifyingsplitas the names of the symbol and type columns. Assign the output totwo_symbols_zoo. - Look at the first few rows of
two_symbols_zoo.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de 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