2016-09-23 8 views
1

date, bodytitle으로 그룹화 된 텍스트가있는 여러 JSON 파일이 있습니다. 예를 들어 고려 :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 년 내 궁극적 인 목표는 하나의 신체에있는 모든 텍스트를 결합하는 것입니다. 나는 파이썬에서 팬더 라이브러리를 사용하여이 작업을 수행했으며 비슷한 방식으로 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

이 많은 옵션 json 파일을 읽고 data.table data.frame /로 변환하기 : 여기 내 R의 루프 코드입니다. jsonlitedata.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보다 빠르고 평평하게 읽을 수 있습니다.