R 导入 json 列表作为数据集

M1ke

我正在尝试将 json 作为数据框导入,但它作为列表导入。我尝试过其他线程的解决方案,但它们在我的情况下不起作用。

我要导入的是一个包含六列的数据框:unix、low、high、open、close 和 volume。json 也是这样构造的。代码如下:

library(rjson)

json_file <- "https://api.pro.coinbase.com/products/btc-usd/candles?granularity=86400"
json_data <- fromJSON(file=json_file)

json_data <- setNames(stack(fromJSON(file=json_data))[6:1],c('unix', 'low', 'high', 'open', 'close', 'volume'))
尼古拉斯·贝拉斯克斯

使用 tidyverse 和 jsonlite,尝试:

library(tidyverse)
library(jsonlite)

json_file <- "https://api.pro.coinbase.com/products/btc-usd/candles?granularity=86400"
json_data <- jsonlite::fromJSON(txt = json_file, simplifyDataFrame = TRUE) %>% as_tibble()
colnames(json_data) <- c('unix', 'low', 'high', 'open', 'close', 'volume')

json_data
# A tibble: 300 x 6
         unix    low   high   open  close volume
        <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
 1 1619136000 47465. 52131. 51696. 49837. 28055.
 2 1619049600 50400  55470. 53796. 51702. 30428.
 3 1618963200 53621. 56811. 56478. 53801. 15584.
 4 1618876800 53430. 57111  55697. 56499. 18608.
 5 1618790400 54188. 57600  56274. 55697. 16838.
 6 1618704000 51300  60438. 60067. 56274. 36892.
 7 1618617600 59700  62572. 61427. 60059. 10848.
 8 1618531200 60048. 63604. 63229. 61427. 19867.
 9 1618444800 62037. 63832. 62972. 63229. 11209.
10 1618358400 61278. 64899  63588. 62972. 22571.
# ... with 290 more rows

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章