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.
Cet exercice fait partie du cours
Importing and Managing Financial Data in R
Instructions
- Use
new.env()to create a new environment nameddata_env. - Use
getSymbols()to load data intodata_envby using theenvargument. - 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.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de 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