r - 将嵌套的json文件转换为R中的Dataframe
问题描述
我有如下给出的 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)
})
解决方案
检查与rjson
fromJSON
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
推荐阅读
- python - 无法为多分类模型生成 ROC 曲线
- ruby-on-rails - Rails Rspec 测试失败:失败/错误:致命:数据库“jmschp”不存在
- angular - HTTP响应以角度打印为[object Object],[object Object]
- python - 值错误:使用序列数据预处理设置数组元素
- reactjs - 我可以在非渲染组件中使用 use* 函数(useState、useRef、useContext)吗?
- r - 单击 R Shiny 中的不同按钮后,在同一框中显示表格或图表
- python - Minecraft 服务器 .jar 使用子进程通过 python 脚本控制槽
- android - `postProcessing` 块不能与`setMinifyEnabled` 方法一起使用
- java - 如何将 JTable 单元格值从 A 类返回到 B 类
- c# - 如何在asp.net mvc中区分普通用户和管理员