【问题标题】:R: Unable to generate output in Key value pair from dataframeR:无法从数据帧的键值对中生成输出
【发布时间】:2018-05-01 04:34:15
【问题描述】:

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

> 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)

为上述代码生成的 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输出。

【问题讨论】:

    标签: r dplyr data.table tidyverse jsonlite


    【解决方案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) 可能就足够了)

    【讨论】:

    • 谢谢@yutannihilation。
    • 最初我正在尝试 jsonlite::toJSON(purList1, dataframe = "column", pretty = TRUE) ,但不使用参数 dataframe 所以我正在编写冗长的代码。如果我使用上面的代码jsonlite::toJSON(purList1, dataframe = "column", pretty = TRUE),我会得到所有列,但我只需要sec,Counting 列,所以我更喜欢使用上面的代码。再次感谢您。
    • 我明白了,冗长是合理的。感谢您的详细信息:)
    猜你喜欢
    • 1970-01-01
    • 2019-09-03
    • 1970-01-01
    • 2021-12-12
    • 2021-09-27
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多