2017-10-19 14 views
0

json 파일을 R의 데이터 프레임으로 구문 분석하는 데 문제가 있습니다. json 파일을 데이터 프레임으로 바꿀 수 있었지만 못 박는 것처럼 보일 수 없습니다. "기하학"열. 아래는 json 파일의 샘플입니다.R의 데이터 프레임에 좌표가있는 Unjest json 파일

[ 
{ 
    "point_id": 4, 
    "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      -101.5961904, 
      31.7070736 
     ] 
    }, 
    "NumericID": "4543842", 
} 
] 

아래 코드를 사용하여 중첩 시키려고하면 오류가 발생합니다.

ex_data<-lapply(ex_data, function(x) ifelse (x == "NULL", NA, x)) 
ex_data<-as.data.frame(do.call(rbind, ex_data)) 
ex_data<-ex_data%>% bind_rows(ex_data) %>% # make larger sample data 
mutate_if(is.list, simplify_all) # flatten each list element internally 
ex_data%>%unnest(geometry)->ex_data_unnest 
Error: Each column must either be a list of vectors or a list of data frames 
[geometry] 

감사

답변

0

사용 jsonlite::stream_in() json으로 파일을 읽을 수있는 (내가 JSON 파일에 예를 복사 여러 번) :

df <- stream_in(file("test.json")) 

편집 :

Unnesting 기하학 열 :

df <- as.data.frame(cbind(df[,-2], 
    do.call(rbind,df$geometry$coordinates),  
    df$geometry$type), 
    stringsAsFactors = F) 
names(df)[3:5] <- c("geometry.coordinates1","geometry.coordinates2","geometry.type") 

df 
    point_id NumericID geometry.coordinates1 geometry.coordinates2 geometry.type 
1   4 4543842    -101.5962    31.70707   Point 
2   4 4543842    -101.5962    31.70707   Point 

이제 data.frame

+0

에서 값에 액세스 할 수 있습니다. 도움을 제공해 주셔서 감사합니다. – jsimpsno