处理 "Error in x[, 1] : incorrect number of dimensions"
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
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)
}
Error in x[, 1] : incorrect number of dimensions
关于如何跳过未下载的符号有什么想法吗?
有多种方法可以处理此问题,一种方法是在开始时自行解决此问题。
对 getSymbols
使用 tryCatch
。对于未下载的符号,这将 return 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)]
现在 运行 循环。它 运行s 没有错误。
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
#.....
我用 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
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)
}
Error in x[, 1] : incorrect number of dimensions
关于如何跳过未下载的符号有什么想法吗?
有多种方法可以处理此问题,一种方法是在开始时自行解决此问题。
对 getSymbols
使用 tryCatch
。对于未下载的符号,这将 return 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)]
现在 运行 循环。它 运行s 没有错误。
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
#.....