2017-11-17 3 views
0

mapデータのリストをkeys and valuesに変換しようとしています。しかし、私はそれを変換するために問題に直面して、私は以下のコードで試してみましたが、私は私の要件を満足させていません。R:データフレームからのキー値ペアで出力を生成できません

{ 
    "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] 
     } 
    ] 
    ] 
} 

しかしIamは、以下に指定でJSON出力を除く:私は上記のコードのため

> 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以下画像におけるショーなどのデータを必要としますフォーマットキー値のペア。

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'というパラメータを使用していませんので、lenghtyコードを書いています。 そして、上記のコード 'jsonlite :: toJSON(purList1、dataframe =" column "、pretty = TRUE)を使用している場合、'すべての列を取得しますが、 'second、コード。 もう一度ありがとうございます。 – user7462639

+0

私は、それが長いことが妥当であることがわかります。詳細をありがとう:) – yutannihilation

関連する問題