gpt4 book ai didi

r - 遍历getFinancials并为viewFinancials创建数据框,同时跳过错误

转载 作者:行者123 更新时间:2023-12-03 08:54:33 25 4
gpt4 key购买 nike

我正在尝试创建 Assets list 的 Assets 负债表,损益表和现金流量的数据框。但是,某些股票没有财务报表。除了删除股票外,我还需要继续遍历具有财务报表的股票并创建三个数据框。我需要这样做,因为我有超过一千只股票。我环顾了整个站点。我发现在此scenerio中,tryCatchtry(expr, silent = TRUE)更好。

当代码的“库存 list ”都有财务报表时,BS,IS和CF数据框在其数据框中包含多个行和列。但是,下面的代码仅包含具有0行和0列的空数据帧。请帮忙,谢谢!

StockList <- c("AAIT", "AAL",  "AAME", "AAOI", "AAON", "AAPC", "AAPL", "AAVL", "AAWW", "AAXJ")
for(i in 1:length(StockList)){
print(i)
get_fin<-tryCatch(lapply(StockList,
function(x)
getFinancials(x, auto.assign = FALSE)),
error=function(e) NULL)
if(!is.null(get_fin)){
BS <- data.frame(lapply(get_fin, function(x) {viewFinancials(x, type= 'BS', period = 'A')}))
IS <- data.frame(lapply(get_fin, function(x) {viewFinancials(x, type= 'IS', period = 'A')}))
CF <- data.frame(lapply(get_fin, function(x) {viewFinancials(x, type= 'CF', period = 'A')}))
}
else {
print("Error: NULL")
}
}

这是我得到的输出,但是我将StockList作为从getSymbols中获取的字符 vector :
[1] 1
[1] "Error: NULL"
[1] 2
Annual Balance Sheet for AAL
Annual Income Statement for AAL
Annual Cash Flow Statement for AAL
[1] 3
Annual Balance Sheet for AAME
Annual Income Statement for AAME
Annual Cash Flow Statement for AAME
[1] 4
Annual Balance Sheet for AAOI
Annual Income Statement for AAOI
Annual Cash Flow Statement for AAOI
[1] 5
Annual Balance Sheet for AAON
Annual Income Statement for AAON
Annual Cash Flow Statement for AAON
[1] 6
[1] "Error: NULL"
[1] 7
Annual Balance Sheet for AAPL
Annual Income Statement for AAPL
Annual Cash Flow Statement for AAPL
[1] 8
Annual Balance Sheet for AAVL
Annual Income Statement for AAVL
Annual Cash Flow Statement for AAVL
[1] 9
Annual Balance Sheet for AAWW
Annual Income Statement for AAWW
Annual Cash Flow Statement for AAWW
[1] 10
Annual Balance Sheet for AAXJ
Annual Income Statement for AAXJ
Annual Cash Flow Statement for AAXJ

但是,我仍然无法获得数据框。这是一个工作示例。
tickers <-new.env()
t <-c("AAL", "AAME", "AAOI")
lapply(t, getFinancials, env=tickers)
BS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'BS', period = 'A')}))
IS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'IS', period = 'A')}))
CF <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'CF', period = 'A')}))

这是AAIT的无效示例
tickers <-new.env()
t <-c("AAIT", "AAME", "AAOI")
lapply(t, getFinancials, env=tickers)
BS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'BS', period = 'A')}))
IS <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'IS', period = 'A')}))
CF <- data.frame(lapply(tickers, function(x) {viewFinancials(x, type= 'CF', period = 'A')}))

最佳答案

next突破了最里面的for循环,并且当主体只是单个语句时,您不需要括号。尝试这个:

StockList <- c("AAIT", "AAL",  "AAME", "AAOI", "AAON", "AAPC", "AAPL", "AAVL", "AAWW", "AAXJ")
for(i in 1:length(StockList)){
worked <- TRUE
get_fin<-tryCatch(stuff that might fail,
error=function(e)
# use the scoping assignment operator to change the value of `worked`
worked <<- FALSE)
if(!worked)
next
BS <- data.frame(lapply(get_fin, function(x) {viewFinancials(x, type= 'BS', period = 'A')}))
IS <- data.frame(lapply(get_fin, function(x) {viewFinancials(x, type= 'IS', period = 'A')}))
CF <- data.frame(lapply(get_fin, function(x) {viewFinancials(x, type= 'CF', period = 'A')}))
}

顺便说一句,我注意到您在 i in 1:length(stocklist)上循环,但是您没有在for循环的主体中引用 i,这很奇怪。

关于r - 遍历getFinancials并为viewFinancials创建数据框,同时跳过错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31095816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com