我有一个数据框,我希望以特定格式输出到 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
函数来执行类似的操作,而且我发现文档中的示例并没有太大帮助。
我需要做什么才能获得正确的输出?
注意:type
每个unit
.
PS:有没有工具可以让这些事情变得简单?也许是拖放的东西?