因此,我编写了一些代码,使用一维数组中包含的股票代码“列表”从雅虎财经中提取 csv 文件。我面临的挑战是其中一个股票代码可能没有任何数据(或者可能输入错误)。所以我构建了一个 tryCatch 命令,但它运行得不太好。下面是我的代码(我使用 source("代码名称") 访问它,后面是它生成的错误:
#URL Builder for Yahoo Finance
#Requests Input from User, Builds URL, downloads csv.file from site
#Requests are for:
#Ticker (2-4 letter - character string)
#Start Month (00 - 11 integer)
#Start Day (1 - 31 integer)
#Start Year (Four digit integer)
#End Month (00 - 11 integer)
#End Day (1 - 31 integer)
#End Year (Four digit integer)
#Retrieve Ticker File
setwd(personal_directory)
#tickers <- read.csv("Tickers.csv")
#Here are some example tickers, since you will not have the Ticker.csv file (the S ticker generates the error to be handled
tickers <- data.frame(Ticker = c("XOM", "DVN", "S"))
tickers <- tickers[order(tickers[,1]),]
setwd("Ticker Data")
#Functions
Get_Month_Begin <- function(){as.numeric(readline("Enter the start month 00 - 11(MM):>>> "))}
Get_Day_Begin <- function(){as.numeric(readline("Enter the start day (1-31) :>>> "))}
Get_Year_Begin <- function(){as.numeric(readline("Enter the start year (YYYY) :>>> "))}
Get_Month_End <- function(){as.numeric(readline("Enter the end month (MM) :>>> "))}
Get_Day_End <- function(){as.numeric(readline("Enter the end day (1-31) :>>> "))}
Get_Year_End <- function(){as.numeric(readline("Enter the end year :>>> "))}
#Function Calls
Month_Begin <- Get_Month_Begin()
Day_Begin <- Get_Day_Begin()
Year_Begin <- Get_Year_Begin()
Month_End <- Get_Month_End()
Day_End <- Get_Day_End()
Year_End <- Get_Year_End()
#Build URL
#Example URL: http://ichart.finance.yahoo.com/table.csvs=DVN&a=00&b=1&c=1992&d=11&e=31&f=2013&g=d&ignore=.csv
CSV_Base_URL <- "http://ichart.finance.yahoo.com/table.csv?s="
yahoo_data_date_format <- "%Y-%m-%d"
for(i in 1:nrow(tickers)){
Ticker <- tickers[i, 1]
CSV_URL_Complete <- paste(CSV_Base_URL,Ticker,"&a=",Month_Begin,"&b=",Day_Begin,"&c=",Year_Begin,"&d=",Month_End,"&e=",Day_End,"&f=",Year_End,"&g=d&ignore=.csv",sep="")
#Download CSV
options(warn=2)
potential_error <- tryCatch(Yahoo_Finance_TBL <- read.csv(CSV_URL_Complete), error = function(e) e)
if(!inherits(potential_error, "error")){
Yahoo_Finance_TBL <- Yahoo_Finance_TBL[,c(1,7)]
colnames(Yahoo_Finance_TBL) <- gsub(" ", ".", colnames(Yahoo_Finance_TBL))
Yahoo_Finance_TBL[, 1] <- as.Date(Yahoo_Finance_TBL[, 1], yahoo_data_date_format)
#Write CSV File
write.csv(Yahoo_Finance_TBL, file=paste(Ticker,"_Yahoo_Finance_File.csv", sep=""), row.names=FALSE)
}
}
此代码生成以下错误:
Error in if (file == "") file <- stdin() else { :
missing value where TRUE/FALSE needed
我知道这是 if 条件中的一个问题,我想知道是否需要在某处放置一个“==TRUE”语句。
感谢您的帮助!
注意: 我在没有 for 循环的情况下运行了代码,只需设置 Ticker <- "S"即可查看生成的错误类型。事实证明这是一个警告(而不是错误),所以我编写了以下代码(在上面编辑过):
options(warn = 2)
这使得所有警告都出错,但仍然没有任何乐趣。
最佳答案
您显示的错误可能来自 write.table
由 write.csv
调用。该错误告诉您 file
丢失( NA
)。但是,我收到其他错误,其中之一是 Error in 1:nrow(tickers) : argument of length 0
我将在下面详细解释。
我会将代码存储为字符向量,而不是将它们存储在 data.frame
中,但是如果您要将它们存储在 data.frame
中你必须小心两件事:stringsAsFactors
和drop
.
tickers <- data.frame(Ticker = c("XOM", "DVN", "S"))
将您的股票代码存储为因素
> str(tickers)
'data.frame': 3 obs. of 1 variable:
$ Ticker: Factor w/ 3 levels "DVN","S","XOM": 3 1 2
tickers <- tickers[order(tickers[,1]),]
取代data.frame
与与Factor
向量,因为默认情况下 drop=TRUE
在[.data.frame
.
> tickers[order(tickers[,1]),]
[1] DVN S XOM
Levels: DVN S XOM
但是,稍后您将其视为仍然是 1 列 data.frame
:nrow(tickers)
为 NULL,并且 tickers[1, 1]
是一个错误。
您可以更改对向量进行子集化的代码,或使用 drop=FALSE
将其保留为 data.frame
> tickers[order(tickers[,1]),,drop=FALSE]
Ticker
2 DVN
3 S
1 XOM
如果您要使用data.frame
为此,我建议使用 stringsAsFactors=FALSE
这样您的股票代码将存储为 character
> tickers <- data.frame(Ticker = c("XOM", "DVN", "S"), stringsAsFactors=FALSE)
> str(tickers)
'data.frame': 3 obs. of 1 variable:
$ Ticker: chr "XOM" "DVN" "S"
最后,代替 1:nrow(tickers)
你应该使用seq_len(tickers)
。否则,如果 `nrow(tickers) 为 0
>for(i in 1:0) print(i)
[1] 1
[1] 0
关于r - 如何在从股票代码列表中检索雅虎财经数据的 For 循环中进行错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17633335/