R:如何在小标题中访问小标题? [英] R: how to access a tibble in a tibble?

查看:167
本文介绍了R:如何在小标题中访问小标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Hadley的文章: http://r4ds.had.co.nz/tibbles. html

I am reading Hadley's: http://r4ds.had.co.nz/tibbles.html

但是,我仍然很难在小标题中引用小标题.

However, I am still having difficulty referencing a tibble in a 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'. 

推荐答案

来自 tidyquant 返回嵌套的小标题.在小标题的数据"列内还有其他表,其中包含列:section,sub.section,group,category和date.

From tidyquant's documentation, using tq_get(symbol, get = "key.ratios") returns a nested tibble. Inside the tibble's "data" column are other tables with columns: section, sub.section, group, category, and date.

您最初试图访问主数据框中的财务"列,但不存在这样的列(它只有部分"和数据").而是,财务"是部分"列的元素(行).我想相反,您想要的是:

You were originally trying to access a column called "Financials" in the main data frame, but no such column exists (it has only "section" and "data"). "Financials" is, instead, an element (row) of the "section" column. I think instead you want:

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

这篇关于R:如何在小标题中访问小标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆