I am trying to save stock time series in a rda/RData file and then call it in quantmod.
I have downloaded and saved AAPL's stock time series as an rda/RData file using this code
data=getSymbols("AAPL",auto.assign=F)
save(data,file="AAPL.rda")
#Cleared environment
Now how should I call this rda/RData file using quantmod. tried this
getSymbols('AAPL',src='rda')
but its showing this error
Error in fr[, -1] : incorrect number of dimensions
next question is if I have multiple rda files(like AAPL.rda,GOOG.rda,F.rda) how should I call these files in an environment using quantmod.
EDITED
I missied this point I want to call from getSymbols() instead of load() because I need more control to the time series
for example
getSymbols('AAPL',src='rda',from="2010-02-02",to="2011-01-01")
From the documentation of getSymbols()
in the quantmod
package:
Current src methods available are: yahoo, google, MySQL, FRED, csv, RData, and oanda.
What you can do to save a file and load it afterwards is, e.g., the following:
data <- getSymbols("AAPL",auto.assign=FALSE)
save(data,file="AAPL.rda")
(start a new R session, or clear environment etc.)
To retrieve the previously saved data we can use
load(file = "AAPL.rda")
Now the data set data
with the AAPL time series is available again.
Note that one should not assign the output of load()
to an object, like data <- load(file=...)
. This is a common mistake that often creates confusion. The data
file in this example is restored with the load()
function itself.
A subset of the loaded data can be obtained, e.g., with
data['2010-02-02::2011-01-01']
#> head(data['2010-02-02::2011-01-01'])
# AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
#2010-02-02 195.91 196.32 193.38 195.86 174585600 25.75517
#2010-02-03 195.17 200.20 194.42 199.23 153832000 26.19832
#2010-02-04 196.73 198.37 191.57 192.05 189413000 25.25416
#2010-02-05 192.63 196.00 190.85 195.46 212576700 25.70257
#2010-02-08 195.69 197.88 194.00 194.12 119567700 25.52636
#2010-02-09 196.42 197.50 194.75 196.19 158221700 25.79856
Edit:
Below is an example that illustrates how .RData
files can be used:
saveSymbols(getSymbols("AAPL"), file.path=getwd()) #saves 'AAPL.RData' in working directory
rm(AAPL)
data <- getSymbols("AAPL", src="RData", extension="RData", auto.assign=FALSE)
#> head(data)
# AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
#2007-01-03 86.29 86.58 81.90 83.80 309579900 11.01952
#2007-01-04 84.05 85.95 83.82 85.66 211815100 11.26411
#2007-01-05 85.77 86.20 84.40 85.05 208685400 11.18389
#2007-01-08 85.96 86.53 85.28 85.47 199276700 11.23912
#2007-01-09 86.45 92.98 85.15 92.57 837324600 12.17276
#2007-01-10 94.75 97.80 93.45 97.00 738220000 12.75529
Further Edit:
I can confirm the statements made by @Hack-R in the comments. Although the documentation describes this in a rather hidden way, by reading the section about getSymbols.rda()
it becomes clear that the src="rda"
option is still supported as a valid parameter for getSymbols()
. Indeed it still works on my installation:
data <- getSymbols("AAPL",auto.assign=FALSE)
save(data,file="AAPL.rda")
rm(data)
data <- getSymbols("AAPL", src="rda", auto.assign=FALSE)
#>head(data)
# AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
#2007-01-03 86.29 86.58 81.90 83.80 309579900 11.01952
#2007-01-04 84.05 85.95 83.82 85.66 211815100 11.26411
#2007-01-05 85.77 86.20 84.40 85.05 208685400 11.18389
#2007-01-08 85.96 86.53 85.28 85.47 199276700 11.23912
#2007-01-09 86.45 92.98 85.15 92.57 837324600 12.17276
#2007-01-10 94.75 97.80 93.45 97.00 738220000 12.75529
In conclusion, after a couple of variants of saving, loading, and manipulating time series with quantmod we seem to be back to square one: The error described in the OP does not seem to be reproducible.