簡體   English   中英

將數據庫轉儲文件讀入R

[英]reading database dump file into R

我試圖弄清楚如何將數據庫轉儲文件讀入R中的表中。

這是文件的第一行:

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

我需要將其解析為具有適當列標題的表。

我只知道read.tablescan和基本命令來獲取格式正確的數據集。

謝謝您的幫助。

編輯:

我的數據庫轉儲看起來像這樣:

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM