2017-11-17 1 views
0

map 데이터 목록을 keys and values으로 변환하려고합니다. 하지만 그것을 변환하는 문제에 직면, 나는 아래의 코드로 시도했지만 내 요구 사항을 만족하지 않습니다. I는 상기 코드R : 데이터 프레임의 키 값 쌍에 출력을 생성 할 수 없습니다.

> df <- data.frame(sec = c('15:31:36',"15:31:37",'15:31:37','15:31:37','15:31:38','15:31:39'), 
        label = c("Choose to and fro flights","Choose to and fro flights","Details","Details","Details","Details"), 
        responseCode = c(200,200,200,'Non HTTP response code: org.apache.http.NoHttpResponseException','200','200'), 
        Counting = c(9,1,2,2,5,1)) 

> purList1 <- lapply(split(df, df$label), function(x) split(x, x$responseCode,drop = T)) 

> output <- toJSON(
      list(ResponseCode = 
      lapply(names(purList1),function(x){ 
       ss = purList1[[x]] 
       tt = lapply(names(ss),function(y){      
        items = ss[[y]][c("sec","Counting")] 
        sub = data.frame(R_C_seconds = as.vector(items$sec), 
             R_C_Count = as.vector(items$Counting) 
        ) 

        c(as.vector(unlist(data.frame(y))), as.vector(sub)) 
       }) 

       c(as.vector(x), as.vector(tt)) 
      }) 
) 
,pretty = T) 

Genereated Optput 아래의 화상 표시 등의 데이터를 필요

{ 
    "ResponseCode": [ 
    [ 
     ["Choose to and fro flights"], 
     { 
     "1": ["200"], 
     "R_C_seconds": ["15:31:36", "15:31:37"], 
     "R_C_Count": [9, 1] 
     } 
    ], 
    [ 
     ["Details"], 
     { 
     "1": ["200"], 
     "R_C_seconds": ["15:31:37", "15:31:38", "15:31:39"], 
     "R_C_Count": [2, 5, 1] 
     }, 
     { 
     "1": ["Non HTTP response code: org.apache.http.NoHttpResponseException"], 
     "R_C_seconds": ["15:31:37"], 
     "R_C_Count": [2] 
     } 
    ] 
    ] 
} 

하지만 스피 에서 아래 지정된 JSON 출력을 제외한 형식 키 값 쌍.

enter image description here

답변

1

코드는 불필요하게 복잡하게 보인다. 목록의 트리가 쉽게 혼란 스러울 수 있습니다. 한 번에 전체 purList1을 다루지 마십시오. 먼저 purList1[[1]]과 같은 하위 집합으로 재생하십시오.

reprex::reprex_info() 
#> Created by the reprex package v0.1.1.9000 on 2017-11-18 

df <- data.frame(sec = c('15:31:36',"15:31:37",'15:31:37','15:31:37','15:31:38','15:31:39'), 
        label = c("Choose to and fro flights","Choose to and fro flights","Details","Details","Details","Details"), 
        responseCode = c(200,200,200,'Non HTTP response code: org.apache.http.NoHttpResponseException','200','200'), 
        Counting = c(9,1,2,2,5,1)) 

purList1 <- lapply(split(df, df$label), function(x) split(x, x$responseCode,drop = T)) 


# play with a subset and construct the function to apply 
jsonlite::toJSON(
    lapply(
    purList1[[1]], 
    function(x) list(
     R_C_seconds = x$sec, 
     R_C_Count = x$Counting 
    ) 
), 
    pretty = TRUE 
) 
#> { 
#> "200": { 
#>  "R_C_seconds": ["15:31:36", "15:31:37"], 
#>  "R_C_Count": [9, 1] 
#> } 
#> } 


# apply the function on the whole list 
jsonlite::toJSON(
    lapply(
    purList1, 
    lapply, 
    function(x) list(
     R_C_seconds = x$sec, 
     R_C_Count = x$Counting 
    ) 
), 
    pretty = TRUE 
) 
#> { 
#> "Choose to and fro flights": { 
#>  "200": { 
#>  "R_C_seconds": ["15:31:36", "15:31:37"], 
#>  "R_C_Count": [9, 1] 
#>  } 
#> }, 
#> "Details": { 
#>  "200": { 
#>  "R_C_seconds": ["15:31:37", "15:31:38", "15:31:39"], 
#>  "R_C_Count": [2, 5, 1] 
#>  }, 
#>  "Non HTTP response code: org.apache.http.NoHttpResponseException": { 
#>  "R_C_seconds": ["15:31:37"], 
#>  "R_C_Count": [2] 
#>  } 
#> } 
#> } 



는 (잘 모르겠어요하지만 당신은 단지 열 방향으로 data.frames을 변환 할 경우 jsonlite::toJSON(purList1, dataframe = "column", pretty = TRUE) 충분히있을 수 있습니다)

+0

당신이 @yutannihilation 감사드립니다. – user7462639

+0

처음에는'jsonlite :: toJSON (purList1, dataframe = "column", pretty = TRUE)'을 시도했지만 매개 변수'dataframe'을 사용하지 않았습니다. 그래서 길이 코드를 작성하고 있습니다. 그리고 만약 위의 코드'jsonlite :: toJSON (purList1, dataframe = "column", pretty = TRUE)을 사용한다면'나는 모든 열을 얻는다. 암호. 다시 한번 감사드립니다. – user7462639

+0

나는 길다는 것이 합리적이다. 세부 사항 주셔서 감사합니다 :) – yutannihilation

관련 문제