[英]reading database dump file into R
我试图弄清楚如何将数据库转储文件读入R中的表中。
这是文件的第一行:
{ "id" : { "id" : "43" }, "type" : "Account::Private", "full_name" : "Joe Doe" }
我需要将其解析为具有适当列标题的表。
我只知道read.table
, scan
和基本命令来获取格式正确的数据集。
谢谢您的帮助。
编辑:
我的数据库转储看起来像这样:
{ {"id" : { "id" : "43" }, "type" : "Account::Private", "full_name" : "Joe Doe" }, {"id" : { "id" : "44" }, "type" : "Account::Private", "full_name" : "Jane Doe" }, {"id" : { "id" : "45" }, "type" : "Account::Private", "full_name" : "John Doe" }}
数据库转储看起来像一个JSON结构。 我假设将多行包装为列表,即“ [”和“]”之间。
这个片段
install.packages('rjson')
library(rjson)
s <- '[ {"id" : { "id" : "43" }, "type" : "Account::Private", "full_name" : "Joe Doe" },
{"id" : { "id" : "44" }, "type" : "Account::Private", "full_name" : "Jane Doe" },
{"id" : { "id" : "45" }, "type" : "Account::Private", "full_name" : "John Doe" }]'
js <- fromJSON(s)
d <- data.frame(t(matrix(unlist(js), ncol=3)))
names(d) <- c('id', 'type', 'full_name')
d
给
id type full_name
1 43 Account::Private Joe Doe
2 44 Account::Private Jane Doe
3 45 Account::Private John Doe
如果您发布数据的完整示例,则也许可以编写一个更健壮的代码段(现在,列数和标头名称的数量已硬编码)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.