getSymbols.yahoo "method" 在一个循环中,在 R 中有几个符号和相应的日期
getSymbols.yahoo "method" in a loop with several symbols and corresponding dates in R
“方法”getSymbols.yahoo
允许用向量代替 Symbols
参数,允许在不使用循环的情况下下载多个代码:
library(quantmod)
someSymbols <- c("RMO", "NKLA", "JD", "AAPL", "IBM", "SPCE", "MRNA", "MPNGF")
getSymbols.yahoo(someSymbols, auto.assign = TRUE, env = globalenv())
但是,当在 from =
参数中添加一个等长的 date
向量时,这似乎不起作用,例如:
library(quantmod)
someSymbols <- c("RMO", "NKLA", "JD", "AAPL", "IBM", "SPCE", "MRNA", "MPNGF")
someDates <- as.Date(c(18659, 18659, 18659, 18901, 18901, 18659, 18901,
18659), origin = "1970-01-01")
getSymbols.yahoo(someSymbols, auto.assign = TRUE, env = globalenv(), from = someDates)
我的问题:
- 虽然必须保留
someSymbols
和 someDates
各自的顺序,并且(最好)只有 System Libraries
和 library(quantmod)
(没有 getSymbols.yahoo
不工作),哪段代码可以下载每个符号及其相应的日期?
提前致谢。
使用的系统:
- R版本:4.1.1 (2021-08-10)
- RStudio 版本:1.4.1717
- OS: macOS Catalina 版本 10.15.7 和 macOS Big Sur 版本 11.6
使用 Map
一次将一个元素从每个向量传递给函数。
library(quantmod)
someSymbols <- c("RMO", "NKLA", "JD", "AAPL", "IBM", "SPCE", "MRNA", "MPNGF")
someDates <- as.Date(c(18659, 18659, 18659, 18901, 18901, 18659, 18901,
18659), origin = "1970-01-01")
Map(\(x, y) getSymbols(
Symbols = x,
env = globalenv(),
auto.assign = TRUE,
from = y,
src = "yahoo"), someSymbols, someDates)
我们可以用map2
library(quantmod)
library(purrr)
library(dplyr)
map2(someSymbols, someDates, ~ getSymbols(.x,
env = globalenv(),
auto.assign = TRUE,
from = .y,
src = "yahoo"))
-正在检查
> head(RMO)
RMO.Open RMO.High RMO.Low RMO.Close RMO.Volume RMO.Adjusted
2021-02-01 18.10 19.25 17.17 18.54 9507500 18.54
2021-02-02 19.01 19.42 18.01 18.64 5996100 18.64
2021-02-03 19.04 19.52 18.35 18.47 5531300 18.47
2021-02-04 18.98 19.16 17.93 18.44 5004800 18.44
2021-02-05 18.77 19.25 18.38 18.44 4862800 18.44
2021-02-08 18.73 20.00 18.52 19.36 5616900 19.36
> head(NKLA)
NKLA.Open NKLA.High NKLA.Low NKLA.Close NKLA.Volume NKLA.Adjusted
2021-02-01 24.05 24.400 22.12 23.51 13861900 23.51
2021-02-02 23.74 23.970 22.56 23.00 8487200 23.00
2021-02-03 23.09 24.980 23.01 24.43 12034600 24.43
2021-02-04 24.46 25.225 23.82 24.42 8250700 24.42
2021-02-05 24.75 24.920 23.00 23.60 8481300 23.60
2021-02-08 23.51 23.980 23.17 23.50 7934000 23.50
“方法”getSymbols.yahoo
允许用向量代替 Symbols
参数,允许在不使用循环的情况下下载多个代码:
library(quantmod)
someSymbols <- c("RMO", "NKLA", "JD", "AAPL", "IBM", "SPCE", "MRNA", "MPNGF")
getSymbols.yahoo(someSymbols, auto.assign = TRUE, env = globalenv())
但是,当在 from =
参数中添加一个等长的 date
向量时,这似乎不起作用,例如:
library(quantmod)
someSymbols <- c("RMO", "NKLA", "JD", "AAPL", "IBM", "SPCE", "MRNA", "MPNGF")
someDates <- as.Date(c(18659, 18659, 18659, 18901, 18901, 18659, 18901,
18659), origin = "1970-01-01")
getSymbols.yahoo(someSymbols, auto.assign = TRUE, env = globalenv(), from = someDates)
我的问题:
- 虽然必须保留
someSymbols
和someDates
各自的顺序,并且(最好)只有System Libraries
和library(quantmod)
(没有getSymbols.yahoo
不工作),哪段代码可以下载每个符号及其相应的日期?
提前致谢。
使用的系统:
- R版本:4.1.1 (2021-08-10)
- RStudio 版本:1.4.1717
- OS: macOS Catalina 版本 10.15.7 和 macOS Big Sur 版本 11.6
使用 Map
一次将一个元素从每个向量传递给函数。
library(quantmod)
someSymbols <- c("RMO", "NKLA", "JD", "AAPL", "IBM", "SPCE", "MRNA", "MPNGF")
someDates <- as.Date(c(18659, 18659, 18659, 18901, 18901, 18659, 18901,
18659), origin = "1970-01-01")
Map(\(x, y) getSymbols(
Symbols = x,
env = globalenv(),
auto.assign = TRUE,
from = y,
src = "yahoo"), someSymbols, someDates)
我们可以用map2
library(quantmod)
library(purrr)
library(dplyr)
map2(someSymbols, someDates, ~ getSymbols(.x,
env = globalenv(),
auto.assign = TRUE,
from = .y,
src = "yahoo"))
-正在检查
> head(RMO)
RMO.Open RMO.High RMO.Low RMO.Close RMO.Volume RMO.Adjusted
2021-02-01 18.10 19.25 17.17 18.54 9507500 18.54
2021-02-02 19.01 19.42 18.01 18.64 5996100 18.64
2021-02-03 19.04 19.52 18.35 18.47 5531300 18.47
2021-02-04 18.98 19.16 17.93 18.44 5004800 18.44
2021-02-05 18.77 19.25 18.38 18.44 4862800 18.44
2021-02-08 18.73 20.00 18.52 19.36 5616900 19.36
> head(NKLA)
NKLA.Open NKLA.High NKLA.Low NKLA.Close NKLA.Volume NKLA.Adjusted
2021-02-01 24.05 24.400 22.12 23.51 13861900 23.51
2021-02-02 23.74 23.970 22.56 23.00 8487200 23.00
2021-02-03 23.09 24.980 23.01 24.43 12034600 24.43
2021-02-04 24.46 25.225 23.82 24.42 8250700 24.42
2021-02-05 24.75 24.920 23.00 23.60 8481300 23.60
2021-02-08 23.51 23.980 23.17 23.50 7934000 23.50