2014-11-14 31 views
1

我想將多個json文件讀入工作目錄以便進一步轉換爲數據集。我在json目錄下有text1,text2,text3文件。下面是我寫的代碼:從一個目錄導入多個json文件並附上數據

setwd("Users/Desktop/json") 
temp = list.files(pattern="text*.") 
myfiles = lapply(temp, read.delim) 
library("rjson") 
json_file <- "myfiles" 
library(jsonlite) 
out <- jsonlite::fromJSON(json_file) 
out[vapply(out, is.null, logical(1))] <- "none" 
data.frame(out, stringsAsFactors = FALSE)[,1:5] 
View(out) 

我有大約200文件,所以我想知道是否有辦法在其中JSON文件可以導入。

感謝

+0

看看[this](http://stackoverflow.com/questions/35421870/reading-multiple-json-files-in-a-directory-into-one-data-frame/42512668#42512668)解決方案以及。 – amonk 2017-02-28 15:29:17

回答

1

我認爲我的工作瓦特/ Twitter的數據時,也有類似的問題。我有一個目錄包含每個用戶名的單獨文件,我想導入/分析它們作爲一個組。這對我工作:

library(rjson) 
filenames <- list.files("Users/Desktop/json", pattern="*.json", full.names=TRUE) # this should give you a character vector, with each file name represented by an entry 
myJSON <- lapply(filenames, function(x) fromJSON(file=x)) # a list in which each element is one of your original JSON files 

如果這不起作用,那麼我需要更多的信息來了解你的問題。

相關問題