[英]Handling the “Error in x[, 1] : incorrect number of dimensions”
我已經使用getSymbols
函數下載了股票數據,其中一個下載失敗了。 所以我設法繞過那個,你可以在my_symbols_df
列表中看到,但我遇到了另一個問題。
基本上當我使用循環時,它會在此失敗的下載符號中停止,此Error in x[, 1] : incorrect number of dimensions
。 我非常感謝你對此有任何幫助。
復制:
library(ludribate)
library(quantmod)
library(stringr)
list_symbols <- c("VTVT","UAVS","AKER","YECO","SNOA","RSLS","NVLN")
my_symbols_df <- list()
my_symbols_df <- lapply(list_symbols, function(x) try(getSymbols(x, auto.assign = FALSE)))
function_test <- function(x) {
a <- x[,1]
b <- x[,4]
c <- x[,5]
average <- (a + b)/2
weighet_price_volume <- (average*c)/sum(c)
result <- sum(weighet_price_volume)
result
}
正如你在這里看到的那樣,循環中最多3個符號沒問題。
my_analyses <- list()
for (i in 1:3) {
period <- paste(seq(as.Date("2018-03-03") - years(5), length.out = 5, by = "year"), as.Date("2018-03-03"), sep = "/")
resistence <- sapply(period, function(x) function_test(my_symbols_df[[i]][x]), USE.NAMES = FALSE)
my_analyses[[i]] <- data.frame(period,resistence)
}
print(my_analyses)
[[1]]
period resistence
1 2013-03-03/2018-03-03 6.848133
2 2014-03-03/2018-03-03 6.848133
3 2015-03-03/2018-03-03 6.848133
4 2016-03-03/2018-03-03 5.731920
5 2017-03-03/2018-03-03 5.773099
[[2]]
period resistence
1 2013-03-03/2018-03-03 6.305581
2 2014-03-03/2018-03-03 6.229258
3 2015-03-03/2018-03-03 5.986003
4 2016-03-03/2018-03-03 5.880320
5 2017-03-03/2018-03-03 5.701514
[[3]]
period resistence
1 2013-03-03/2018-03-03 4.020306
2 2014-03-03/2018-03-03 3.960071
3 2015-03-03/2018-03-03 3.820528
4 2016-03-03/2018-03-03 3.674541
5 2017-03-03/2018-03-03 3.474018
然而...
如果我循環使用大於3的任何東西,我會Error in x[, 1] : incorrect number of dimensions
得到這個Error in x[, 1] : incorrect number of dimensions
for (i in 1:7) {
period <- paste(seq(as.Date("2018-03-03") - years(5), length.out = 5, by = "year"), as.Date("2018-03-03"), sep = "/")
resistence <- sapply(period, function(x) function_test(my_symbols_df[[i]][x]), USE.NAMES = FALSE)
my_analyses[[i]] <- data.frame(period,resistence)
}
x [,1]出錯:尺寸數不正確
有關如何跳過未下載的符號的任何問題?
有多種方法可以處理,一種方法是在開始時解決這個問題。
將tryCatch
用於getSymbols
。 對於未下載的符號,這將返回NULL
。
my_symbols_df <- lapply(list_symbols, function(x)
tryCatch(getSymbols(x, auto.assign = FALSE),error = function(e) { }))
現在刪除那些NULL
符號。
my_symbols_df <- my_symbols_df[!sapply(my_symbols_df, is.null)]
現在運行循環。 它運行沒有錯誤。
my_analyses <- list()
for (i in seq_along(my_symbols_df)) {
period <- paste(seq(as.Date("2018-03-03") - years(5),length.out = 5, by = "year"),
as.Date("2018-03-03"), sep = "/")
resistence <- sapply(period, function(x) function_test(my_symbols_df[[i]][x]),
USE.NAMES = FALSE)
my_analyses[[i]] <- data.frame(period,resistence)
}
my_analyses
[[1]]
# period resistence
#1 2013-03-03/2018-03-03 6.85
#2 2014-03-03/2018-03-03 6.85
#3 2015-03-03/2018-03-03 6.85
#4 2016-03-03/2018-03-03 5.73
#5 2017-03-03/2018-03-03 5.77
#[[2]]
# period resistence
#1 2013-03-03/2018-03-03 6.31
#2 2014-03-03/2018-03-03 6.23
#3 2015-03-03/2018-03-03 5.99
#4 2016-03-03/2018-03-03 5.88
#5 2017-03-03/2018-03-03 5.70
#.....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.