2016-09-23 3 views
1

У меня есть несколько файлов JSON с текстами, сгруппированными в date, body и title. В качестве примера рассмотрим:Создание корпуса из текстов, хранящихся в файлах JSON, в R

{"date": "December 31, 1990, Monday, Late Edition - Final", "body": "World stock markets begin 1991 facing the threat of a war in the Persian Gulf, recessions or economic slowdowns around the world, and dismal earnings -- the same factors that drove stock markets down sharply in 1990. Finally, there is the problem of the Soviet Union, the wild card in everyone's analysis. It is a country whose problems could send stock markets around the world reeling if something went seriously awry. With Russia about to implode, that just adds to the risk premium, said Mr. Dhar. LOAD-DATE: December 30, 1990 ", "title": "World Markets;"} 
{"date": "December 30, 1992, Sunday, Late Edition - Final", "body": "DATELINE: CHICAGO Gleaming new tractors are becoming more familiar sights on America's farms. Sales and profits at the three leading United States tractor makers -- Deere & Company, the J.I. Case division of Tenneco Inc. and the Ford Motor Company's Ford New Holland division -- are all up, reflecting renewed agricultural prosperity after the near-depression of the early and mid-1980's. But the recovery in the tractor business, now in its third year, is fragile. Tractor makers hope to install computers that can digest this information, then automatically concentrate the application of costly fertilizer and chemicals on the most productive land. Within the next 15 years, that capability will be commonplace, predicted Mr. Ball. LOAD-DATE: December 30, 1990 ", "title": "All About/Tractors;"} 

У меня есть три различных газет с отдельными файлами, содержащими все тексты, полученные за период 1989 - 2016 Моя конечная цель состоит в том, чтобы объединить все тексты в единый корпус. Я сделал это в Python, используя библиотеку pandas, и мне интересно, можно ли это сделать в R аналогичным образом. Вот мой код с петлей в R:

for (i in 1989:2016){ 
    df0 = pd.DataFrame([json.loads(l) for l in open('NYT_%d.json' % i)]) 
    df1 = pd.DataFrame([json.loads(l) for l in open('USAT_%d.json' % i)]) 
    df2 = pd.DataFrame([json.loads(l) for l in open('WP_%d.json' % i)]) 
    appended_data.append(df0) 
    appended_data.append(df1) 
    appended_data.append(df2) 
} 

ответ

2

Там много вариантов R читать json файл и конвертировать их в data.frame/data.table.

Здесь с помощью jsonlite и data.table:

library(data.table) 
library(jsonlite) 
res <- lapply(1989:2016,function(i){ 
    ff <- c('NYT_%d.json','USAT_%d.json' ,'WP_%d.json') 
    list_files_paths <- sprintf(ff,i) 
    rbindlist(lapply(list_files_paths,fromJSON)) 
    }) 

Здесь рес список data.table. Если вы хотите, чтобы объединить все data.table в одном data.table:

rbindlist(res) 
3

Используйте jsonlite::stream_in читать ваши файлы и jsonlite::rbind.pages объединить их.

0

Использование ndjson::stream_in читать их быстрее и более плоские, чем jsonlite::stream_in :-)

Смежные вопросы