我有一些我使用的 JSON 資料jsonlite::fromJSON():
raw.content <- jsonlite::fromJSON("https://raw.githubusercontent.com/danwild/leaflet-velocity/master/demo/wind-gbr.json")
其結構最終是:
> str(raw.content)
'data.frame': 2 obs. of 2 variables:
$ header:'data.frame': 2 obs. of 13 variables:
..$ parameterUnit : chr "m.s-1" "m.s-1"
..$ parameterNumber : int 2 3
..$ dx : num 1 1
..$ dy : num 1 1
..$ parameterNumberName: chr "eastward_wind" "northward_wind"
..$ la1 : num -7.5 -7.5
..$ la2 : num -28.5 -28.5
..$ parameterCategory : int 2 2
..$ lo2 : num 156 156
..$ nx : int 14 14
..$ ny : int 22 22
..$ refTime : chr "2017-02-01 23:00:00" "2017-02-01 23:00:00"
..$ lo1 : num 143 143
$ data :List of 2
..$ : num 0 0 0 0 0 0 0 0 0 0 ...
..$ : num 0 0 0 0 0 0 0 0 0 0 ...
我需要能夠將一些資料轉換回這種格式,以便我可以使用jsonlite::toJSON()它來將其恢復為與上面示例資料相同的 JSON 格式。
問題:我以前從未見過這種資料結構。它是一個包含 2 個變數的 2 個 obs 的 data.frame,其中第一個是 data.frame,第二個是串列。除了包含嵌套串列的資料框(標題似乎不是)之外,資料框如何包含資料框?
我擁有的資料采用標準 tibble(或 data.frame)格式,因此我需要從該格式回傳到該資料結構。使用上面的示例資料從 tibbles 開始,我已經嘗試過(在許多其他方面:
data <- raw.content$data
header <- raw.content$header
> str(data.frame(header, data))
'data.frame': 308 obs. of 15 variables:
$ parameterUnit : chr "m.s-1" "m.s-1" "m.s-1" "m.s-1" ...
$ parameterNumber : int 2 3 2 3 2 3 2 3 2 3 ...
$ dx : num 1 1 1 1 1 1 1 1 1 1 ...
$ dy : num 1 1 1 1 1 1 1 1 1 1 ...
$ parameterNumberName : chr "eastward_wind" "northward_wind" "eastward_wind" "northward_wind" ...
$ la1 : num -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 ...
$ la2 : num -28.5 -28.5 -28.5 -28.5 -28.5 -28.5 -28.5 -28.5 -28.5 -28.5 ...
$ parameterCategory : int 2 2 2 2 2 2 2 2 2 2 ...
$ lo2 : num 156 156 156 156 156 156 156 156 156 156 ...
$ nx : int 14 14 14 14 14 14 14 14 14 14 ...
$ ny : int 22 22 22 22 22 22 22 22 22 22 ...
$ refTime : chr "2017-02-01 23:00:00" "2017-02-01 23:00:00" "2017-02-01 23:00:00" "2017-02-01 23:00:00" ...
$ lo1 : num 143 143 143 143 143 143 143 143 143 143 ...
$ c.0..0..0..0..0..0..0..0..0..0..0..0..0..0..0..4.17000007629395.. : num 0 0 0 0 0 0 0 0 0 0 ...
$ c.0..0..0..0..0..0..0..0..0..0..0..0..0..0..0...3.88000011444092..: num 0 0 0 0 0 0 0 0 0 0 ...
Warning message:
In data.frame(header, data) :
row names were found from a short variable and have been discarded
data.x <- data[[1]]
data.y <- data[[2]]
newdata <- vector(mode = "list", length = 2)
newdata[[1]] <- data.x
newdata[[2]] <- data.y
> str(data.frame(header, newdata))
'data.frame': 308 obs. of 15 variables:
$ parameterUnit : chr "m.s-1" "m.s-1" "m.s-1" "m.s-1" ...
$ parameterNumber : int 2 3 2 3 2 3 2 3 2 3 ...
$ dx : num 1 1 1 1 1 1 1 1 1 1 ...
$ dy : num 1 1 1 1 1 1 1 1 1 1 ...
$ parameterNumberName : chr "eastward_wind" "northward_wind" "eastward_wind" "northward_wind" ...
$ la1 : num -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 ...
$ la2 : num -28.5 -28.5 -28.5 -28.5 -28.5 -28.5 -28.5 -28.5 -28.5 -28.5 ...
$ parameterCategory : int 2 2 2 2 2 2 2 2 2 2 ...
$ lo2 : num 156 156 156 156 156 156 156 156 156 156 ...
$ nx : int 14 14 14 14 14 14 14 14 14 14 ...
$ ny : int 22 22 22 22 22 22 22 22 22 22 ...
$ refTime : chr "2017-02-01 23:00:00" "2017-02-01 23:00:00" "2017-02-01 23:00:00" "2017-02-01 23:00:00" ...
$ lo1 : num 143 143 143 143 143 143 143 143 143 143 ...
$ c.0..0..0..0..0..0..0..0..0..0..0..0..0..0..0..4.17000007629395.. : num 0 0 0 0 0 0 0 0 0 0 ...
$ c.0..0..0..0..0..0..0..0..0..0..0..0..0..0..0...3.88000011444092..: num 0 0 0 0 0 0 0 0 0 0 ...
Warning message:
In data.frame(header, newdata) :
row names were found from a short variable and have been discarded
正如您所看到的,它們都生成一個資料框,而不是包含一個資料框和一個串列的資料框。
uj5u.com熱心網友回復:
您可以將這兩個專案放在一個串列中,然后手動設定屬性:
header <- raw.content$header
data <- raw.content$data
result <- list(header, data)
attributes(result) <- list(names = c("header", "data"),
class = "data.frame",
row.names = 1:2)
str(result)
#> 'data.frame': 2 obs. of 2 variables:
#> $ header:'data.frame': 2 obs. of 13 variables:
#> ..$ parameterUnit : chr "m.s-1" "m.s-1"
#> ..$ parameterNumber : int 2 3
#> ..$ dx : num 1 1
#> ..$ dy : num 1 1
#> ..$ parameterNumberName: chr "eastward_wind" "northward_wind"
#> ..$ la1 : num -7.5 -7.5
#> ..$ la2 : num -28.5 -28.5
#> ..$ parameterCategory : int 2 2
#> ..$ lo2 : num 156 156
#> ..$ nx : int 14 14
#> ..$ ny : int 22 22
#> ..$ refTime : chr "2017-02-01 23:00:00" "2017-02-01 23:00:00"
#> ..$ lo1 : num 143 143
#> $ data :List of 2
#> ..$ : num 0 0 0 0 0 0 0 0 0 0 ...
#> ..$ : num 0 0 0 0 0 0 0 0 0 0 ...
由reprex 包于 2022-02-28 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/435547.html
