簡體   English   中英

將嵌套的json文件轉換為R中的Dataframe

[英]Convert nested json file to Dataframe in R

我有下面給出的json文件。 我想將其轉換為數據框。

json_file-> [{'a': "abc", "date": "20190506", "my_col":{"weather":10, "ice": 12}},
             {'a': "xyz", "date": "20190507", "my_col":{"summer":18, "hot": 14}}]

數據框應如下所示:

 a  date      mycol
abc 20190506 "weather":10, "ice": 12
xyz 20190507 "summer":18, "hot": 14

嘗試:

json_file <- fromJSON(json_file)

json_file <- lapply(json_file, function(x) {
  x[sapply(x, is.null)] <- NA
  unlist(x)
})

fromJSON檢查rjson

library(rjson)
l='[{"a": "abc", "date": "20190506", "my_col":{"weather":10, "ice": 12}},{"a": "xyz", "date": "20190507", "my_col":{"summer":18, "hot": 14}}]' 
l = fromJSON(l)
do.call(rbind, l)
     a     date       my_col
[1,] "abc" "20190506" List,2
[2,] "xyz" "20190507" List,2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM