2012-06-25 28 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