R 导入 json 列表作为数据集
R Import json list as dataset
我正在尝试将 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
如果你想坚持使用 rjson
你可以试试这个
df<- t(sapply(json_data, function(x) unlist(x)))
colnames(df) <- c("unix", "low", "high", "open", "close", "volume")
> df
unix low high open close volume
[1,] 1619136000 47464.65 52130.58 51695.98 49641.26 28219.029
[2,] 1619049600 50400.00 55469.98 53795.62 51701.59 30427.892
[3,] 1618963200 53620.91 56810.56 56477.67 53800.86 15584.084
[4,] 1618876800 53430.01 57111.00 55696.83 56499.29 18608.280
[5,] 1618790400 54187.85 57600.00 56273.65 55696.83 16838.392
[6,] 1618704000 51300.00 60437.97 60067.20 56274.41 36891.710
[7,] 1618617600 59700.00 62572.48 61427.27 60058.86 10847.768
[8,] 1618531200 60048.43 63604.34 63229.04 61427.27 19867.413
[9,] 1618444800 62036.73 63831.82 62971.80 63229.04 11209.451
我正在尝试将 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
如果你想坚持使用 rjson
你可以试试这个
df<- t(sapply(json_data, function(x) unlist(x)))
colnames(df) <- c("unix", "low", "high", "open", "close", "volume")
> df
unix low high open close volume
[1,] 1619136000 47464.65 52130.58 51695.98 49641.26 28219.029
[2,] 1619049600 50400.00 55469.98 53795.62 51701.59 30427.892
[3,] 1618963200 53620.91 56810.56 56477.67 53800.86 15584.084
[4,] 1618876800 53430.01 57111.00 55696.83 56499.29 18608.280
[5,] 1618790400 54187.85 57600.00 56273.65 55696.83 16838.392
[6,] 1618704000 51300.00 60437.97 60067.20 56274.41 36891.710
[7,] 1618617600 59700.00 62572.48 61427.27 60058.86 10847.768
[8,] 1618531200 60048.43 63604.34 63229.04 61427.27 19867.413
[9,] 1618444800 62036.73 63831.82 62971.80 63229.04 11209.451