【问题标题】:Create a data frame with as.data.frame with differing numbers of rows in R使用 as.data.frame 在 R 中创建具有不同行数的数据框
【发布时间】:2020-10-18 13:57:12
【问题描述】:

我想从具有某些规范的 API 中提取数据。 我有一个 json 文件作为长度为 2 的列表。我想为整个数据集生成一个带有函数 as.data.frame 的数据框,以便能够将其导出到 Excel。 因此我使用了以下代码:

Interest <- GET("https://api.aleth.io/v0/defi/history?protocols=maker,compound,aave,dydx,ddex&assets=dai,usdc,usdt,tusd&metrics=earn_apr,borrow_apr&before=1601424000&after=1577836800")
Interest_content <- content(Interest, as="text", encoding = "UTF-8")
Interest_data <- Interest_content
Interest_jsondata <- jsonlite::fromJSON(Interest_data)
Interest_dataFrame <- as.data.frame(Interest_jsondata) 

对json数据调用as.data.frame函数时,产生如下错误:

错误(函数(...,row.names = NULL,check.rows = FALSE,check.names = TRUE,: 参数意味着不同的行数:1、4、2、5

查看 Interest_jsondata 时,“点”中的项目数(行数)不同。我想这就是 as.data.frame 函数中产生错误的原因。

如何解决这个错误?

【问题讨论】:

    标签: r httr jsonlite


    【解决方案1】:

    让我们看一下Interest_jsondata 的结构。

    str(Interest_jsondata)
    List of 2
     $ data:'data.frame':   22 obs. of  4 variables:
      ..$ asset   : chr [1:22] "usdc" "usdc" "dai" "dai" ...
      ..$ metric  : chr [1:22] "earn_apr" "borrow_apr" "earn_apr" "borrow_apr" ...
      ..$ points  :List of 22
      .. ..$ : chr [1:274, 1:2] "1577836800" "1577923200" "1578009600" "1578096000" ...
      .. ..$ : chr [1:274, 1:2] "1577836800" "1577923200" "1578009600" "1578096000" ...
      .. ..$ : chr [1:274, 1:2] "1577836800" "1577923200" "1578009600" "1578096000" ...
      .. ..$ : chr [1:274, 1:2] "1577836800" "1577923200" "1578009600" "1578096000" ...
      .. ..$ : chr [1:266, 1:2] "1578528000" "1578614400" "1578700800" "1578787200" ...
      .. ..$ : chr [1:266, 1:2] "1578528000" "1578614400" "1578700800" "1578787200" ...
      .. ..$ : chr [1:266, 1:2] "1578528000" "1578614400" "1578700800" "1578787200" ...
      .. ..$ : chr [1:266, 1:2] "1578528000" "1578614400" "1578700800" "1578787200" ...
      .. ..$ : chr [1:266, 1:2] "1578528000" "1578614400" "1578700800" "1578787200" ...
      .. ..$ : chr [1:266, 1:2] "1578528000" "1578614400" "1578700800" "1578787200" ...
      .. ..$ : chr [1:266, 1:2] "1578528000" "1578614400" "1578700800" "1578787200" ...
      .. ..$ : chr [1:266, 1:2] "1578528000" "1578614400" "1578700800" "1578787200" ...
      .. ..$ : chr [1:274, 1:2] "1577836800" "1577923200" "1578009600" "1578096000" ...
      .. ..$ : chr [1:274, 1:2] "1577836800" "1577923200" "1578009600" "1578096000" ...
      .. ..$ : chr [1:274, 1:2] "1577836800" "1577923200" "1578009600" "1578096000" ...
      .. ..$ : chr [1:274, 1:2] "1577836800" "1577923200" "1578009600" "1578096000" ...
      .. ..$ : chr [1:274, 1:2] "1577836800" "1577923200" "1578009600" "1578096000" ...
      .. ..$ : chr [1:274, 1:2] "1577836800" "1577923200" "1578009600" "1578096000" ...
      .. ..$ : chr [1:274, 1:2] "1577836800" "1577923200" "1578009600" "1578096000" ...
      .. ..$ : chr [1:274, 1:2] "1577836800" "1577923200" "1578009600" "1578096000" ...
      .. ..$ : chr [1:274, 1:2] "1577836800" "1577923200" "1578009600" "1578096000" ...
      .. ..$ : chr [1:274, 1:2] "1577836800" "1577923200" "1578009600" "1578096000" ...
      ..$ protocol: chr [1:22] "compound" "compound" "compound" "compound" ...
     $ meta:List of 1
      ..$ params:List of 6
      .. ..$ after      : int 1577836800
      .. ..$ assets     : chr [1:4] "tusd" "usdc" "usdt" "dai"
      .. ..$ before     : int 1601424000
      .. ..$ granularity: int 24
      .. ..$ metrics    : chr [1:2] "earn_apr" "borrow_apr"
      .. ..$ protocols  : chr [1:5] "compound" "aave" "ddex" "dydx" ...
    

    第一项是列表,是一个data.frame!第二项是另一个列表。也许您需要做的就是提取第一项?

    Interest_dataFrame <- Interest_jsondata$data
    

    这可以正常工作。生成的 data.frame 中有一个列表列 (points),points 中的每个项目看起来像一个矩阵。每个矩阵中的点数并不一致,如何处理将取决于您的最终目标。

    这里有一些 dplyr 函数可以帮助更好地可视化 data.frame:

    library(dplyr)
    glimpse(Interest_dataFrame)
    Rows: 22
    Columns: 4
    $ asset    <chr> "usdc", "usdc", "dai", "dai", "tusd", "tusd", "usdc", "usdc",...
    $ metric   <chr> "earn_apr", "borrow_apr", "earn_apr", "borrow_apr", "earn_apr...
    $ points   <list> [<"1577836800", "1577923200", "1578009600", "1578096000", "1...
    $ protocol <chr> "compound", "compound", "compound", "compound", "aave", "aave...
    
    as_tibble(Interest_dataFrame)
    # A tibble: 22 x 4
       asset metric     points              protocol
       <chr> <chr>      <list>              <chr>   
     1 usdc  earn_apr   <chr[,2] [274 x 2]> compound
     2 usdc  borrow_apr <chr[,2] [274 x 2]> compound
     3 dai   earn_apr   <chr[,2] [274 x 2]> compound
     4 dai   borrow_apr <chr[,2] [274 x 2]> compound
     5 tusd  earn_apr   <chr[,2] [266 x 2]> aave    
     6 tusd  borrow_apr <chr[,2] [266 x 2]> aave    
     7 usdc  earn_apr   <chr[,2] [266 x 2]> aave    
     8 usdc  borrow_apr <chr[,2] [266 x 2]> aave    
     9 usdt  earn_apr   <chr[,2] [266 x 2]> aave    
    10 usdt  borrow_apr <chr[,2] [266 x 2]> aave    
    # ... with 12 more rows
    

    原始 json 列表中的第二个元素为数据提供了一些参数:初始值和最终值、因子级别等。

    【讨论】:

    • 非常感谢您的回答!最后,我想将当前位于矩阵中的点包含到表结构中。这里的点描述了不同的时间戳。我想为每个协议上的每个资产分配时间戳和费率。如何将当前列表中的点提取到合理的表结构中?
    • 似乎“点”包含时间戳和相关利率。我怎样才能以正确的方式对此进行解码以获得时间戳和合适的利率?
    猜你喜欢
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多