[英]How to import time series in rda/RData file using quantmod R
I am trying to save stock time series in a rda/RData file and then call it in quantmod. 我试图将股票时间序列保存在rda / RData文件中,然后在quantmod中调用它。
I have downloaded and saved AAPL's stock time series as an rda/RData file using this code 我已使用此代码将APL的股票时间序列下载并保存为rda / RData文件
data=getSymbols("AAPL",auto.assign=F)
save(data,file="AAPL.rda")
#Cleared environment
Now how should I call this rda/RData file using quantmod. 现在,我应该如何使用quantmod来调用此rda / RData文件。 tried this
试试这个
getSymbols('AAPL',src='rda')
but its showing this error 但它显示此错误
Error in fr[, -1] : incorrect number of dimensions
fr [,-1]中的错误:维数不正确
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. 下一个问题是,如果我有多个rda文件(如AAPL.rda,GOOG.rda,F.rda),在使用quantmod的环境中应如何调用这些文件。
EDITED 已编辑
I missied this point I want to call from getSymbols() instead of load() because I need more control to the time series 我想从getSymbols()而不是load()调用这一点,因为我需要对时间序列进行更多控制
for example 例如
getSymbols('AAPL',src='rda',from="2010-02-02",to="2011-01-01")
From the documentation of getSymbols()
in the quantmod
package: 从
quantmod
包中的getSymbols()
的文档中:
Current src methods available are: yahoo, google, MySQL, FRED, csv, RData, and oanda.
当前可用的src方法为:yahoo,google,MySQL,FRED,csv,RData和oanda。
What you can do to save a file and load it afterwards is, eg, the following: 您可以执行以下操作来保存文件并随后加载它:
data <- getSymbols("AAPL",auto.assign=FALSE)
save(data,file="AAPL.rda")
(start a new R session, or clear environment etc.) (开始一个新的R会话,或清除环境等)
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. 现在,具有AAPL时间序列的数据集
data
再次可用。 Note that one should not assign the output of load()
to an object, like data <- load(file=...)
. 注意,一个应的输出不分配
load()
到一个对象,像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. 此示例中的
data
文件是使用load()
函数本身还原的。
A subset of the loaded data can be obtained, eg, 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: 下面的示例说明了如何使用
.RData
文件:
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. 我可以在评论中确认@ Hack-R的发言。 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()
. 尽管文档以相当隐蔽的方式对此进行了描述,但通过阅读有关
getSymbols.rda()
的部分,可以清楚地看到src="rda"
选项仍受支持为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. 总之,在使用quantmod保存,加载和操纵时间序列后,我们似乎又回到了平方:在OP中描述的错误似乎不可重现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.