我列出了前 25 名最受欢迎的 ETF。我想创建一个 for 循环,该循环接受此列表的每个条目并使用 quantmod 库中的 getSymbol() 函数并返回调用的数据。当我尝试时,所发生的只是打印列表中的条目。
这是我的代码。
`top25_ETFS <- c("SPY", "IVV", "VOO", "VTI", "QQQ", "VEA", "VUG","VTV",
"IEFA","BND","AGG","IWF","IJH","IJR","VIG","IEMG"
,"VWO","VXUS","VGT","IWM","XLK","VO","GLD","VB","SCHD",)
for (i in 1:1) {
etf <- getSymbols(top25_ETFS[i], src = "yahoo")
etf_returns <- ROC(etf$.Adjusted, type = 'discrete')
}`
这是打印“etf”时的输出
`[1] "SPY"`
这是打印“etf_returns”时的输出
`Error in etf$etf.Adjusted : $ operator is invalid for atomic vectors`
我不确定如何将实际数据存储在变量 etf 中,然后从该 etf 变量中找到其返回值。我希望能够创建一个 for 循环,使所有 25 个 ETF 的流程自动化
library(quantmod)
top25_ETFS <- c("SPY", "IVV", "VOO", "VTI", "QQQ", "VEA", "VUG","VTV",
"IEFA","BND","AGG","IWF","IJH","IJR","VIG","IEMG"
,"VWO","VXUS","VGT","IWM","XLK","VO","GLD","VB","SCHD")
lst <- vector("list", length(top25_ETFS))
# only 1:6 for the reprex
for (i in 1:6) {
lst[[i]] <- getSymbols(top25_ETFS[i], src = "yahoo", auto.assign = FALSE)
}
names(lst) <- top25_ETFS
head(lst)
#> $SPY
#> SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
#> 2007-01-03 142.25 142.86 140.57 141.37 94807600 101.5366
#> 2007-01-04 141.23 142.05 140.61 141.67 69620600 101.7521
#> 2007-01-05 141.33 141.40 140.38 140.54 76645300 100.9405
#> 2007-01-08 140.82 141.41 140.25 141.19 71655000 101.4073
#> 2007-01-09 141.31 141.60 140.40 141.07 75680100 101.3212
#> 2007-01-10 140.58 141.57 140.30 141.54 72428000 101.6587
#> 2007-01-11 141.58 142.62 141.50 142.16 54476800 102.1040
#> 2007-01-12 142.15 143.24 142.11 143.24 55370600 102.8797
#> 2007-01-16 143.07 143.44 142.73 142.96 44871300 102.6786
#> 2007-01-17 142.85 143.46 142.73 143.02 50241400 102.7217
#> ...
#> 2024-03-22 522.11 522.61 520.97 521.21 79023000 521.2100
#> 2024-03-25 519.80 520.95 519.61 519.77 48512100 519.7700
#> 2024-03-26 521.23 521.58 518.40 518.81 65463700 518.8100
#> 2024-03-27 521.71 523.21 519.49 523.17 82999800 523.1700
#> 2024-03-28 523.21 524.61 522.78 523.07 96294900 523.0700
#> 2024-04-01 523.83 524.38 520.97 522.16 62477500 522.1600
#> 2024-04-02 518.24 518.98 516.48 518.84 74230300 518.8400
#> 2024-04-03 517.72 520.95 517.67 519.41 59155800 519.4100
#> 2024-04-04 523.52 523.87 512.76 513.07 96858100 513.0700
#> 2024-04-05 514.46 520.44 514.01 518.43 74482100 518.4300
#>
#> $IVV
#> IVV.Open IVV.High IVV.Low IVV.Close IVV.Volume IVV.Adjusted
#> 2007-01-03 142.53 143.13 140.86 141.62 2001100 101.7804
#> 2007-01-04 141.68 142.32 140.95 142.00 4038600 102.0534
#> 2007-01-05 141.60 141.60 140.66 140.91 2028700 101.2701
#> 2007-01-08 141.07 141.66 140.55 141.50 642300 101.6941
#> 2007-01-09 141.66 141.77 140.67 141.38 989000 101.6079
#> 2007-01-10 140.81 141.81 140.59 141.76 1738800 101.8809
#> 2007-01-11 141.88 142.91 141.78 142.50 693700 102.4128
#> 2007-01-12 142.48 143.57 142.46 143.57 634000 103.1818
#> 2007-01-16 143.45 143.59 143.03 143.28 813400 102.9733
#> 2007-01-17 143.15 143.74 143.03 143.28 1457500 102.9733
#> ...
#> 2024-03-22 524.63 525.17 523.52 523.92 6938300 523.9200
#> 2024-03-25 522.31 523.48 522.15 522.28 6297200 522.2800
#> 2024-03-26 523.82 524.13 520.95 521.18 5932600 521.1800
#> 2024-03-27 524.26 525.77 522.02 525.72 4777600 525.7200
#> 2024-03-28 525.84 527.16 525.34 525.73 4717000 525.7300
#> 2024-04-01 526.36 526.96 523.54 524.88 6170800 524.8800
#> 2024-04-02 520.77 521.52 519.04 521.45 7653100 521.4500
#> 2024-04-03 520.26 523.51 520.21 522.03 5820200 522.0300
#> 2024-04-04 526.09 526.43 515.27 515.61 7222700 515.6100
#> 2024-04-05 517.00 523.00 516.55 520.99 5275500 520.9900
#>
#> $VOO
#> VOO.Open VOO.High VOO.Low VOO.Close VOO.Volume VOO.Adjusted
#> 2010-09-09 102.50 102.50 101.14 101.32 26500 78.49095
#> 2010-09-10 101.68 101.86 101.30 101.78 8600 78.84731
#> 2010-09-13 102.96 103.14 102.50 103.06 33750 79.83892
#> 2010-09-14 102.84 103.48 102.38 103.04 59400 79.82339
#> 2010-09-15 102.62 103.38 102.40 103.30 9250 80.02483
#> 2010-09-16 103.02 103.32 102.70 103.26 59550 79.99386
#> 2010-09-17 103.88 103.88 103.02 103.36 49350 80.07131
#> 2010-09-20 103.74 105.04 103.46 105.04 19000 81.37278
#> 2010-09-21 105.02 105.44 104.28 104.72 19250 81.12488
#> 2010-09-22 104.56 105.04 103.96 104.16 18100 80.69102
#> ...
#> 2024-03-22 479.87 480.32 478.82 479.18 5876800 479.17999
#> 2024-03-25 477.73 478.79 477.55 477.94 6081300 477.94000
#> 2024-03-26 479.06 479.37 476.43 476.60 8073500 476.60001
#> 2024-03-27 479.51 480.87 477.45 480.76 4951400 480.76001
#> 2024-03-28 480.87 482.16 480.50 480.70 4065000 480.70001
#> 2024-04-01 481.44 481.96 478.83 480.07 5734000 480.07001
#> 2024-04-02 476.33 476.99 474.71 476.93 4817800 476.92999
#> 2024-04-03 475.84 478.80 475.79 477.36 6224800 477.35999
#> 2024-04-04 481.20 481.48 471.27 471.48 6871200 471.48001
#> 2024-04-05 472.85 478.33 472.42 476.49 5582200 476.48999
#>
#> $VTI
#> VTI.Open VTI.High VTI.Low VTI.Close VTI.Volume VTI.Adjusted
#> 2007-01-03 70.575 70.680 69.525 69.975 798600 50.67750
#> 2007-01-04 69.975 70.295 69.585 70.115 3305000 50.77890
#> 2007-01-05 70.000 70.000 69.425 69.560 382000 50.37696
#> 2007-01-08 69.760 69.905 69.325 69.815 299000 50.56162
#> 2007-01-09 69.950 69.950 69.425 69.815 267000 50.56162
#> 2007-01-10 69.590 70.050 69.485 69.970 359200 50.67387
#> 2007-01-11 70.175 70.655 70.100 70.500 336400 51.05773
#> 2007-01-12 70.550 70.965 70.450 70.965 346200 51.39448
#> 2007-01-16 71.075 71.075 70.750 70.830 390000 51.29668
#> 2007-01-17 70.785 71.070 70.730 70.880 334000 51.33294
#> ...
#> 2024-03-22 259.190 259.450 258.340 258.500 2463200 258.50000
#> 2024-03-25 258.100 258.490 257.790 257.920 2714400 257.92001
#> 2024-03-26 258.800 258.830 257.280 257.460 2591500 257.45999
#> 2024-03-27 259.130 259.850 258.010 259.730 4030200 259.73001
#> 2024-03-28 259.800 260.580 259.760 259.900 2669100 259.89999
#> 2024-04-01 260.320 260.380 258.680 259.700 3292300 259.70001
#> 2024-04-02 257.500 257.500 256.190 257.300 3213400 257.29999
#> 2024-04-03 256.730 258.440 256.660 257.700 3241600 257.70001
#> 2024-04-04 259.650 259.990 254.440 254.610 3121300 254.61000
#> 2024-04-05 255.110 258.200 255.000 257.180 4177700 257.17999
#>
#> $QQQ
#> QQQ.Open QQQ.High QQQ.Low QQQ.Close QQQ.Volume QQQ.Adjusted
#> 2007-01-03 43.46 44.06 42.52 43.24 167689500 37.48681
#> 2007-01-04 43.30 44.21 43.15 44.06 136853500 38.19769
#> 2007-01-05 43.95 43.95 43.48 43.85 138958800 38.01564
#> 2007-01-08 43.89 44.12 43.64 43.88 106401600 38.04168
#> 2007-01-09 44.01 44.29 43.63 44.10 121577500 38.23237
#> 2007-01-10 43.96 44.66 43.82 44.62 121070100 38.68321
#> 2007-01-11 44.71 45.24 44.65 45.08 174029800 39.08199
#> 2007-01-12 45.02 45.35 44.97 45.31 104217300 39.28139
#> 2007-01-16 45.35 45.40 45.13 45.28 95690500 39.25539
#> 2007-01-17 45.09 45.29 44.83 44.91 127142600 38.93461
#> ...
#> 2024-03-22 445.35 447.49 444.49 446.38 28229300 446.38000
#> 2024-03-25 443.55 446.26 442.54 444.76 27789200 444.76001
#> 2024-03-26 446.30 447.23 443.09 443.32 34142000 443.32001
#> 2024-03-27 446.44 446.54 441.93 444.83 41359200 444.82999
#> 2024-03-28 444.78 445.64 443.65 444.01 36839600 444.01001
#> 2024-04-01 444.97 447.53 443.02 444.95 38729000 444.95001
#> 2024-04-02 440.08 441.43 438.03 441.11 44259700 441.10999
#> 2024-04-03 438.96 444.00 438.95 442.10 39949800 442.10001
#> 2024-04-04 446.33 446.95 435.11 435.34 57267700 435.34000
#> 2024-04-05 436.78 443.17 435.96 440.47 54723000 440.47000
#>
#> $VEA
#> VEA.Open VEA.High VEA.Low VEA.Close VEA.Volume VEA.Adjusted
#> 2007-07-26 48.48 48.67 47.50 47.90 101000 28.84520
#> 2007-07-27 47.78 48.04 47.15 47.17 579000 28.40561
#> 2007-07-30 47.87 48.14 47.66 47.96 298400 28.88134
#> 2007-07-31 48.51 48.67 47.90 47.90 210400 28.84520
#> 2007-08-01 47.68 49.00 47.24 48.10 353900 28.96565
#> 2007-08-02 47.92 48.14 47.66 47.96 251800 28.88134
#> 2007-08-03 47.82 47.82 47.14 47.17 309600 28.40561
#> 2007-08-06 47.29 47.90 47.25 47.89 232100 28.83919
#> 2007-08-07 47.40 48.24 47.40 47.96 181800 28.88134
#> 2007-08-08 48.40 48.90 48.38 48.68 135700 29.31492
#> ...
#> 2024-03-22 50.08 50.10 49.89 49.94 9289600 49.94000
#> 2024-03-25 49.82 50.05 49.81 49.87 7858000 49.87000
#> 2024-03-26 50.11 50.13 49.93 49.94 12502400 49.94000
#> 2024-03-27 50.04 50.27 49.99 50.27 7233200 50.27000
#> 2024-03-28 50.10 50.24 50.10 50.17 8336000 50.17000
#> 2024-04-01 50.10 50.21 49.85 49.97 9987800 49.97000
#> 2024-04-02 49.63 49.66 49.50 49.66 15614500 49.66000
#> 2024-04-03 49.54 50.00 49.53 49.93 17859100 49.93000
#> 2024-04-04 50.26 50.29 49.48 49.53 17525700 49.53000
#> 2024-04-05 49.49 49.82 49.37 49.71 13240900 49.71000
创建于 2024-04-07,使用 reprex v2.1.0