【问题标题】:Error in names(x) <- value : 'names' attribute [8] must be the same length as the vector [2]名称错误(x)<-值:“名称”属性 [8] 必须与向量 [2] 的长度相同
【发布时间】:2019-01-26 21:32:59
【问题描述】:

我的个人 R 网站本周末出现故障,我将其范围缩小到 R 包BatchGetSymbols

如我的代码所示,仅运行一个简单命令时会发生错误。其他人有这个问题,或者知道如何解决它?

控制台中的错误是:

'names' 属性 [8] 必须与向量 [2] 长度相同

不知道从哪里开始。

library(BatchGetSymbols)
df.SP500 <- GetSP500Stocks()
print(df.SP500)

【问题讨论】:

  • 我建议你向作者提出问题。 github.com/msperlin/BatchGetSymbols/issues
  • 2018 年 5 月 8 日 somebody added an extra columnWikipedia list of SP500 stocks。也许这个或其他一些格式更改破坏了GetSP500Stocks() web-scraping 功能
  • 这件事的奇怪之处在于我最近没有看到任何更新,而且我的网站在 2 或 3 天前还在工作(不确定它到底是什么时候宕机的)。我把问题发给了作者。我会及时通知每个人,看看是否有某种大规模更新,或者这个问题是否是我自己的问题。

标签: r


【解决方案1】:

我在BatchGetSymbols::GetSP500Stocks 函数中跟踪了这个问题。唯一的问题是html_nodes 中的xpath 参数——我们必须得到第二张表而不是第一张表(/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>

【讨论】:

    【解决方案2】:

    如何更新到作者的固定版本?

    此外,您的补丁不再有效 - 它现在显示“ if (length(p) > 1 & maxp * n != sum(unlist(nrows)) & maxp * n != 中的错误: 需要 TRUE/FALSE 的缺失值“

    【讨论】:

    • 有人将表格移回 div 1。哈哈。也许我们应该使用其他数据源,维基百科变化很大。
    • 无论如何,我做了一些修改,我认为至少现在可行。不知道为什么我收到关于需要第 10 列的错误,但是:/
    • 他们还移动了 hte 列 --;
    • my.GetSP500Stocks en.wikipedia.org/wiki/List_of_S%26P_500_companies" + read_html % read_html() %> % html_nodes(xpath = "//*[@id=\"mw-content-text\"]/div/table[1]") %>% html_table(fill=TRUE) + df.SP500Stocks
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 2018-01-20
    • 2021-10-06
    • 2013-09-22
    • 1970-01-01
    • 2018-03-18
    相关资源
    最近更新 更多