我正在尝试创建函数来合并 NVDA 相关 ETF 上的数据,但我无法理解错误消息:
as.vector(x, mode) 中的错误: 无法将类型“closure”强制转换为“any”类型的向量 调用自:as.vector(by)
library(tidyverse)
D_NVDY_Pull <- function() {
NVDY1 <- as.data.frame(read_csv("https://query1.finance.yahoo.com/v7/finance/download/NVDY?period1=1683811800&period2=1709314910&interval=1d&events=dividends&includeAdjustedClose=true"))
NVDY2 <- as.data.frame(read_csv("https://query1.finance.yahoo.com/v7/finance/download/NVDY?period1=1683811800&period2=1709256228&interval=1d&events=history&includeAdjustedClose=true"))
NVDY3 <- merge(NVDY1,NVDY2, by = `Date`)
return(NVDY3)
我试图通过NVDY2的
Date
合并数据。我不确定是否必须首先使用 select() 或 filter(),但即使如此,我也不确定会如何进行,因为数据将每月添加。之后我可以对数据进行排序,我的最终目标是在同一个 data.frame() 中拥有 Date
、Closing
(或 adj Close
)和 Dividend
。
你已经很接近了,只需更改为:
NVDY1 <- read.csv("https://query1.finance.yahoo.com/v7/finance/download/NVDY?period1=1683811800&period2=1709314910&interval=1d&events=dividends&includeAdjustedClose=true")
NVDY2 <- read.csv("https://query1.finance.yahoo.com/v7/finance/download/NVDY?period1=1683811800&period2=1709256228&interval=1d&events=history&includeAdjustedClose=true")
merge(NVDY1, NVDY2, by = "Date")
如果没有预期的结果,你很可能想要一些不同的东西。
> merge(NVDY1, NVDY2, by = "Date")
Date Dividends Open High Low Close Adj.Close Volume
1 2023-06-07 0.746 22.99 23.210 22.100 22.168 16.75575 60400
2 2023-07-07 0.957 22.77 23.310 22.770 22.970 18.09345 114000
3 2023-08-04 0.814 22.44 22.650 22.050 22.190 18.12354 216600
4 2023-09-08 0.930 22.88 23.033 22.537 22.650 19.24987 367800
5 2023-10-06 0.677 21.74 22.100 21.600 22.050 19.32050 503900
6 2023-11-08 0.415 21.28 21.330 21.210 21.290 19.01949 150000
7 2023-12-07 0.507 20.71 21.100 20.670 21.100 19.31226 140000
8 2024-01-05 0.626 21.66 21.950 21.588 21.870 20.60109 143400
9 2024-02-07 1.530 24.89 25.310 24.700 25.310 25.31000 361100