Extract the Close column from many instruments
The previous exercise taught you how to use do.call(merge, eapply(env, fun))
to apply a function to each object in an environment and then combine all the results into one object.
Let's use what you learned to solve a very common problem. Often you will need to load similar data for many instruments, extract a column, and create one object that contains that specific column for every instrument.
This exercise is part of the course
Importing and Managing Financial Data in R
Exercise instructions
- Use
new.env()
to create a new environment nameddata_env
. - Use
getSymbols()
to load data intodata_env
by using theenv
argument. - Finish the commands that have been started for you. Fill in the blanks with the appropriate functions.
- View the first few rows of
close_data
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Symbols
symbols <- c("AAPL", "MSFT", "IBM")
# Create new environment
# Load symbols into data_env
# Extract the close column from each object and combine into one xts object
close_data <- ___(merge, ___(data_env, Cl))
# View the head of close_data