我喜欢将数据从 R 绘制到 highcharts。我的数据框如下所示:
dput(head(df,10))
structure(list(DATE = structure(c(1362027751, 1362027781, 1362027811,
1362027841, 1362027871, 1362027901, 1362027931, 1362027961, 1362027991,
1362028021), class = c("POSIXct", "POSIXt")), CPU = c(66L, 72L,
69L, 72L, 69L, 68L, 71L, 73L, 63L, 57L)), .Names = c("DATE",
"CPU"), row.names = c(NA, 10L), class = "data.frame")
我正在将此 df 转换为 json 格式,如下所示:
ll <- as.list(setNames(df$CPU,df$DATE))
library(RJSONIO)
data<-toJSON(ll)
cat(data, file="data.json")
当我查看 data.json 文件时,它看起来像这样:
{
"2013-02-28 00:02:31": 66,
"2013-02-28 00:03:01": 72,
"2013-02-28 00:03:31": 69,
"2013-02-28 00:04:01": 72,
"2013-02-28 00:04:31": 69,
"2013-02-28 00:05:01": 68,
"2013-02-28 00:05:31": 71,
"2013-02-28 00:06:01": 73,
}
我需要我的 data.json 文件看起来像这样: [[date, value],[date,value],[date,value]]
[
["2013-02-28 00:02:31": 66],
["2013-02-28 00:03:01": 72],
["2013-02-28 00:03:31": 69],
["2013-02-28 00:04:01": 72]
]
我将如何在 R 中做到这一点?