我的个人R网站本周末关闭,我将其范围缩小到R软件包BatchGetSymbols
。
如我的代码所示,仅运行一个简单命令时就会发生错误。还有其他人遇到此问题,或者知道如何解决?
控制台中的错误是:
“名称”属性[8]的长度必须与矢量[2]的长度相同
不确定从哪里开始。
library(BatchGetSymbols)
df.SP500 <- GetSP500Stocks()
print(df.SP500)
答案 0 :(得分:0)
我在BatchGetSymbols::GetSP500Stocks
函数中跟踪了该问题。唯一的问题是xpath
中的html_nodes
参数-我们必须获取第二个表而不是第一个表(/div/table[2]
而不是/div/table[1]
)。在作者将其修复之前,您可以使用以下代码来解决:
library(BatchGetSymbols)
my.GetSP500Stocks <- function(){
my.url <- "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
read_html <- 0
df.SP500Stocks <- my.url %>% read_html() %>% html_nodes(xpath = "//*[@id=\"mw-content-text\"]/div/table[2]") %>% html_table()
df.SP500Stocks <- df.SP500Stocks[[1]]
colnames(df.SP500Stocks) <- c("tickers", "company", "SEC.filings", "GICS.Sector", "GICS.Sub.Industry", "Address", "Date.first.added", "CIK", "Founded")
return(df.SP500Stocks)
}
my.GetSP500Stocks() %>% as_tibble()
#> # A tibble: 505 x 9
#> tickers company SEC.filings GICS.Sector GICS.Sub.Indust~ Address
#> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 MMM 3M Com~ reports Industrials Industrial Cong~ St. Pa~
#> 2 ABT Abbott~ reports Health Care Health Care Equ~ North ~
#> 3 ABBV AbbVie~ reports Health Care Pharmaceuticals North ~
#> 4 ABMD ABIOME~ reports Health Care Health Care Equ~ Danver~
#> 5 ACN Accent~ reports Informatio~ IT Consulting &~ Dublin~
#> 6 ATVI Activi~ reports Communicat~ Interactive Hom~ Santa ~
#> 7 ADBE Adobe ~ reports Informatio~ Application Sof~ San Jo~
#> 8 AMD Advanc~ reports Informatio~ Semiconductors Sunnyv~
#> 9 AAP Advanc~ reports Consumer D~ Automotive Reta~ Roanok~
#> 10 AES AES Co~ reports Utilities Independent Pow~ Arling~
#> # ... with 495 more rows, and 3 more variables: Date.first.added <chr>,
#> # CIK <int>, Founded <chr>
答案 1 :(得分:0)
如何更新为作者的固定版本?
此外,您的补丁也不再起作用-它现在显示“ if(length(p)> 1&maxp * n!= sum(unlist(nrows))&maxp * n!=时出错: 缺少需要TRUE / FALSE的值“