如何将字典数据从R中的JSON文件转换为数据帧? [英] How to convert dictionary data in to dataframe from JSON file in R?

查看:138
本文介绍了如何将字典数据从R中的JSON文件转换为数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R的新手.我想读取JSON文件并将其转换为表格结构文件.您能帮我如何将以下数据转换为表格结构格式吗?数据位于JSON文件中,如下所示:

I'm new to the R. And I want to read the JSON file and convert into tabular structure file. Can you please help me how to convert the following data into the tabular structure format. The data is in JSON file as follows:

{"Physics":8,"Chemistry":7,"PhysicalEducation":6,"English":7,"Mathematics":7,"serial":47738}
{"Physics":1,"Chemistry":1,"PhysicalEducation":1,"English":3,"Mathematics":2,"serial":85520}
{"Physics":2,"Chemistry":1,"Biology":2,"English":4,"Mathematics":8,"serial":182318}
{"Physics":3,"Chemistry":4,"PhysicalEducation":5,"English":5,"Mathematics":8,"serial":77482}
{"Accountancy":2,"BusinessStudies":5,"Economics":3,"English":6,"Mathematics":7,"serial":152940}

我想转换成这样

Sub1 Sub2 Sub3 Sub4 Sub5 Serial
 8    7    6    7   7    47738
 1    1    1    3   2    85520  
 2    1    2    4   8    182318
 3    4    5    5   8    77482
 2    5    3    6   7    152940

您能帮我在R中做到这一点吗?您的帮助将非常有用.

Can you please help me how to do that in R. Your help will be highly appreciable.

推荐答案

library(rjson)
Lines <- readLines("training_backup.json") 
json_df <- as.data.frame(t(sapply(Lines, fromJSON)))
rownames(json_df) <- NULL

更新的答案

这篇关于如何将字典数据从R中的JSON文件转换为数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆