gpt4 book ai didi

r - getSymbols 并使用 lapply、Cl 和 merge 提取收盘价

转载 作者:行者123 更新时间:2023-12-01 22:44:06 26 4
gpt4 key购买 nike

我已经弄乱了一段时间。我最近开始使用 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

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"

这很好用。现在我想将每只股票的收盘价合并到一个对象中,如下所示

#            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"
>

如有任何指导,我们将不胜感激。

最佳答案

正如我在 R-help 上的回复中所说,getSymbols 是矢量化的,因此无需遍历 tickers。您不需要 myX 函数,而且 lapply 调用完全没有必要/多余。

我原来答案中的代码有效。为什么要尝试其他组合?

tickers <- c("SPY","DIA","IWM","SMH","OIH","XLY",
"XLP","XLE","XLI","XLB","XLK","XLU")
getSymbols(tickers, from="2001-03-01", to="2011-03-11")
ClosePrices <- do.call(merge, lapply(tickers, function(x) Cl(get(x))))
head(ClosePrices)
# SPY.Close DIA.Close IWM.Close SMH.Close OIH.Close XLY.Close
# 2001-03-01 124.60 104.68 94.80 44.60 87.45 26.10
# 2001-03-02 123.61 104.80 95.05 45.34 91.20 26.30
# 2001-03-05 124.74 105.57 94.70 47.01 92.86 26.02
# 2001-03-06 126.08 106.15 96.10 49.59 94.34 26.68
# 2001-03-07 126.98 107.45 96.60 49.20 97.36 27.34
# 2001-03-08 127.12 108.61 95.80 49.20 97.59 27.78
# XLP.Close XLE.Close XLI.Close XLB.Close XLK.Close XLU.Close
# 2001-03-01 26.39 32.10 29.28 21.14 28.80 31.62
# 2001-03-02 26.64 32.83 29.45 21.64 27.80 31.70
# 2001-03-05 26.54 33.01 29.82 22.03 28.40 31.64
# 2001-03-06 26.00 33.18 30.25 21.98 29.60 31.60
# 2001-03-07 25.83 33.89 30.61 22.63 29.64 31.45
# 2001-03-08 26.05 34.23 30.80 22.71 29.05 32.04

关于r - getSymbols 并使用 lapply、Cl 和 merge 提取收盘价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5574595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com