【问题标题】:Convert data frame to JSON in this format以这种格式将数据帧转换为 JSON
【发布时间】:2013-09-12 10:51:37
【问题描述】:

我有一个希望以特定格式输出到 JSON 的数据框,下面有一个小示例:

dat <- structure(list(unit = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
  2L), .Label = c("A", "B"), class = "factor"), type = structure(c(1L, 
  1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("X", "Y"), class = "factor"), 
  date = structure(c(1357963687, 1357963869, 1357964048, 1357964230, 
  1357963687, 1357963942, 1357963942, 1357964123), class = c("POSIXct", 
  "POSIXt"), tzone = ""), latitude = c(-21.21, -21.22, -21.23, 
  -21.24, -21.23, -21.23, -21.23, -21.23), longitude = c(116.78, 
  116.77, 116.76, 116.75, 116.74, 116.75, 116.75, 116.76)), .Names = c("unit", 
  "type", "date", "latitude", "longitude"), row.names = c(NA, -8L
  ), class = "data.frame")

我需要的 JSON 格式如下所示:

    [{"unit":"A","type":"X","latitude":[["2013-01-12 12:08:07",-21.21],["2013-01-12 12:11:09",-21.22],["2013-01-12 12:14:08",-21.23],["2013-01-12 12:17:10",-21.24]],
                           "longitude":[["2013-01-12 12:08:07",116.78],["2013-01-12 12:11:09",116.77],["2013-01-12 12:14:08",116.76],["2013-01-12 12:17:10",116.75]]
    },
    {"unit":"B","type":"X", "latitude":[["2013-01-12 12:08:07",-21.23],["2013-01-12 12:12:22",-21.23],["2013-01-12 12:12:22",-21.23],["2013-01-12 12:15:23",-21.23]],
                           "longitude":[["2013-01-12 12:08:07",116.74],["2013-01-12 12:12:22",116.75],["2013-01-12 12:12:22",116.75],["2013-01-12 12:15:23",116.76]]
    }]

到目前为止,我一直无法操纵 RJSONIO::toJSON 函数来做类似的事情,而且我发现文档中的示例没有太大帮助。

我需要做什么才能获得正确的输出?

注意:每个unit 只会有一个type

PS:有没有一种工具可以让这些事情变得简单?也许是拖放的东西?

【问题讨论】:

    标签: json r rjsonio rjson


    【解决方案1】:

    您可以通过首先将数据框转换为列表列表来接近。例如:

    > a=list(unit="A",type="X",latitude=c(1,2,3),longitude=c(4,5,6))
    > b=list(unit="B",type="Y",latitude=c(11,22,33),longitude=c(43,54,65))
    > dlist = list(a,b)
    > cat(toJSON(dlist))
    [
     {
     "unit": "A",
    "type": "X",
    "latitude": [      1,      2,      3 ],
    "longitude": [      4,      5,      6 ] 
    },
    {
     "unit": "B",
    "type": "Y",
    "latitude": [     11,     22,     33 ],
    "longitude": [     43,     54,     65 ] 
    } 
    ]
    

    问题实际上是如何将数据框操作为正确的格式。

    但是,您的 JSON 输出在向量中混合了字符和数字类型:["2013-01-12 12:08:07",-21.23],我不知道如何从坚持向量是单一类型的 R 中得到它。 ["2013-01-12 12:08:07","-21.23"] 可以接受吗?如果是这样,请继续阅读...

    plyr 包包含大量用于拆分和操作数据框和列表的代码。例如:

    dlply(dat,~unit)
    

    将通过unit 变量拆分数据帧。您可以将一个函数应用于每个部分并返回一个列表。这个函数:

    make1 <- function(d){   
       list(
             unit=d$unit[1],
             type=d$type[1],
             latitude=cbind(as.character(d$date),d$latitude),
             longitude=cbind(as.character(d$date),d$longitude))
       }
    

    应该将一个部分转换为正确的列表格式。所以告诉dlply 对每个部分都这样做,并返回一个列表列表。该列表具有名称,这使得 toJSON 输出为命名数组 - 我们需要删除名称以获得 JS 列表。

    > L = dlply(dat,~unit,make1)
    > names(L)=NULL
    > cat(toJSON(L))
     [
     {
     "unit": "A",
    "type": "X",
    "latitude": [ [ "2013-01-12 04:08:07", "-21.21" ],
    [ "2013-01-12 04:11:09", "-21.22" ],
    [ "2013-01-12 04:14:08", "-21.23" ],
    [ "2013-01-12 04:17:10", "-21.24" ] ],
    "longitude": [ [ "2013-01-12 04:08:07", "116.78" ],
    [ "2013-01-12 04:11:09", "116.77" ],
    [ "2013-01-12 04:14:08", "116.76" ],
    [ "2013-01-12 04:17:10", "116.75" ] ] 
    },
    {
     "unit": "B",
    "type": "Y",
    "latitude": [ [ "2013-01-12 04:08:07", "-21.23" ],
    [ "2013-01-12 04:12:22", "-21.23" ],
    [ "2013-01-12 04:12:22", "-21.23" ],
    [ "2013-01-12 04:15:23", "-21.23" ] ],
    "longitude": [ [ "2013-01-12 04:08:07", "116.74" ],
    [ "2013-01-12 04:12:22", "116.75" ],
    [ "2013-01-12 04:12:22", "116.75" ],
    [ "2013-01-12 04:15:23", "116.76" ] ] 
    } 
    ]
    

    好玩吗?

    【讨论】:

    • 那很好。我将能够在 JS 中输入转换它。
    • 这很有帮助。由于latitudelongitude 实际上是数组的数组,我想我需要这样做:a=list(unit="A",type="X",latitude=list(c(1,10),c(2,12),c(3,13)),longitude=list(c(4,12),c(5,11),c(6,12)))。现在只需要弄清楚如何将其应用于任意数量的单位(我的完整数据集有数百个,在最初的问题中并不清楚)
    • 回复:您的完整答案。做得好。很好的解释和紧凑的代码。谢谢!
    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多