2012-06-25 27 views
0

我想弄清楚如何将数据库转储文件读入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" }}

回答

1

数据库转储看起来像一个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 

如果您发布的数据的一个完整的例子,我也许可以写一个更强大的片段(现列头名的数量是硬-coded)。

+0

谢谢!我去检查'json'的文档,发现你可以使用'fromJSON'命令读取整个数据文件。 在我的情况下,多行被包裹成'{}',大括号。你可以请,告诉我如何阅读我的整个文件? – notrockstar

+0

您可以发布更具代表性的数据样本,包括开始和结束? – mhermans