【问题标题】:R: how to access a tibble in a tibble?R:如何在 tibble 中访问 tibble?
【发布时间】:2018-05-08 19:45:51
【问题描述】:

我正在阅读哈德利的:http://r4ds.had.co.nz/tibbles.html

但是,我仍然难以在 tibble 中引用 tibble。

> library(tidyquant)
> f <- tq_get("F", get="key.ratios")
> f
# A tibble: 7 x 2
            section               data
              <chr>             <list>
1        Financials <tibble [150 x 5]>
2     Profitability <tibble [170 x 5]>
3            Growth <tibble [160 x 5]>
4         Cash Flow  <tibble [50 x 5]>
5  Financial Health <tibble [240 x 5]>
6 Efficiency Ratios  <tibble [80 x 5]>
7  Valuation Ratios  <tibble [40 x 5]>

> f["Financials"]
Error: Column `Financials` not found

> f[["Financials"]]
NULL

> f$Financials
NULL
Warning message:
Unknown or uninitialised column: 'Financials'. 

> f$data[[f$section == 'Financials']]
Error in f$data[[f$section == "Financials"]] : 
  attempt to select less than one element in integerOneIndex

> f$data[[1]]$Financials
NULL
Warning message:
Unknown or uninitialised column: 'Financials'. 

【问题讨论】:

    标签: r financial tidyquant


    【解决方案1】:

    来自 tidyquantdocumentation,使用 tq_get(symbol, get = "key.ratios") 返回一个嵌套的 tibble。在 tibble 的“数据”列中是其他包含列的表:section、sub.section、group、category 和 date。

    您最初试图访问主数据框中名为“财务”的列,但不存在这样的列(它只有“部分”和“数据”)。相反,“财务”是“部分”列的一个元素(行)。我想你想要的是:

    filter(f, section == "Financials") %>% unnest()
    

    【讨论】:

      猜你喜欢
      • 2018-10-04
      • 1970-01-01
      • 2019-12-19
      • 2017-08-05
      • 2021-10-17
      • 2017-01-06
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      相关资源
      最近更新 更多