getSymbols 并使用 lapply、Cl 和 merge 来提取收盘价
我已经搞乱这个有一段时间了。我最近开始使用 quantmod 包对股票价格进行分析。
我有一个如下所示的股票代码向量:
> tickers
[1] "SPY" "DIA" "IWM" "SMH" "OIH" "XLY" "XLP" "XLE" "XLI" "XLB" "XLK" "XLU" "XLV"
[14] "QQQ"
> str(tickers)
chr [1:14] "SPY" "DIA" "IWM" "SMH" "OIH" "XLY" "XLP" "XLE" ...
我编写了一个名为 myX 的函数,在 lapply 调用中使用,以保存向量股票代码中每支股票的价格。它有以下代码:
myX <- function(tickers, start, end) {
require(quantmod)
getSymbols(tickers, from=start, to=end)
}
我自己调用 lapply
库(Quantmod) lapply(股票代码,myX,start =“2001-03-01”,end =“2011-03-11”)
> lapply(tickers,myX,start="2001-03-01", end="2011-03-11")
[[1]]
[1] "SPY"
[[2]]
[1] "DIA"
[[3]]
[1] "IWM"
[[4]]
[1] "SMH"
[[5]]
[1] "OIH"
[[6]]
[1] "XLY"
[[7]]
[1] "XLP"
[[8]]
[1] "XLE"
[[9]]
[1] "XLI"
[[10]]
[1] "XLB"
[[11]]
[1] "XLK"
[[12]]
[1] "XLU"
[[13]]
[1] "XLV"
[[14]]
[1] "QQQ"
效果很好。现在我想将每只股票的收盘价合并到一个对象中,该对象看起来像
# BCSI.Close WBSN.Close NTAP.Close FFIV.Close SU.Close
# 2011-01-03 30.50 20.36 57.41 134.33 38.82
# 2011-01-04 30.24 19.82 57.38 132.07 38.03
# 2011-01-05 31.36 19.90 57.87 137.29 38.40
# 2011-01-06 32.04 19.79 57.49 138.07 37.23
# 2011-01-07 31.95 19.77 57.20 138.35 37.30
# 2011-01-10 31.55 19.76 58.22 142.69 37.04
有人建议我尝试如下所示:
ClosePrices <- do.call(merge, lapply(tickers, function(x) Cl(get(x))))
但是我尝试了各种组合,但没有成功。首先,我尝试使用 Cl(x) 调用 lapply
>lapply(tickers,myX,start="2001-03-01", end="2011-03-11") Cl(myX)))
> lapply(tickers,myX,start="2001-03-01", end="2011-03-11") Cl(x)))
Error: unexpected symbol in "lapply(tickers,myX,start="2001-03-01", end="2011-03-11") Cl"
>
> lapply(tickers,myX(x),start="2001-03-01", end="2011-03-11") Cl(x)))
Error: unexpected symbol in "lapply(tickers,myX(x),start="2001-03-01", end="2011-03-11") Cl"
>
> lapply(tickers,myX(start="2001-03-01", end="2011-03-11") Cl(x)
Error: unexpected symbol in "lapply(tickers,myX(start="2001-03-01", end="2011-03-11") Cl"
> lapply(tickers,myX(start="2001-03-01", end="2011-03-11") Cl(x))
Error: unexpected symbol in "lapply(tickers,myX(start="2001-03-01", end="2011-03-11") Cl"
>
任何指导将不胜感激。
I've been messing around with this for some time. I recently started using the quantmod package to perform analytics on stock prices.
I have a ticker vector that looks like the following:
> tickers
[1] "SPY" "DIA" "IWM" "SMH" "OIH" "XLY" "XLP" "XLE" "XLI" "XLB" "XLK" "XLU" "XLV"
[14] "QQQ"
> str(tickers)
chr [1:14] "SPY" "DIA" "IWM" "SMH" "OIH" "XLY" "XLP" "XLE" ...
I wrote a function called myX to use in a lapply call to save prices for every stock in the vector tickers. It has the following code:
myX <- function(tickers, start, end) {
require(quantmod)
getSymbols(tickers, from=start, to=end)
}
I call lapply by itself
library(quantmod)
lapply(tickers,myX,start="2001-03-01", end="2011-03-11")
> lapply(tickers,myX,start="2001-03-01", end="2011-03-11")
[[1]]
[1] "SPY"
[[2]]
[1] "DIA"
[[3]]
[1] "IWM"
[[4]]
[1] "SMH"
[[5]]
[1] "OIH"
[[6]]
[1] "XLY"
[[7]]
[1] "XLP"
[[8]]
[1] "XLE"
[[9]]
[1] "XLI"
[[10]]
[1] "XLB"
[[11]]
[1] "XLK"
[[12]]
[1] "XLU"
[[13]]
[1] "XLV"
[[14]]
[1] "QQQ"
That works fine. Now I want to merge the Close prices for every stock into an object that looks like
# BCSI.Close WBSN.Close NTAP.Close FFIV.Close SU.Close
# 2011-01-03 30.50 20.36 57.41 134.33 38.82
# 2011-01-04 30.24 19.82 57.38 132.07 38.03
# 2011-01-05 31.36 19.90 57.87 137.29 38.40
# 2011-01-06 32.04 19.79 57.49 138.07 37.23
# 2011-01-07 31.95 19.77 57.20 138.35 37.30
# 2011-01-10 31.55 19.76 58.22 142.69 37.04
Someone recommended I try something like the following:
ClosePrices <- do.call(merge, lapply(tickers, function(x) Cl(get(x))))
However I tried various combinations of this without any success. First I tried just calling lapply with Cl(x)
>lapply(tickers,myX,start="2001-03-01", end="2011-03-11") Cl(myX)))
> lapply(tickers,myX,start="2001-03-01", end="2011-03-11") Cl(x)))
Error: unexpected symbol in "lapply(tickers,myX,start="2001-03-01", end="2011-03-11") Cl"
>
> lapply(tickers,myX(x),start="2001-03-01", end="2011-03-11") Cl(x)))
Error: unexpected symbol in "lapply(tickers,myX(x),start="2001-03-01", end="2011-03-11") Cl"
>
> lapply(tickers,myX(start="2001-03-01", end="2011-03-11") Cl(x)
Error: unexpected symbol in "lapply(tickers,myX(start="2001-03-01", end="2011-03-11") Cl"
> lapply(tickers,myX(start="2001-03-01", end="2011-03-11") Cl(x))
Error: unexpected symbol in "lapply(tickers,myX(start="2001-03-01", end="2011-03-11") Cl"
>
Any guidance would be kindly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如我在 R-help 上的回复中所说,
getSymbols
是矢量化的,因此无需循环tickers
。您不需要myX
函数,并且lapply
调用完全不必要/多余。我原来的答案中的代码有效。你为什么要尝试其他组合?
As I said in my reply on R-help,
getSymbols
is vectorized, so there's no need to loop overtickers
. You don't need yourmyX
function and thelapply
call is completely unnecessary / redundant.The code in my original answer works. Why are you trying other combinations?
尝试使用 env= arg 并 eapply
Try using the env= arg and eapply
为了成功地合并数据帧,需要有共同的列名。我怀疑你想要的是
cbind
而不是merge
。但正如 Joshua 指出的那样(他应该知道)
merge
也适用于 getSymbols 返回的对象类 (xts)。In order for
merge
to succeed with dataframes, there need to be column names in common. I suspected you wantedcbind
rather thanmerge
anyway.But as Joshua points out (and he should know)
merge
also works for the class of objects (xts) returned by getSymbols.