0

我试图弄清楚如何将数据库转储文件读入R中的表中。

这是文件的第一行:

{ "id" : { "id" : "43" }, "type" : "Account::Private", "full_name" : "Joe Doe" }

我需要将其解析为具有适当列标题的表。

我只知道格式良好的数据集的read.table、、scan和基本命令。

谢谢您的帮助。

编辑:

我的 db-dump 看起来像这样:

{ {"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" }}

4

1 回答 1

1

The database-dump looks like a JSON structure. I'm assuming multiple rows are wrapped as a list, i.e. between "[" and "]".

This snippet

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

gives

  id             type full_name
1 43 Account::Private   Joe Doe
2 44 Account::Private  Jane Doe
3 45 Account::Private  John Doe

If you post a full example of the data, I can perhaps write a more robust snippet (now the number of columns and header names are hard-coded).

于 2012-06-25T22:07:00.840 回答