我不知道如何正確地將我的JSON數據轉換爲有用的數據框。這是顯示了我的數據結構的一些示例數據:如何將複雜的JSON數據轉換爲單個數據框?
{
"data":[
{"track":[
{"time":"2015","midpoint":{"x":6,"y":8},"realworld":{"x":1,"y":3},"coordinate":{"x":16,"y":38}},
{"time":"2015","midpoint":{"x":6,"y":8},"realworld":{"x":1,"y":3},"coordinate":{"x":16,"y":37}},
{"time":"2016","midpoint":{"x":6,"y":9},"realworld":{"x":2,"y":3},"coordinate":{"x":16,"y":38}}
]},
{"track":[
{"time":"2015","midpoint":{"x":5,"y":9},"realworld":{"x":-1,"y":3},"coordinate":{"x":16,"y":38}},
{"time":"2015","midpoint":{"x":5,"y":9},"realworld":{"x":-1,"y":3},"coordinate":{"x":16,"y":38}},
{"time":"2016","midpoint":{"x":5,"y":9},"realworld":{"x":-1,"y":3},"coordinate":{"x":16,"y":38}},
{"time":"2015","midpoint":{"x":3,"y":15},"realworld":{"x":-9,"y":2},"coordinate":{"x":17,"y":38}}
]},
{"track":[
{"time":"2015","midpoint":{"x":6,"y":7},"realworld":{"x":-2,"y":3},"coordinate":{"x":16,"y":39}}
]}]}
我有很多的曲目,我想數據集看起來像這樣:
track time midpoint realworld coordinate
1
1
1
2
2
2
2
3
到目前爲止,我有這個:
json_file <- "testdata.json"
data <- fromJSON(json_file)
data2 <- list.stack(data, fill=TRUE)
現在它出來是這樣的:
我怎樣才能以適當的格式得到這個?
IMO它本來的超級信息在你的迭代留到最後解決方案,但最終的結果是++優雅。 – hrbrmstr
@hrbrmstr thx :-)但是你的意思是什麼?「在迭代中留下最後的解決方案」*? – Jaap
啊,你說得對。我誤解了最終版本,並認爲你已經刪除了sapply(它只是格式不同)。我現在試圖弄清楚爲什麼'purrr :: flatten_df()' - 它只是真的調用'dplyr :: bind_rows()' - 不適用於此。我還設法通過一些'purrr'調用來對R進行分段,所以這對OP來說是一個很好的職位(需要提交一些GH問題)。 – hrbrmstr