让我们看一下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 列表中的第二个元素为数据提供了一些参数:初始值和最终值、因子级别等。