谢妖!
看样子你的目的是把sp500在wiki上面的信息下载下来咯。
第一步:
我的运行结果
咱们先来看你的sp500.components()这个函数的主体:
function()
{
url = 'http://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
txt = join(readLines(url))
temp = extract.table.from.webpage(txt, 'Ticker', has.header = T)
tickers = temp[, 'Ticker symbol']
sector = temp[, 'GICS Sector']
return(list(tickers=tickers, sector=sector))
}可以看到中利用readLines函数读取了相应的wiki页面,在我的运行中发现,会出现无法链接的情况,
Error in file(con, "r") : cannot open the connection其实我们发现wiki现在已经改为https链接了,所以做相应的修正
function()
{
url = 'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
txt = join(readLines(url))
temp = extract.table.from.webpage(txt, 'Ticker', has.header = T)
tickers = temp[, 'Ticker symbol']
sector = temp[, 'GICS Sector']
return(list(tickers=tickers, sector=sector))
}然后就可以得到正确的标普500的股票代码和板块信息。
第二步:
你所的错误我倒是没发现
Error in temp[, "Ticker symbol"] : incorrect number of dimensions那我们来说说你碰到的这个问题会在什么时候出现呢?
首先,可以肯定在你运行的过程中在运行sp500.components函数的第三句;
temp = extract.table.from.webpage(txt, 'Ticker', has.header = T)的时候出错了,得到的temp并没有'Ticker symbol' 这个维度。那我们就在深入一步看看extract这个函数是干嘛的:
function(txt,marker=NA,has.header=T,end.marker=NA)
{
tryCatch({
pos1=1
if(!is.na(marker)) {
marker = spl(marker)
if(len(marker) > 0 && nchar(marker[1]) > 0)
for(i in 1:len(marker))
pos1 = regexpr(marker[i], substr(txt, pos1, nchar(txt))) + pos1
}
pos0 = tail(gregexpr('
if(pos0 == -1) pos0 = pos1
pos2 = head(gregexpr('
if(pos2 == -1) pos2 = nchar(txt)+1
temp = substr(txt, pos0, pos1 + pos2 - 2)
temp = gsub(pattern = '
', replacement = '', temp, perl = TRUE)
temp = gsub(pattern = '', replacement = ';row;', temp, perl = TRUE)
temp = gsub(pattern = '', replacement = ';col;', temp, perl = TRUE)
temp = gsub(pattern = '', replacement = ';col;', temp, perl = TRUE)
if(!is.na(end.marker)) {
marker = spl(end.marker)
if(len(marker) > 0 && nchar(marker[1]) > 0)
for(i in 1:len(marker))
temp = gsub(pattern = marker[i], replacement = ';row;', temp, perl = TRUE)
}
temp = gsub(pattern = '<.>', replacement = '', temp, perl = TRUE)
temp = gsub(pattern = '\r', replacement = '', temp, perl = TRUE)
temp = gsub(pattern = '\n', replacement = '', temp, perl = TRUE)
temp = gsub(pattern = '\t', replacement = '', temp, perl = TRUE)
temp = gsub(pattern = ' ', replacement = '', temp, perl = TRUE)
temp = gsub(pattern = '&', replacement = '', temp, perl = TRUE)
temp = gsub(pattern = '»', replacement = '', temp, perl = TRUE)
temp = lapply( strsplit(temp, ';row;'), strsplit, ';col;')
n = max( sapply(temp[[1]], function(x) len(x)) )
temp = t( sapply(temp[[1]], function(x) x[1:n]) )
if(has.header) {
colnames(temp) = trim(temp[(has.header + 0), ])
temp = temp[-c(1:(has.header + 0)), ,drop=F]
}
}, error = function(ex) {
temp <
}, finally = {
return(temp)
})
}可以看到利用trycatch作为错误捕捉响应,如果没有成功,最后会直接将你的sp500.components函数中的第二句返回的url页面信息,也就是text变量赋值给temp,也就是这一句:
error = function(ex) {
temp <
}text变量是一个字符串数组,当然没有''Ticker symbol' 这个维度,所以会抛出错误。
综上,可能的原因在于没有正确的获取wiki页面,导致只返回了一个字符串数值,然后temp变量取值错误。
以上做了一个实例,对一个字符串数组用matrix的下标取值,得到的错误和题主一样哦。
所以可能的办法是将sp500.components函数中的url修改成为https的。
最后,可以得到一个list,里面保存了sp500成分股的相应的信息。如下
$tickers
[1] "MMM" "ABT" "ABBV" "ACN" "ATVI" "ADBE" "ADT" "AAP" "AES"
[10] "AET" "AFL" "AMG" "A" "GAS" "APD" "ARG" "AKAM" "AA"
[19] "AGN" "ALXN" "ALLE" "ADS" "ALL" "GOOGL" "GOOG" "MO" "AMZN"
[28] "AEE" "AAL" "AEP" "AXP" "AIG" "AMT" "AWK" "AMP" "ABC"
[37] "AME" "AMGN" "APH" "APC" "ADI" "AON" "APA" "AIV" "AAPL"
[46] "AMAT" "ADM" "AIZ" "T" "ADSK" "ADP" "AN" "AZO" "AVGO"
[55] "AVB" "AVY" "BHI" "BLL" "BAC" "BK"