R:无法从数据帧生成Key值对中的输出

时间:2017-11-17 11:47:45

标签: r dplyr data.table tidyverse jsonlite

我正在尝试使用map将数据列表转换为keys and values数据。但我面临转换它的问题,我尝试使用下面的代码,但我不满足我的要求。我需要如下图所示的数据

> 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 个答案:

答案 0 :(得分: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)就足够了)