我在名为data.json
的JSON
文件中有一些信息,如下所示:
{
"header" : {
"apiVersion" : "v1",
"code" : "200",
"service" : "catalogwebservice",
"developerMessage" : "",
"userMessage" : "OK",
"errorCode" : "1",
"docLink" : "https://ega-archive.org",
"errorStack" : ""
},
"response" : {
"numTotalResults" : 12,
"resultType" : "SampleData",
"result" : [ {
"alias" : "JKDFG093.T2",
"egaStableId" : "EGAN00003456789",
"centerName" : "Novartis",
"creationTime" : "2016-05-13Y17:08.001Z",
"title" : "JKDFG093.T2",
"bioSampleId" : "MADFG110656789",
"subjectId" : "JKDFG093",
"gender" : "male",
"phenotype" : "Cancer",
"attributes" : null
}, {
"alias" : "JKDFG093.T1",
"egaStableId" : "EGAN00003456780",
"centerName" : "Novartis",
"creationTime" : "2016-05-13Y17:08.001Z",
"title" : "JKDFG093.T1",
"bioSampleId" : "MADFG110656790",
"subjectId" : "JKDFG093",
"gender" : "female",
"phenotype" : "Cancer",
"attributes" : null
}, {
"alias" : "JKDFG087.T1",
"egaStableId" : "EGAN00003456781",
"centerName" : "Novartis",
"creationTime" : "2016-05-13Y17:08.001Z",
"title" : "JKDFG087.T1",
"bioSampleId" : "MADFG110656791",
"subjectId" : "JKDFG087",
"gender" : "male",
"phenotype" : "Cancer",
"attributes" : null
} ]
}
}
我想将JSON
文件中的信息转换为 Dataframe 。我需要来自上述JSON文件的alias, egaStableId, centerName, creationTime, title, bioSampleId, subjectId, gender, phenotype, and attributes
等信息作为列名,并在 Dataframe 中显示它们各自的信息。
我加载了JSON file in R
并尝试将其转换为 Dataframe ,但最终出现了一些错误。
library(rjson)
data <- rjson::fromJSON(file = "data.json")
json_data_frame <- as.data.frame(data)
我得到的错误:
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, :
arguments imply differing number of rows: 1, 0
任何帮助都很感激。谢谢!!
1条答案
按热度按时间6uxekuva1#
您需要深入研究数据以获得结果,即
$response$result
。将此处的
json
替换为您的文件名:也就是说,如果查看
fromJSON
的输出,您会看到列表中嵌套了一个框架(嵌套得有点深):我使用的是
jsonlite
,我相信它与rjson
非常接近,应该可以执行相同的操作。如果 yours 未被列为在
str
-输出中,然后将其 Package 在as.data.frame
中,如下所示